-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Marco-Zechner/11-refactor-and-add-rest-of…
…-api-model 11 refactor and add rest of api model # Added the last missing API types logit_bias logprobs top_logprobs Added Custom Exception for GPTs Error response Added Examples to Test Project Split GPTModel into 3 partial classes Added Payload and Response Handler where the json Content can be changed/printed/etc. by the user if needed. Split some Models from one file into their own file each. # Added LogProbs support in the GPT ChatResponse Added example usage for LogProbs in ConversationWithCustomHandlers Removed Deprecated Role "Functions"
- Loading branch information
Showing
19 changed files
with
399 additions
and
173 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
OpenAI.ChatGPT.Net.IntegrationTests/ConversationWithCustomHandlers.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using OpenAI.ChatGPT.Net.DataModels; | ||
using OpenAI.ChatGPT.Net.Exeptions; | ||
|
||
namespace OpenAI.ChatGPT.Net.IntegrationTests | ||
{ | ||
public class ConversationWithCustomHandlers | ||
{ | ||
public static async Task Run() | ||
{ | ||
static string PrintPayloadHandler(string payload) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Green; | ||
Console.WriteLine("Payload:"); | ||
Console.WriteLine(payload); | ||
Console.ResetColor(); | ||
return payload; | ||
} | ||
|
||
static string PrintResponseHandler(string response) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
Console.WriteLine("Response:"); | ||
Console.WriteLine(response); | ||
Console.ResetColor(); | ||
return response; | ||
} | ||
|
||
GPTModel model = new("f", APIKey.KEY) | ||
{ | ||
PayloadHandler = PrintPayloadHandler, | ||
ResponseHandler = PrintResponseHandler, | ||
Logprobs = true, | ||
TopLogprobs = 2 | ||
}; | ||
|
||
/*======================================================================\\ | ||
|| The following code is the same as the SimpleConversationTest.cs file || | ||
|| But as an example the try-catch was moved inside and the catch won't || | ||
|| stop the conversation. || | ||
\\======================================================================*/ | ||
|
||
Console.Write($"{ChatRole.User}: "); | ||
ChatMessage initialMessage = new(ChatRole.User, Console.ReadLine()); | ||
List<ChatMessage> messageHistory = [initialMessage]; | ||
|
||
while (true) | ||
{ | ||
ChatResponse response = await model.Complete(messageHistory); | ||
ChatMessage message; | ||
try { | ||
message = (ChatMessage)response; // This will throw an exception if the response is an error | ||
|
||
Console.WriteLine($"{message.Role}: {message.Content}"); | ||
messageHistory.Add(message); | ||
} catch (GPTAPIResponseException ex) { | ||
Console.WriteLine(ex.Message); | ||
// You don't need to stop here, you can try to recover from the error. | ||
model.Model = "gpt-4o"; // Change to a different model since we know this is the error | ||
Console.WriteLine("Switching to model \"gpt-4o\""); | ||
|
||
// continue; | ||
// be carful with skipping user input, this could lead to infinit recalling of the model. | ||
// a better approach is to let the user reenter his message. | ||
messageHistory.RemoveAt(messageHistory.Count - 1); | ||
} | ||
|
||
Console.Write($"{ChatRole.User}: "); | ||
ChatMessage nextMessage = new(ChatRole.User, Console.ReadLine()); | ||
messageHistory.Add(nextMessage); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
using OpenAI.ChatGPT.Net.IntegrationTests; | ||
|
||
await SingleCompletionTest.Run(); | ||
//await SingleCompletionTest.TotalMin(); | ||
|
||
//SimpleConversationTest.Run(); | ||
//await SingleCompletionTest.Run(); | ||
|
||
//await SimpleConversationTest.Run(); | ||
|
||
await ConversationWithCustomHandlers.Run(); | ||
|
||
//AddTools.Run(); |
45 changes: 24 additions & 21 deletions
45
OpenAI.ChatGPT.Net.IntegrationTests/SimpleConversationTest.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
using OpenAI.ChatGPT.Net; | ||
using OpenAI.ChatGPT.Net.DataModels; | ||
using OpenAI.ChatGPT.Net.Exeptions; | ||
|
||
namespace OpenAI.ChatGPT.Net.IntegrationTests | ||
{ | ||
internal class SimpleConversationTest | ||
{ | ||
public static async void Run() | ||
public static async Task Run() | ||
{ | ||
//GPTModel model = new GPTModel("gpt-4o", "key"); | ||
GPTModel model = new("gpt-4o", APIKey.KEY); | ||
|
||
//GPTMessage initialMessage = new GPTMessage(GPTRole.User, Console.ReadLine()); | ||
//List<GPTMessage> messageHistory = [initialMessage]; | ||
Console.Write($"{ChatRole.User}: "); | ||
ChatMessage initialMessage = new(ChatRole.User, Console.ReadLine()); | ||
List<ChatMessage> messageHistory = [initialMessage]; | ||
|
||
//do | ||
//{ | ||
// GPTResponse response = await model.Complete(messageHistory); | ||
try | ||
{ | ||
while (true) | ||
{ | ||
ChatResponse response = await model.Complete(messageHistory); | ||
|
||
// if (response is GPTError error) | ||
// { | ||
// Console.WriteLine($"Error: {error.Message}"); | ||
// break; // or try to generate it again. | ||
// } | ||
ChatMessage message = (ChatMessage)response; | ||
Console.WriteLine(message); | ||
messageHistory.Add(message); | ||
|
||
// GPTMessage message = (GPTMessage)response; // or: GPTMessage messag = response as GPTMessage | ||
// Console.WriteLine(message.Role + ": " + message.Message); | ||
// messageHistory.Add(message); | ||
|
||
// GPTMessage nextMessage = new GPTMessage(GPTRole.User, Console.ReadLine()); | ||
// messageHistory.Add(nextMessage); | ||
//} | ||
//while (true); | ||
Console.Write($"{ChatRole.User}: "); | ||
ChatMessage nextMessage = new(ChatRole.User, Console.ReadLine()); | ||
messageHistory.Add(nextMessage); | ||
} | ||
} | ||
catch (GPTAPIResponseException ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ public enum ChatRole | |
System, | ||
User, | ||
Assistant, | ||
Tool, | ||
Function | ||
Tool | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,27 @@ | ||
using Newtonsoft.Json; | ||
using System.Text; | ||
|
||
namespace OpenAI.ChatGPT.Net.DataModels | ||
namespace OpenAI.ChatGPT.Net.DataModels | ||
{ | ||
// https://platform.openai.com/docs/api-reference/chat/create | ||
public class GPTModel(string model, string apiKey) | ||
public partial class GPTModel(string model, string apiKey) | ||
{ | ||
public string BaseUrl { get; set; } = "https://api.openai.com/v1/chat/completions"; | ||
private readonly string _apiKey = !string.IsNullOrWhiteSpace(apiKey) ? apiKey : throw new ArgumentException("API key cannot be null or empty", nameof(apiKey)); | ||
|
||
public string Model { get; } = !string.IsNullOrWhiteSpace(model) ? model : throw new ArgumentException("Model cannot be null or empty", nameof(model)); | ||
public double FrequencyPenalty { get; set; } = 0.0; | ||
public long MaxTokens { get; set; } = 256; | ||
public long N { get; set; } = 1; | ||
public double PresencePenalty { get; set; } = 0.0; | ||
public string Model { get; set; } = !string.IsNullOrWhiteSpace(model) ? model : throw new ArgumentException("Model cannot be null or empty", nameof(model)); | ||
public double? FrequencyPenalty { get; set; } = 0.0; | ||
public Dictionary<int, int>? LogitBias { get; set; } = null; | ||
public bool? Logprobs { get; set; } = null; | ||
public int? TopLogprobs { get; set; } = null; | ||
public long? MaxTokens { get; set; } = 256; | ||
public long? N { get; set; } = 1; | ||
public double? PresencePenalty { get; set; } = 0.0; | ||
public string[]? Stop { get; set; } = null; | ||
public bool Stream { get; set; } = false; | ||
public bool? Stream { get; set; } = false; | ||
public StreamOptions? StreamOptions { get; set; } = null; | ||
public double Temperature { get; set; } = 0.7; | ||
public double TopP { get; set; } = 0.9; | ||
public double? Temperature { get; set; } = 0.7; | ||
public double? TopP { get; set; } = 0.9; | ||
private readonly List<Tool>? tools = null; | ||
private readonly ToolChoice? toolChoice = null; | ||
public bool ParallelToolCalls { get; set; } = true; | ||
public string? User = null; | ||
|
||
|
||
public async Task<ChatResponse> Complete(ChatMessage initialMessage) => await Complete([initialMessage]); | ||
public async Task<ChatResponse> Complete(List<ChatMessage> chatHistory) | ||
{ | ||
var jsonPayload = GenerateJsonPayload(chatHistory); | ||
var jsonResponse = await SendAndReceiveJsonAsync(jsonPayload); | ||
return GenerateResponse(jsonResponse); | ||
} | ||
|
||
private static ChatResponse GenerateResponse(string jsonResponse) | ||
{ | ||
return JsonConvert.DeserializeObject<ChatResponse>(jsonResponse) ?? throw new JsonSerializationException("Deserialization failed."); | ||
} | ||
|
||
private string GenerateJsonPayload(List<ChatMessage> messages) | ||
{ | ||
ChatGPTRequest requestBody = new( | ||
Messages: messages, | ||
Model: Model, | ||
FrequencyPenalty: FrequencyPenalty, | ||
MaxTokens: MaxTokens, | ||
N: N, | ||
PresencePenalty: PresencePenalty, | ||
Stop: Stop, | ||
Stream: Stream, | ||
StreamOptions: StreamOptions, | ||
Temperature: Temperature, | ||
TopP: TopP, | ||
Tools: tools, | ||
ToolChoice: toolChoice, | ||
ParallelToolCalls: ParallelToolCalls, | ||
User: User | ||
); | ||
|
||
return JsonConvert.SerializeObject(requestBody, Formatting.Indented); | ||
} | ||
|
||
private async Task<string> SendAndReceiveJsonAsync(string jsonPayload) | ||
{ | ||
Console.WriteLine(jsonPayload); | ||
using var httpClient = new HttpClient(); | ||
var request = new HttpRequestMessage(HttpMethod.Post, BaseUrl) | ||
{ | ||
Headers = { { "Authorization", $"Bearer {_apiKey}" } }, | ||
Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json") | ||
}; | ||
var response = await httpClient.SendAsync(request); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
public bool? ParallelToolCalls { get; set; } = true; | ||
public string? User { get; set; } = null; | ||
} | ||
} |
Oops, something went wrong.