OpenAI-API-dotnet icon indicating copy to clipboard operation
OpenAI-API-dotnet copied to clipboard

StreamResponseEnumerableFromChatbotAsync wrong role?

Open SemperFu opened this issue 2 years ago • 6 comments

/// Calls the API to get a response, which is appended to the current chat's <see cref="Messages"/> as an <see cref="ChatMessageRole.Assistant"/>

_chat.AppendUserInput(prompt);
await foreach (string responseChunk in _chat.StreamResponseEnumerableFromChatbotAsync())
{
    OutputText.Text += responseChunk;
}

Trying to use the following but when looking at the messages in chat I can see that the role for the responses is marked as User instead of Assistant.

image

SemperFu avatar Apr 19 '23 02:04 SemperFu

I have checked the source code Conversation.cs#L193 and there seems to be no problem.

Is it possible that the OpenAI APIf return value is wrong?

Tryanks avatar Apr 19 '23 10:04 Tryanks

same problem,in openai api stream result,only the first jsonpack has the role information.

NorKaiser avatar Apr 19 '23 11:04 NorKaiser

It also looks like there are no errors in the content returned by the OpenAI API :

  "choices": [
    {
      "delta": {
        "role": "assistant"
      },
      "finish_reason": null,
      "index": 0
    }
  ]

Then I don't know what the problem is.

Maybe the behavior of Conversation.cs#L193 can be hard-coded as Assistant?

@OkGoDoIt Can you take a look at this?

Tryanks avatar Apr 19 '23 11:04 Tryanks

I just found the causing reason, it will assign responseRole multiple times, first time it assignted to assistance, by next token response, it will reassigned to user although it's not response in API stream. Pull request to fix: https://github.com/OkGoDoIt/OpenAI-API-dotnet/pull/122

Workaround before official merge: Specify following code after every call to chat.StreamResponseEnumerableFromChatbotAsync():

chat.Messages[^1].Role = ChatMessageRole.Assistant;

example:

OpenAIAPI api = OpenAIAPI.ForAzure("*****", "*****", "******");
api.ApiVersion = "2023-03-15-preview";
Conversation chat = api.Chat.CreateConversation();

while (true)
{
	string? input = Console.ReadLine();
	if (input == "exit" || input == "q") break;


	chat.AppendUserInput(input);
	Console.WriteLine($"> {input}");

	await foreach (string res in chat.StreamResponseEnumerableFromChatbotAsync())
	{
		Console.Write(res);
	}
	Console.WriteLine();
	chat.Messages[^1].Role = ChatMessageRole.Assistant; // important
}

sdcb avatar Apr 20 '23 09:04 sdcb

by next token response, it will reassigned to user although it's not response in API stream.

Does this mean that the default value of delta.Role is not null but user? Does it depend on the API or class ChatMessage initialization?

Tryanks avatar Apr 20 '23 09:04 Tryanks

by next token response, it will reassigned to user although it's not response in API stream.

Does this mean that the default value of delta.Role is not null but user? Does it depend on the API or class ChatMessage initialization?

Yes it seems ChatMessage's default role is user instead of null: image

sdcb avatar Apr 20 '23 09:04 sdcb