-
Notifications
You must be signed in to change notification settings - Fork 350
Add serialization/deserialization example with chat completions #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add serialization/deserialization example with chat completions
- Loading branch information
commit 4eb919daf2b274f24c2186724b28e4c938724da8
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| using NUnit.Framework; | ||
| using OpenAI.Chat; | ||
| using System; | ||
| using System.ClientModel.Primitives; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
|
|
||
| namespace OpenAI.Examples; | ||
|
|
||
| public partial class ChatExamples | ||
| { | ||
| #region | ||
| public static List<ChatMessage> DeserializeMessages(BinaryData data) | ||
| { | ||
| List<ChatMessage> messages = []; | ||
| using JsonDocument messagesAsJson = JsonDocument.Parse(data.ToString()); | ||
|
|
||
| foreach (JsonElement jsonElement in messagesAsJson.RootElement.EnumerateArray()) | ||
| { | ||
| BinaryData jsonElementAsData = BinaryData.FromString(jsonElement.ToString()); | ||
joseharriaga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ChatMessage message = ModelReaderWriter.Read<ChatMessage>(jsonElementAsData, ModelReaderWriterOptions.Json); | ||
| messages.Add(message); | ||
| } | ||
|
|
||
| return messages; | ||
| } | ||
| #endregion | ||
|
|
||
| #region | ||
| public static BinaryData SerializeMessages(IEnumerable<ChatMessage> messages) | ||
| { | ||
| using MemoryStream stream = new(); | ||
| using Utf8JsonWriter writer = new(stream); | ||
|
|
||
| writer.WriteStartArray(); | ||
| foreach (ChatMessage message in messages) | ||
| { | ||
| (message as IJsonModel<ChatMessage>).Write(writer, ModelReaderWriterOptions.Json); | ||
| } | ||
| writer.WriteEndArray(); | ||
| writer.Flush(); | ||
|
|
||
| return BinaryData.FromBytes(stream.ToArray()); | ||
| } | ||
| #endregion | ||
|
|
||
| [Test] | ||
| public void Example06_ChatSerialization() | ||
joseharriaga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | ||
|
|
||
| BinaryData serializedData = BinaryData.FromBytes(""" | ||
| [ | ||
| { | ||
| "role": "user", | ||
| "content": "Who won the world series in 2020?" | ||
| }, | ||
| { | ||
| "role": "assistant", | ||
| "content": "The Los Angeles Dodgers won the World Series in 2020." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": "Where was it played?" | ||
| } | ||
| ] | ||
| """u8.ToArray()); | ||
|
|
||
| List<ChatMessage> messages = DeserializeMessages(serializedData); | ||
|
|
||
| ChatCompletion completion = client.CompleteChat(messages); | ||
| messages.Add(new AssistantChatMessage(completion)); | ||
|
|
||
| foreach (ChatMessage message in messages) | ||
| { | ||
| switch (message) | ||
| { | ||
| case UserChatMessage userMessage: | ||
| Console.WriteLine($"[USER]:"); | ||
| break; | ||
|
|
||
| case AssistantChatMessage assistantMessage when assistantMessage.Content.Count > 0: | ||
| Console.WriteLine($"[ASSISTANT]:"); | ||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| Console.WriteLine($"{message.Content[0].Text}"); | ||
| Console.WriteLine(); | ||
| } | ||
|
|
||
| serializedData = SerializeMessages(messages); | ||
|
|
||
| Console.WriteLine("****************************************************"); | ||
| Console.WriteLine(); | ||
| Console.WriteLine(serializedData.ToString()); | ||
| } | ||
| } | ||
This file contains hidden or 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,64 @@ | ||
| using NUnit.Framework; | ||
| using OpenAI.Chat; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace OpenAI.Examples; | ||
|
|
||
| public partial class ChatExamples | ||
| { | ||
| [Test] | ||
| public async Task Example06_ChatSerializationAsync() | ||
| { | ||
| ChatClient client = new("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | ||
|
|
||
| BinaryData serializedData = BinaryData.FromBytes(""" | ||
| [ | ||
| { | ||
| "role": "user", | ||
| "content": "Who won the world series in 2020?" | ||
| }, | ||
| { | ||
| "role": "assistant", | ||
| "content": "The Los Angeles Dodgers won the World Series in 2020." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": "Where was it played?" | ||
| } | ||
| ] | ||
| """u8.ToArray()); | ||
|
|
||
| List<ChatMessage> messages = DeserializeMessages(serializedData); | ||
|
|
||
| ChatCompletion completion = await client.CompleteChatAsync(messages); | ||
| messages.Add(new AssistantChatMessage(completion)); | ||
|
|
||
| foreach (ChatMessage message in messages) | ||
| { | ||
| switch (message) | ||
| { | ||
| case UserChatMessage userMessage: | ||
| Console.WriteLine($"[USER]:"); | ||
| break; | ||
|
|
||
| case AssistantChatMessage assistantMessage when assistantMessage.Content.Count > 0: | ||
| Console.WriteLine($"[ASSISTANT]:"); | ||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| Console.WriteLine($"{message.Content[0].Text}"); | ||
| Console.WriteLine(); | ||
| } | ||
|
|
||
| serializedData = SerializeMessages(messages); | ||
|
|
||
| Console.WriteLine("****************************************************"); | ||
| Console.WriteLine(); | ||
| Console.WriteLine(serializedData.ToString()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.