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> 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(responseContent, jsonDeserializerSettings); // ArgumentNullException.ThrowIfNull(response); // var choices = response.Choices.OrderBy(c => c.Index); // return choices; // } public async Task 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(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(responseContent, jsonDeserializerSettings); ArgumentNullException.ThrowIfNull(response); var choices = response.Choices.OrderBy(c => c.Index); return new(newContext, choices); } }