From 77f03a339b77b529cf0f6087276c81d69d7714f9 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Tue, 11 Jun 2024 17:17:42 -0700 Subject: [PATCH 1/2] Added sample showing how to do cancellations --- .../Example01_SimpleChat_Cancellations.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/Chat/Example01_SimpleChat_Cancellations.cs diff --git a/examples/Chat/Example01_SimpleChat_Cancellations.cs b/examples/Chat/Example01_SimpleChat_Cancellations.cs new file mode 100644 index 00000000..efe33fbc --- /dev/null +++ b/examples/Chat/Example01_SimpleChat_Cancellations.cs @@ -0,0 +1,30 @@ +using NUnit.Framework; +using OpenAI.Chat; +using System; +using System.ClientModel; +using System.ClientModel.Primitives; +using System.Threading; + +namespace OpenAI.Examples; + +public partial class ChatExamples +{ + [Test] + public void Example01_SimpleChat_Cancellations() + { + ChatClient client = new(model: "gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); + + CancellationTokenSource ct = new CancellationTokenSource(); + RequestOptions options = new() { CancellationToken = ct.Token }; + + // The following code will be simplified in the future. + var wireFormat = new ModelReaderWriterOptions("W"); + ChatMessage message = ChatMessage.CreateUserMessage("Say 'this is a test.'"); + BinaryData json = ModelReaderWriter.Write(message, wireFormat); + + ClientResult result = client.CompleteChat(BinaryContent.Create(json), options); + + ChatCompletion completion = ModelReaderWriter.Read(result.GetRawResponse().Content, wireFormat); + Console.WriteLine($"[ASSISTANT]: {completion}"); + } +} From 6a5af6a8e11b0f0cb2d41af25053c76d93286f20 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Wed, 12 Jun 2024 10:26:14 -0700 Subject: [PATCH 2/2] fixed a bug --- .../Example01_SimpleChat_Cancellations.cs | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/Chat/Example01_SimpleChat_Cancellations.cs b/examples/Chat/Example01_SimpleChat_Cancellations.cs index efe33fbc..aaf13df0 100644 --- a/examples/Chat/Example01_SimpleChat_Cancellations.cs +++ b/examples/Chat/Example01_SimpleChat_Cancellations.cs @@ -17,13 +17,23 @@ public void Example01_SimpleChat_Cancellations() CancellationTokenSource ct = new CancellationTokenSource(); RequestOptions options = new() { CancellationToken = ct.Token }; - // The following code will be simplified in the future. - var wireFormat = new ModelReaderWriterOptions("W"); ChatMessage message = ChatMessage.CreateUserMessage("Say 'this is a test.'"); - BinaryData json = ModelReaderWriter.Write(message, wireFormat); - - ClientResult result = client.CompleteChat(BinaryContent.Create(json), options); + var body = new { + model = "gpt-4o", + messages = new[] { + new + { + role = "user", + content = "Say \u0027this is a test.\u0027" + } + } + }; + BinaryData json = BinaryData.FromObjectAsJson(body); + ClientResult result = client.CompleteChat(BinaryContent.Create(json), options); + + // The following code will be simplified in the future. + var wireFormat = new ModelReaderWriterOptions("W"); ChatCompletion completion = ModelReaderWriter.Read(result.GetRawResponse().Content, wireFormat); Console.WriteLine($"[ASSISTANT]: {completion}"); }