Question about: Streaming response text with the 8.0.1 streaming feature for Assistants #327
-
Hi, I've been trying to get the highly anticipated streaming feature for the assistant thread to work, but for some reason I can't get it to actually stream the response text. The 'Create thread and run streaming' sample ends with this code: var messages = await thread.ListMessagesAsync();
foreach (var response in messages.Items.Reverse())
{
Console.WriteLine($"{response.Role}: {response.PrintContent()}");
} which hints that the response is not actually streamed but needs to be retrieved with a separate request. (which doesn't seem to be necessary when that last MessageResponse was captured) The OpenAI doc 'context with streaming', especially 'Step 4' seem to hint that 'delta.value' is available for the on_text_delta in the AssistantEventHandler, which makes me think that response streaming is supported. Are my expectations way off? Or am I misunderstanding the streaming feature, or am I doing something wrong? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 29 replies
-
Hi @StephenHodgson , var i = 0;
using var api = new OpenAIClient(new OpenAIAuthentication(OpenAI_key, OpenAI_org));
var assistantRequest = new CreateAssistantRequest(instructions: "You are a helpful assistant.");
var assistant = await api.AssistantsEndpoint.CreateAssistantAsync(assistantRequest);
ThreadResponse thread = null;
async void StreamEventHandler(IServerSentEvent streamEvent)
{
switch (streamEvent)
{
case ThreadResponse threadResponse:
thread = threadResponse;
break;
case RunResponse runResponse:
break;
case MessageResponse messageResponse:
Console.WriteLine($"{i++} {messageResponse.Status}: {messageResponse.PrintContent()}");
break;
default:
Console.WriteLine(streamEvent.ToJsonString());
break;
}
}
var run = await assistant.CreateThreadAndRunAsync("Who was Mozart?", StreamEventHandler);
run = await run.WaitForStatusChangeAsync(); When I run this code I get this output: {"id":"step_E6EZvvZLoSB","object":"thread.run.step","created_at":1718292737,"assistant_id":"asst_PimbMdHzENgoXAD","thread_id":"thread_DfIhiyygVf7nC5wG7NUaQrDH","run_id":"run_0B5uoe","type":"message_creation","status":"in_progress","step_details":{"message_creation":{"message_id":"msg_4w8HUPJBOsK"}}}
{"id":"step_E6EZvvZLoSB","object":"thread.run.step","created_at":1718292737,"assistant_id":"asst_PimbXHzENgoXAD","thread_id":"thread_DfIhiyygVf7nC5wG7NUaQrDH","run_id":"run_0B5uokZaZvve","type":"message_creation","status":"in_progress","step_details":{"message_creation":{"message_id":"msg_4w8HULFOsK"}}}
0 InProgress:
1 InProgress:
2 InProgress:
3 InProgress:
...
269 InProgress:
270 InProgress:
271 InProgress:
272 Completed: Wolfgang Amadeus Mozart was an influential and prolific composer of the Classical period.
...
His influence on Western music cannot be overstated, and he is often cited as one of the greatest composers in the history of Western music.
{"id":"step_E6EZvkj8oLoSB","object":"thread.run.step","created_at":1718292737,"assistant_id":"asst_PimbX2HzEgoXAD","thread_id":"thread_DfIhiyygVUaQrDH","run_id":"run_0B5uokZvcZvve","type":"message_creation","status":"completed","step_details":{"message_creation":{"message_id":"msg_4w8HULFABpOsK"}},"completed_at":1718292742,"usage":{"prompt_tokens":34,"completion_tokens":272,"total_tokens":306}} MessageResponse 272 Shows the Completed status and contains the full text response. |
Beta Was this translation helpful? Give feedback.
-
Firstly I'd like to thank you for putting so much effort in answering my question and I'm a big fan of OpenAI-DotNet and the API it provides. What I see in your log file is exactly the same as in mine: the MessageResponse events (that should probably contain the delta's) are empty until the last one (Status: Completed) which contains the entire response. |
Beta Was this translation helpful? Give feedback.
-
Hi @StephenHodgson , case "thread.message.incomplete":
var partialMessage = sseResponse.Deserialize<MessageResponse>(ssEvent, client);
if (message == null)
{
message = partialMessage;
}
else
{
message.AppendFrom(partialMessage);
}
481 streamEventHandler?.Invoke(partialMessage);
return; Line 481 originally contains: Next, I can print the delta's in my test project like this: case MessageResponse messageResponse:
if (messageResponse.Delta != null)
{
Console.Write($"{messageResponse.Delta.Content.FirstOrDefault()?.Text}");
}
break; Running the test project will show the familiar chat-gpt delta streaming I was trying to accomplish. |
Beta Was this translation helpful? Give feedback.
@rkuiper1 I think I fixed it in
Try it out and let me know. The
Delta
object should show be properly populated.