They can now answer
This commit is contained in:
@@ -6,14 +6,16 @@ using OpenAI.Chat;
|
||||
|
||||
namespace TelegramBot;
|
||||
|
||||
public class OpenAiAgent
|
||||
{
|
||||
public class OpenAiAgent {
|
||||
private ApiKeyCredential apikey;
|
||||
private OpenAIClient oaiClient;
|
||||
private ChatClient chatClient;
|
||||
private HttpClient httpClient;
|
||||
|
||||
private Dictionary<(long, Actor), List<(ChatMessage, int)>> oaiChats = new();
|
||||
|
||||
public int ContextSize { get; set; } = 4096;
|
||||
|
||||
public OpenAiAgent(string baseUrl, string apiKey, string model) {
|
||||
OpenAIClientOptions options = new OpenAIClientOptions() {
|
||||
Endpoint = new(baseUrl),
|
||||
@@ -79,8 +81,29 @@ public class OpenAiAgent
|
||||
return chat!;
|
||||
}
|
||||
|
||||
public string GetChatResponse(long chatId, Actor actor) {
|
||||
return String.Empty;
|
||||
public string GetChatResponse(long chatId, Agent agent) {
|
||||
int currentContextSize = agent.SystemPromptLength;
|
||||
List<ChatMessage> chatHistory = new();
|
||||
chatHistory.Add(new SystemChatMessage(agent.SystemPrompt));
|
||||
|
||||
//Fetch the chat history from the dictionary trimming to the context size
|
||||
var history = GetChatHistory(chatId, agent.Actor).ToList();
|
||||
history.Reverse();
|
||||
//Add the chat history to the list until the context size is reached
|
||||
foreach (var (message, tokenLenght) in history) {
|
||||
if (currentContextSize + tokenLenght > ContextSize) break;
|
||||
chatHistory.Add(message);
|
||||
currentContextSize += tokenLenght;
|
||||
}
|
||||
//Reverse the chat history to get the correct order
|
||||
chatHistory.Reverse(1, chatHistory.Count - 1);
|
||||
|
||||
var completion = chatClient.CompleteChat(chatHistory).Value.Content[0].Text;
|
||||
|
||||
//Add the response to the chat history
|
||||
ChatHistoryAppend(agent.Actor, chatId, completion);
|
||||
|
||||
return completion;
|
||||
}
|
||||
|
||||
public void AddChatToDictionary(long id) {
|
||||
|
||||
Reference in New Issue
Block a user