This repository has been archived on 2023-11-16. You can view files and clone it, but cannot push or open issues or pull requests.
GOA/LLMApi/Services/ChatGpt3_5.cs
2023-09-24 12:57:19 +02:00

90 lines
3.0 KiB
C#

using LLMApi.Data.Contracts.ChatGpt3_5;
using LLMApi.Data.Contracts.Game.ChatGpt3_5;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace LLMApi.Services;
public class ChatGpt3_5Service : ILlmApiService
{
private readonly HttpClient _client;
private static readonly JsonSerializerSettings jsonDeserializerSettings = new()
{
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
private const string VERSION_IDENTIFIER = "gpt-3.5-turbo";
private readonly string _llmRuleset;
public ChatGpt3_5Service(HttpClient client, IConfiguration configuration)
{
_client = client;
_client.BaseAddress = new Uri("https://api.openai.com");
var api_key = configuration["api_key"];
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {api_key}");
_llmRuleset = configuration["llm_ruleset"] ?? "";
}
// public async Task<IEnumerable<Choice>> GetAnswerToPrompt(string prompt)
// {
// var requestContract = new ChatCompletionRequest(VERSION_IDENTIFIER,
// new Message[]
// {
// new("system", "You are a helpful assistant."),
// new("user", prompt),
// });
// var responseMessage = await _client.PostAsJsonAsync("/v1/chat/completions", requestContract);
// var responseContent = await responseMessage.Content.ReadAsStringAsync();
// var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
// ArgumentNullException.ThrowIfNull(response);
// var choices = response.Choices.OrderBy(c => c.Index);
// return choices;
// }
public async Task<AnswerResponse> GetAnswer(AnswerRequest answerRequest)
{
var (context, prompt) = answerRequest;
Message[] newContext;
if (context is null)
{
newContext = new[]
{
new Message("system", _llmRuleset),
new Message("user", prompt)
};
}
else
{
newContext = new List<Message>(context)
{
new("user", prompt)
}.ToArray();
}
var requestContract = new ChatCompletionRequest(VERSION_IDENTIFIER, newContext);
var responseMessage = await _client.PostAsJsonAsync("/v1/chat/completions", requestContract);
var responseContent = await responseMessage.Content.ReadAsStringAsync();
// var responseContent = await File.ReadAllTextAsync("/home/finn/Documents/pit_hackathon/GOA.git/llmapi/LLMApi/test.json");
var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
ArgumentNullException.ThrowIfNull(response);
var choices = response.Choices.OrderBy(c => c.Index);
return new(newContext, choices);
}
}