Skip to content

Commit

Permalink
Restructured OpenAiLlmClient.cs and Campaign.razor
Browse files Browse the repository at this point in the history
  • Loading branch information
mirakst committed Nov 9, 2023
1 parent 8f5c6fd commit e6c6169
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 38 deletions.
16 changes: 2 additions & 14 deletions ChatRPG/API/IOpenAiLlmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@ namespace ChatRPG.API;

public interface IOpenAiLlmClient
{
Task<string> GetChatCompletion(OpenAiGptMessage input);
Task<string> GetChatCompletion(List<OpenAiGptMessage> inputs);
IAsyncEnumerable<string> GetStreamedChatCompletion(OpenAiGptMessage input);
IAsyncEnumerable<string> GetStreamedChatCompletion(List<OpenAiGptMessage> inputs);
Task<string> GetChatCompletion(params OpenAiGptMessage[] inputs);
IAsyncEnumerable<string> GetStreamedChatCompletion(params OpenAiGptMessage[] inputs);
}

public record ChatCompletionObject(string Id, string Object, int Created, string Model, Choice[] Choices, Usage Usage);

public record Choice(int Index, Message Message, string FinishReason);

public record Message(string Role, string Content);

public record Usage(int PromptTokens, int CompletionTokens, int TotalTokens);

public record OpenAiGptMessage(string Role, string Content);

public record OpenAiGptInput(string Model, List<OpenAiGptMessage> Messages, double Temperature);
26 changes: 6 additions & 20 deletions ChatRPG/API/OpenAiLlmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,36 @@ public class OpenAiLlmClient : IOpenAiLlmClient
private const string Model = "gpt-3.5-turbo";
private const double Temperature = 0.7;

private readonly IHttpClientFactory _httpClientFactory;
private readonly OpenAIAPI _openAiApi;

public OpenAiLlmClient(IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
_openAiApi = new OpenAIAPI(configuration.GetSection("ApiKeys").GetValue<string>("OpenAI") ?? string.Empty);
_openAiApi.Chat.DefaultChatRequestArgs.Model = Model;
_openAiApi.Chat.DefaultChatRequestArgs.Temperature = Temperature;
_openAiApi.HttpClientFactory = httpClientFactory;
}

public async Task<string> GetChatCompletion(OpenAiGptMessage input)
{
List<OpenAiGptMessage> inputList = new List<OpenAiGptMessage> { input };
return await GetChatCompletion(inputList);
}

public async Task<string> GetChatCompletion(List<OpenAiGptMessage> inputs)
public async Task<string> GetChatCompletion(params OpenAiGptMessage[] inputs)
{
Conversation chat = CreateConversation(inputs);

return await chat.GetResponseFromChatbotAsync();
}

public IAsyncEnumerable<string> GetStreamedChatCompletion(OpenAiGptMessage input)
{
List<OpenAiGptMessage> inputList = new List<OpenAiGptMessage> { input };
return GetStreamedChatCompletion(inputList);
}

public IAsyncEnumerable<string> GetStreamedChatCompletion(List<OpenAiGptMessage> inputs)
public IAsyncEnumerable<string> GetStreamedChatCompletion(params OpenAiGptMessage[] inputs)
{
Conversation chat = CreateConversation(inputs);

return chat.StreamResponseEnumerableFromChatbotAsync();
}

private Conversation CreateConversation(List<OpenAiGptMessage> inputs)
private Conversation CreateConversation(params OpenAiGptMessage[] messages)
{
if (inputs.IsNullOrEmpty()) throw new ArgumentNullException(nameof(inputs));
if (messages.IsNullOrEmpty()) throw new ArgumentNullException(nameof(messages));

Conversation chat = _openAiApi.Chat.CreateConversation();
_openAiApi.HttpClientFactory = _httpClientFactory;
foreach (OpenAiGptMessage openAiGptInputMessage in inputs)
foreach (OpenAiGptMessage openAiGptInputMessage in messages)
{
chat.AppendMessage(ChatMessageRole.FromString(openAiGptInputMessage.Role), openAiGptInputMessage.Content);
}
Expand Down
8 changes: 4 additions & 4 deletions ChatRPG/Pages/Campaign.razor
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@
OpenAiGptMessage userInput = new OpenAiGptMessage("user", _userInput);
_conversation.Add(userInput);

if (!_shouldStream)
if (_shouldStream)
{
string response = await OpenAiLlmClient.GetChatCompletion(userInput);
HandleResponse(response);
await HandleStreamedResponse(OpenAiLlmClient.GetStreamedChatCompletion(userInput));
}
else
{
await HandleStreamedResponse(OpenAiLlmClient.GetStreamedChatCompletion(userInput));
string response = await OpenAiLlmClient.GetChatCompletion(userInput);
HandleResponse(response);
}

if (_shouldSave && _fileUtil != null)
Expand Down

0 comments on commit e6c6169

Please sign in to comment.