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

90 lines
3.0 KiB
C#
Raw Permalink Normal View History

2023-09-23 11:21:46 +02:00
using LLMApi.Data.Contracts.ChatGpt3_5;
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;
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;
public ChatGpt3_5Service(HttpClient client, IConfiguration configuration)
2023-09-23 11:21:46 +02:00
{
_client = client;
2023-09-23 11:21:46 +02:00
_client.BaseAddress = new Uri("https://api.openai.com");
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
}
// 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
// ArgumentNullException.ThrowIfNull(response);
2023-09-23 12:00:41 +02:00
// var choices = response.Choices.OrderBy(c => c.Index);
2023-09-23 11:21:46 +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
}
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");
var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
2023-09-23 12:00:41 +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
}
}