Skip to content
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

Added sample showing how to do cancellations #48

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

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
Added sample showing how to do cancellations
  • Loading branch information
KrzysztofCwalina committed Jun 12, 2024
commit 77f03a339b77b529cf0f6087276c81d69d7714f9
30 changes: 30 additions & 0 deletions examples/Chat/Example01_SimpleChat_Cancellations.cs
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This BinaryContent contains the JSON for one message:

{
    "role": "user",
    "content": "Say \u0027this is a test.\u0027"
}

This will result in a 400 error.

The appropriate format should be:

{
    "model": "gpt-4o",
    "messages": [
        {
            "role": "user",
            "content": "Say \u0027this is a test.\u0027"
        }
    ]
}

Copy link
Collaborator Author

@KrzysztofCwalina KrzysztofCwalina Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I forgot about this. Now I get the context of your question about models not corresponding 1-1 to the payloads returned from protocol methods. I also don't think that we can get to 100% of 1-1 mappings, unless we introduce the concept of "protocol models", which i am not sure is worth it.


ChatCompletion completion = ModelReaderWriter.Read<ChatCompletion>(result.GetRawResponse().Content, wireFormat);
Console.WriteLine($"[ASSISTANT]: {completion}");
}
}
Loading