OkGoDoIt / OpenAI-API-dotnet

An unofficial C#/.NET SDK for accessing the OpenAI GPT-3 API
https://www.nuget.org/packages/OpenAI/
Other
1.86k stars 430 forks source link

StreamChatEnumerableAsync returns null #128

Open w1tsky opened 1 year ago

w1tsky commented 1 year ago

Hi I have such simple chat code where token in await foreach loop is equal null:

using OpenAI_API.Chat;
using OpenAI_API.Completions;
using OpenAI_API.Models;

internal class Program
{
    private static async Task Main(string[] args)
    {
        OpenAi _openAi = new OpenAi("apikey...");

        List<ChatMessage> chatMessages = new List<ChatMessage>();

        Console.WriteLine("Ask a question:");

        var message = Console.ReadLine();

        chatMessages.Add(new ChatMessage(ChatMessageRole.User, message));

        ChatRequest request = new ChatRequest()
        {
            Model = Model.ChatGPTTurbo,
            Temperature = 0.1,
            MaxTokens = 500,
            Messages = chatMessages
        };

        Console.WriteLine("Answer: ");

        await foreach (var token in _openAi.Chat.StreamChatEnumerableAsync(request))
        {
            Console.Write(token);
        }
    }
}

image

How can I fix this?

Marilyth commented 1 year ago

This is an error with the ChatChoice.ToString method for streamed responses, which you are calling implicitly with Console.Write(token).

The response of a streamed ChatChoice is in Delta, not in Message, as is falsely done here https://github.com/OkGoDoIt/OpenAI-API-dotnet/blob/8bc534bc0163e4ca0ddad21a6d58a57910dc6189/OpenAI_API/Chat/ChatResult.cs#L80

An easy workaround would be to write Console.Write(token.Choices.First().Delta.Content) instead.