Skip to content

Commit

Permalink
Added sample showing how to do cancellations (#48)
Browse files Browse the repository at this point in the history
* Added sample showing how to do cancellations

* fixed a bug
  • Loading branch information
KrzysztofCwalina committed Jun 12, 2024
1 parent db6328a commit 63e50ea
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/Chat/Example01_SimpleChat_Cancellations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 };

ChatMessage message = ChatMessage.CreateUserMessage("Say 'this is a test.'");
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<ChatCompletion>(result.GetRawResponse().Content, wireFormat);
Console.WriteLine($"[ASSISTANT]: {completion}");
}
}

0 comments on commit 63e50ea

Please sign in to comment.