StreamResponseEnumerableFromChatbotAsync wrong role?
/// 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.

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?
same problem,in openai api stream result,only the first jsonpack has the role information.
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?
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
}
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?
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
nullbutuser? Does it depend on the API or classChatMessageinitialization?
Yes it seems ChatMessage's default role is user instead of null:
