2023-09-23 11:21:46 +02:00
|
|
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
2023-09-23 15:33:16 +02:00
|
|
|
using LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
2023-09-23 11:21:46 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
|
|
namespace LLMApi.Services;
|
|
|
|
|
|
|
|
public class ChatGpt3_5Service : ILlmApiService
|
|
|
|
{
|
|
|
|
private readonly HttpClient _client;
|
2023-09-23 15:33:16 +02:00
|
|
|
private static readonly JsonSerializerSettings jsonDeserializerSettings = new()
|
2023-09-23 11:21:46 +02:00
|
|
|
{
|
2023-09-23 12:00:41 +02:00
|
|
|
ContractResolver = new DefaultContractResolver()
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
|
|
|
}
|
2023-09-23 11:21:46 +02:00
|
|
|
};
|
|
|
|
private const string VERSION_IDENTIFIER = "gpt-3.5-turbo";
|
|
|
|
|
2023-09-23 21:51:28 +02:00
|
|
|
private readonly string _llmRuleset;
|
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
public ChatGpt3_5Service(HttpClient client, IConfiguration configuration)
|
2023-09-23 11:21:46 +02:00
|
|
|
{
|
|
|
|
_client = client;
|
2023-09-23 15:33:16 +02:00
|
|
|
|
2023-09-23 11:21:46 +02:00
|
|
|
_client.BaseAddress = new Uri("https://api.openai.com");
|
2023-09-23 15:33:16 +02:00
|
|
|
|
|
|
|
var api_key = configuration["api_key"];
|
|
|
|
|
|
|
|
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {api_key}");
|
2023-09-23 21:51:28 +02:00
|
|
|
|
|
|
|
_llmRuleset = configuration["llm_ruleset"] ?? "";
|
2023-09-23 11:21:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
// 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);
|
2023-09-23 11:21:46 +02:00
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
// ArgumentNullException.ThrowIfNull(response);
|
2023-09-23 12:00:41 +02:00
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
// var choices = response.Choices.OrderBy(c => c.Index);
|
2023-09-23 11:21:46 +02:00
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
// return choices;
|
|
|
|
// }
|
|
|
|
|
|
|
|
public async Task<AnswerResponse> GetAnswer(AnswerRequest answerRequest)
|
|
|
|
{
|
|
|
|
var (context, prompt) = answerRequest;
|
|
|
|
|
2023-09-23 21:51:28 +02:00
|
|
|
Message[] newContext;
|
|
|
|
if (context is null)
|
|
|
|
{
|
|
|
|
newContext = new[]
|
|
|
|
{
|
|
|
|
new Message("system", _llmRuleset),
|
|
|
|
new Message("user", prompt)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-24 12:57:19 +02:00
|
|
|
newContext = new List<Message>(context)
|
|
|
|
{
|
|
|
|
new("user", prompt)
|
|
|
|
}.ToArray();
|
2023-09-23 21:51:28 +02:00
|
|
|
}
|
2023-09-23 15:33:16 +02:00
|
|
|
|
|
|
|
var requestContract = new ChatCompletionRequest(VERSION_IDENTIFIER, newContext);
|
|
|
|
|
|
|
|
var responseMessage = await _client.PostAsJsonAsync("/v1/chat/completions", requestContract);
|
|
|
|
var responseContent = await responseMessage.Content.ReadAsStringAsync();
|
2023-09-24 12:57:19 +02:00
|
|
|
// var responseContent = await File.ReadAllTextAsync("/home/finn/Documents/pit_hackathon/GOA.git/llmapi/LLMApi/test.json");
|
2023-09-23 15:33:16 +02:00
|
|
|
var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
|
2023-09-23 12:00:41 +02:00
|
|
|
|
2023-09-23 15:33:16 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(response);
|
|
|
|
|
|
|
|
var choices = response.Choices.OrderBy(c => c.Index);
|
2023-09-24 12:57:19 +02:00
|
|
|
return new(newContext, choices);
|
2023-09-23 11:21:46 +02:00
|
|
|
}
|
|
|
|
}
|