Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ChatHistory interface #669

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/ChatChineseGB2312.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task Run()
else
{
var chatHistoryJson = File.ReadAllText("Assets/chat-with-kunkun-chinese.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
IChatHistory chatHistory = ChatHistorySerializer.FromJson(chatHistoryJson, typeof(ChatHistory)) ?? new ChatHistory();

session = new ChatSession(executor, chatHistory);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ in session.RegenerateAssistantMessageAsync(
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
new Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/ChatSessionStripRoleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static async Task Run()
var executor = new InteractiveExecutor(context);

var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
IChatHistory chatHistory = ChatHistorySerializer.FromJson(chatHistoryJson, typeof(ChatHistory)) ?? new ChatHistory();

ChatSession session = new(executor, chatHistory);
session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
Expand All @@ -46,7 +46,7 @@ public static async Task Run()
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
new Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/ChatSessionWithHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static async Task Run()
else
{
var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
IChatHistory chatHistory = ChatHistorySerializer.FromJson(chatHistoryJson, typeof(ChatHistory)) ?? new ChatHistory();

session = new ChatSession(executor, chatHistory);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ in session.RegenerateAssistantMessageAsync(
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
new Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Expand Down
14 changes: 7 additions & 7 deletions LLama.Examples/Examples/ChatSessionWithRestart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static async Task Run()
var executor = new InteractiveExecutor(context);

var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
ChatSession prototypeSession =
IChatHistory chatHistory = ChatHistorySerializer.FromJson(chatHistoryJson, typeof(ChatHistory)) ?? new ChatHistory();
ChatSession prototypeSession =
await ChatSession.InitializeSessionFromHistoryAsync(executor, chatHistory);
prototypeSession.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
new string[] { "User:", "Assistant:" },
Expand Down Expand Up @@ -50,10 +50,10 @@ public static async Task Run()
while (userInput != "exit")
{
// Load the session state from the reset state
if(userInput == "reset")
if (userInput == "reset")
{
session.LoadSession(resetState);
Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.History)}");
Console.WriteLine($"Reset to history:\n{session.HistoryTransform.HistoryToText(session.SessionChatHistory)}");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Session reset.");
}
Expand All @@ -75,10 +75,10 @@ public static async Task Run()

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Provide assistant input: ");

Console.ForegroundColor = ConsoleColor.Green;
string assistantInputOverride = Console.ReadLine() ?? "";

await session.AddAndProcessUserMessage(userInputOverride);
await session.AddAndProcessAssistantMessage(assistantInputOverride);

Expand All @@ -90,7 +90,7 @@ public static async Task Run()
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
new Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/Examples/ChatSessionWithRoleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static async Task Run()
var executor = new InteractiveExecutor(context);

var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
IChatHistory chatHistory = ChatHistorySerializer.FromJson(chatHistoryJson, typeof(ChatHistory)) ?? new ChatHistory();

ChatSession session = new(executor, chatHistory);

Expand All @@ -41,7 +41,7 @@ public static async Task Run()
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, userInput),
new Message(AuthorRole.User, userInput),
inferenceParams))
{
Console.ForegroundColor = ConsoleColor.White;
Expand Down
2 changes: 1 addition & 1 deletion LLama.Examples/Examples/LoadAndSaveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task Run()
await foreach (
var text
in session.ChatAsync(
new ChatHistory.Message(AuthorRole.User, prompt),
new Message(AuthorRole.User, prompt),
new InferenceParams()
{
Temperature = 0.6f,
Expand Down
2 changes: 1 addition & 1 deletion LLama.SemanticKernel/ChatCompletion/HistoryTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LLamaSharp.SemanticKernel.ChatCompletion;
public class HistoryTransform : DefaultHistoryTransform
{
/// <inheritdoc/>
public override string HistoryToText(global::LLama.Common.ChatHistory history)
public string HistoryToText(global::LLama.Common.ChatHistory history)
{
return base.HistoryToText(history) + $"{AuthorRole.Assistant}: ";
}
Expand Down
2 changes: 1 addition & 1 deletion LLama.WebAPI/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<string> SendHistory([FromBody] HistoryInput input, [FromServic
{
var history = new ChatHistory();

var messages = input.Messages.Select(m => new ChatHistory.Message(Enum.Parse<AuthorRole>(m.Role), m.Content));
var messages = input.Messages.Select(m => new Message(Enum.Parse<AuthorRole>(m.Role), m.Content));

history.Messages.AddRange(messages);

Expand Down
6 changes: 3 additions & 3 deletions LLama.WebAPI/Services/StatefulChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public StatefulChatService(IConfiguration configuration, ILogger<StatefulChatSer
_context = new LLamaContext(weights, @params);

_session = new ChatSession(new InteractiveExecutor(_context));
_session.History.AddMessage(Common.AuthorRole.System, SystemPrompt);
_session.SessionChatHistory.AddMessage(Common.AuthorRole.System, SystemPrompt);
}

public void Dispose()
Expand All @@ -46,7 +46,7 @@ public async Task<string> Send(SendMessageInput input)
}
_logger.LogInformation("Input: {text}", input.Text);
var outputs = _session.ChatAsync(
new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text),
new Common.Message(Common.AuthorRole.User, input.Text),
new Common.InferenceParams()
{
RepeatPenalty = 1.0f,
Expand Down Expand Up @@ -74,7 +74,7 @@ public async IAsyncEnumerable<string> SendStream(SendMessageInput input)
_logger.LogInformation(input.Text);

var outputs = _session.ChatAsync(
new Common.ChatHistory.Message(Common.AuthorRole.User, input.Text!)
new Common.Message(Common.AuthorRole.User, input.Text!)
, new Common.InferenceParams()
{
RepeatPenalty = 1.0f,
Expand Down
3 changes: 1 addition & 2 deletions LLama.WebAPI/Services/StatelessChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ public async Task<string> SendAsync(ChatHistory history)
}
public class HistoryTransform : DefaultHistoryTransform
{
public override string HistoryToText(ChatHistory history)
public override string HistoryToText(IChatHistory history)
{
return base.HistoryToText(history) + "\n Assistant:";
}

}
}
8 changes: 5 additions & 3 deletions LLama/Abstractions/IHistoryTransform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LLama.Common;
using System;
using System.Text.Json.Serialization;

namespace LLama.Abstractions
Expand All @@ -14,15 +15,16 @@ public interface IHistoryTransform
/// </summary>
/// <param name="history">The ChatHistory instance</param>
/// <returns></returns>
string HistoryToText(ChatHistory history);
string HistoryToText(IChatHistory history);

/// <summary>
/// Converts plain text to a ChatHistory instance.
/// </summary>
/// <param name="role">The role for the author.</param>
/// <param name="text">The chat history as plain text.</param>
/// <param name="type">The type of the chat history.</param>
/// <returns>The updated history.</returns>
ChatHistory TextToHistory(AuthorRole role, string text);
IChatHistory TextToHistory(AuthorRole role, string text, Type type);

/// <summary>
/// Copy the transform.
Expand Down
Loading
Loading