-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample showing how to do cancellations
- Loading branch information
1 parent
e6fe89c
commit 77f03a3
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
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,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<ChatCompletion>(result.GetRawResponse().Content, wireFormat); | ||
Console.WriteLine($"[ASSISTANT]: {completion}"); | ||
} | ||
} |