Last active
August 2, 2023 18:03
-
-
Save Draugr-official/7e0e6c19efc49edc3c91485940e3db73 to your computer and use it in GitHub Desktop.
A simple wrapper around the OpenAI API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class AiModel | |
{ | |
HttpClient httpClient = new HttpClient(); | |
List<GptMessage> Messages = new List<GptMessage>(); | |
public AiModel() | |
{ | |
httpClient.DefaultRequestHeaders.Add("api-key", "APIKEY"); | |
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer APIKEY"); | |
Messages.Add(new GptMessage() | |
{ | |
role = "system", | |
content = AiPrompts.SupportPrompt | |
}); | |
} | |
/// <summary> | |
/// Generates a reply given the current prompt and context | |
/// </summary> | |
/// <param name="message"></param> | |
/// <returns></returns> | |
public string GenerateReply(string message) | |
{ | |
Messages.Add(new GptMessage() | |
{ | |
role = "user", | |
content = message | |
}); | |
string completion = RequestCompletion(); | |
Messages.Add(new GptMessage() | |
{ | |
role = "assistant", | |
content = completion | |
}); | |
return completion; | |
} | |
/// <summary> | |
/// Requests a completion with the current messages to the OpenAI API | |
/// </summary> | |
/// <returns></returns> | |
string RequestCompletion() | |
{ | |
GptData gptData = new GptData() | |
{ | |
model = "gpt-3.5-turbo", | |
messages = Messages, | |
stream = false | |
}; | |
HttpContent content = new StringContent(JsonConvert.SerializeObject(gptData), Encoding.UTF8, "application/json"); | |
HttpResponseMessage response = httpClient.PostAsync("https://api.openai.com/v1/chat/completions", content).GetAwaiter().GetResult(); | |
ChatCompletion chatCompletion = JsonConvert.DeserializeObject<ChatCompletion>(response.Content.ReadAsStringAsync().Result); | |
return chatCompletion.Choices[0].Message.Content; | |
} | |
/// <summary> | |
/// A message that will be sendt to GPT | |
/// </summary> | |
public class GptMessage | |
{ | |
/// <summary> | |
/// <para>system = a system message to explain the behaviour of the AI</para> | |
/// <para>user = a reference message from the user</para> | |
/// <para>assistant = a reference message from the AI</para> | |
/// </summary> | |
public string role { get; set; } | |
/// <summary> | |
/// The content of the message | |
/// </summary> | |
public string content { get; set; } | |
} | |
public class GptData | |
{ | |
public string model { get; set; } | |
public List<GptMessage> messages { get; set; } | |
public bool stream { get; set; } | |
} | |
public class ChatCompletion | |
{ | |
public string Id { get; set; } | |
public string Object { get; set; } | |
public long Created { get; set; } | |
public string Model { get; set; } | |
public List<Choice> Choices { get; set; } | |
public Usage Usage { get; set; } | |
} | |
public class Choice | |
{ | |
public int Index { get; set; } | |
public Message Message { get; set; } | |
public FinishReason FinishReason { get; set; } | |
} | |
public enum FinishReason | |
{ | |
Stop, | |
Timeout, | |
Error | |
} | |
public class Message | |
{ | |
public string Role { get; set; } | |
public string Content { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment