Skip to content

Commit d5ec282

Browse files
committed
adding streaming reference
1 parent 9f6d0e0 commit d5ec282

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

articles/ai-services/openai/assistants-reference-runs.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to use Azure OpenAI's Python & REST API runs with Assista
55
manager: nitinme
66
ms.service: azure-ai-openai
77
ms.topic: conceptual
8-
ms.date: 02/01/2024
8+
ms.date: 04/16/2024
99
author: mrbullwinkle
1010
ms.author: mbullwin
1111
recommendations: false
@@ -583,3 +583,115 @@ Represent a step in execution of a run.
583583
| `failed_at`| integer or null | The Unix timestamp (in seconds) for when the run step failed.|
584584
| `completed_at`| integer or null | The Unix timestamp (in seconds) for when the run step completed.|
585585
| `metadata`| map | Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.|
586+
587+
## Stream a run result (preview)
588+
589+
Stream the result of executing a Run or resuming a Run after submitting tool outputs. You can stream events after:
590+
* [Create Thread and Run](#create-thread-and-run)
591+
* [Create Run](#create-run)
592+
* [Submit Tool Outputs](#submit-tool-outputs-to-run)
593+
594+
To stream a result, pass `"stream": true` while creating a run. The response will be a [Server-Sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) stream.
595+
596+
### Streaming example
597+
598+
```python
599+
from typing_extensions import override
600+
from openai import AssistantEventHandler
601+
602+
# First, we create a EventHandler class to define
603+
# how we want to handle the events in the response stream.
604+
605+
class EventHandler(AssistantEventHandler):
606+
@override
607+
def on_text_created(self, text) -> None:
608+
print(f"\nassistant > ", end="", flush=True)
609+
610+
@override
611+
def on_text_delta(self, delta, snapshot):
612+
print(delta.value, end="", flush=True)
613+
614+
def on_tool_call_created(self, tool_call):
615+
print(f"\nassistant > {tool_call.type}\n", flush=True)
616+
617+
def on_tool_call_delta(self, delta, snapshot):
618+
if delta.type == 'code_interpreter':
619+
if delta.code_interpreter.input:
620+
print(delta.code_interpreter.input, end="", flush=True)
621+
if delta.code_interpreter.outputs:
622+
print(f"\n\noutput >", flush=True)
623+
for output in delta.code_interpreter.outputs:
624+
if output.type == "logs":
625+
print(f"\n{output.logs}", flush=True)
626+
627+
# Then, we use the `create_and_stream` SDK helper
628+
# with the `EventHandler` class to create the Run
629+
# and stream the response.
630+
631+
with client.beta.threads.runs.stream(
632+
thread_id=thread.id,
633+
assistant_id=assistant.id,
634+
instructions="Please address the user as Jane Doe. The user has a premium account.",
635+
event_handler=EventHandler(),
636+
) as stream:
637+
stream.until_done()
638+
```
639+
640+
641+
## Message delta object
642+
643+
Represents a message delta. For example any changed fields on a message during streaming.
644+
645+
|Name | Type | Description |
646+
|--- |--- |--- |
647+
| `id` | string | The identifier of the message, which can be referenced in API endpoints. |
648+
| `object` | string | The object type, which is always `thread.message.delta`. |
649+
| `delta` | object | The delta containing the fields that have changed on the Message. |
650+
651+
## Run step delta object
652+
653+
Represents a run step delta. For example any changed fields on a run step during streaming.
654+
655+
|Name | Type | Description |
656+
|--- |--- |--- |
657+
| `id` | string | The identifier of the run step, which can be referenced in API endpoints. |
658+
| `object` | string | The object type, which is always `thread.run.step.delta`. |
659+
| `delta` | object | The delta containing the fields that have changed on the run step.
660+
661+
## Assistant stream events
662+
663+
Represents an event emitted when streaming a Run. Each event in a server-sent events stream has an event and data property:
664+
665+
```json
666+
event: thread.created
667+
data: {"id": "thread_123", "object": "thread", ...}
668+
```
669+
670+
Events are emitted whenever a new object is created, transitions to a new state, or is being streamed in parts (deltas). For example, `thread.run.created` is emitted when a new run is created, `thread.run.completed` when a run completes, and so on. When an Assistant chooses to create a message during a run, we emit a `thread.message.created` event, a `thread.message.in_progress` event, many thread.`message.delta` events, and finally a `thread.message.completed` event.
671+
672+
|Name | Type | Description |
673+
|--- |--- |--- |
674+
| `thread.created` | `data` is a thread. | Occurs when a new thread is created. |
675+
| `thread.run.created` | `data` is a run. | Occurs when a new run is created. |
676+
| `thread.run.queued` | `data` is a run. | Occurs when a run moves to a queued status. |
677+
| `thread.run.in_progress` | `data` is a run. | Occurs when a run moves to an in_progress status. |
678+
| `thread.run.requires_action` | `data` is a run. | Occurs when a run moves to a `requires_action` status. |
679+
| `thread.run.completed` | `data` is a run. | Occurs when a run is completed. |
680+
| `thread.run.failed` | `data` is a run. | Occurs when a run fails. |
681+
| `thread.run.cancelling` | `data` is a run. | Occurs when a run moves to a `cancelling` status. |
682+
| `thread.run.cancelled` | `data` is a run. | Occurs when a run is cancelled. |
683+
| `thread.run.expired` | `data` is a run. | Occurs when a run expires. |
684+
| `thread.run.step.created` | `data` is a run step. | Occurs when a run step is created. |
685+
| `thread.run.step.in_progress` | `data` is a run step. | Occurs when a run step moves to an `in_progress` state. |
686+
| `thread.run.step.delta` | `data` is a run step delta. | Occurs when parts of a run step are being streamed. |
687+
| `thread.run.step.completed` | `data` is a run step. | Occurs when a run step is completed. |
688+
| `thread.run.step.failed` | `data` is a run step. | Occurs when a run step fails. |
689+
| `thread.run.step.cancelled` | `data` is a run step. | Occurs when a run step is cancelled. |
690+
| `thread.run.step.expired` | `data` is a run step. | Occurs when a run step expires. |
691+
| `thread.message.created` | `data` is a message. | Occurs when a message is created. |
692+
| `thread.message.in_progress` | `data` is a message. | Occurs when a message moves to an in_progress state. |
693+
| `thread.message.delta` | `data` is a message delta. | Occurs when parts of a Message are being streamed. |
694+
| `thread.message.completed` | `data` is a message. | Occurs when a message is completed. |
695+
| `thread.message.incomplete` | `data` is a message. | Occurs when a message ends before it is completed. |
696+
| `error` | `data` is an error. | Occurs when an error occurs. This can happen due to an internal server error or a timeout. |
697+
| `done` | `data` is `[DONE]` | Occurs when a stream ends. |

0 commit comments

Comments
 (0)