Skip to content

StreamResponseEnumerableFromChatbotAsync wrong role? #119

Open
@SemperFu

Description

@SemperFu
/// 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

Activity

Tryanks

Tryanks commented on Apr 19, 2023

@Tryanks

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?

NorKaiser

NorKaiser commented on Apr 19, 2023

@NorKaiser

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

Tryanks

Tryanks commented on Apr 19, 2023

@Tryanks

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?

added a commit that references this issue on Apr 20, 2023
975149c
sdcb

sdcb commented on Apr 20, 2023

@sdcb

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: #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
}
Tryanks

Tryanks commented on Apr 20, 2023

@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?

sdcb

sdcb commented on Apr 20, 2023

@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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @sdcb@SemperFu@NorKaiser@Tryanks

      Issue actions

        StreamResponseEnumerableFromChatbotAsync wrong role? · Issue #119 · OkGoDoIt/OpenAI-API-dotnet