Skip to content

Commit e9ad2b3

Browse files
Merge pull request #272354 from aahill/streaming-api
[Assistants] adding streaming reference
2 parents 81868d5 + d5ec282 commit e9ad2b3

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
@@ -585,3 +585,115 @@ Represent a step in execution of a run.
585585
| `failed_at`| integer or null | The Unix timestamp (in seconds) for when the run step failed.|
586586
| `completed_at`| integer or null | The Unix timestamp (in seconds) for when the run step completed.|
587587
| `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.|
588+
589+
## Stream a run result (preview)
590+
591+
Stream the result of executing a Run or resuming a Run after submitting tool outputs. You can stream events after:
592+
* [Create Thread and Run](#create-thread-and-run)
593+
* [Create Run](#create-run)
594+
* [Submit Tool Outputs](#submit-tool-outputs-to-run)
595+
596+
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.
597+
598+
### Streaming example
599+
600+
```python
601+
from typing_extensions import override
602+
from openai import AssistantEventHandler
603+
604+
# First, we create a EventHandler class to define
605+
# how we want to handle the events in the response stream.
606+
607+
class EventHandler(AssistantEventHandler):
608+
@override
609+
def on_text_created(self, text) -> None:
610+
print(f"\nassistant > ", end="", flush=True)
611+
612+
@override
613+
def on_text_delta(self, delta, snapshot):
614+
print(delta.value, end="", flush=True)
615+
616+
def on_tool_call_created(self, tool_call):
617+
print(f"\nassistant > {tool_call.type}\n", flush=True)
618+
619+
def on_tool_call_delta(self, delta, snapshot):
620+
if delta.type == 'code_interpreter':
621+
if delta.code_interpreter.input:
622+
print(delta.code_interpreter.input, end="", flush=True)
623+
if delta.code_interpreter.outputs:
624+
print(f"\n\noutput >", flush=True)
625+
for output in delta.code_interpreter.outputs:
626+
if output.type == "logs":
627+
print(f"\n{output.logs}", flush=True)
628+
629+
# Then, we use the `create_and_stream` SDK helper
630+
# with the `EventHandler` class to create the Run
631+
# and stream the response.
632+
633+
with client.beta.threads.runs.stream(
634+
thread_id=thread.id,
635+
assistant_id=assistant.id,
636+
instructions="Please address the user as Jane Doe. The user has a premium account.",
637+
event_handler=EventHandler(),
638+
) as stream:
639+
stream.until_done()
640+
```
641+
642+
643+
## Message delta object
644+
645+
Represents a message delta. For example any changed fields on a message during streaming.
646+
647+
|Name | Type | Description |
648+
|--- |--- |--- |
649+
| `id` | string | The identifier of the message, which can be referenced in API endpoints. |
650+
| `object` | string | The object type, which is always `thread.message.delta`. |
651+
| `delta` | object | The delta containing the fields that have changed on the Message. |
652+
653+
## Run step delta object
654+
655+
Represents a run step delta. For example any changed fields on a run step during streaming.
656+
657+
|Name | Type | Description |
658+
|--- |--- |--- |
659+
| `id` | string | The identifier of the run step, which can be referenced in API endpoints. |
660+
| `object` | string | The object type, which is always `thread.run.step.delta`. |
661+
| `delta` | object | The delta containing the fields that have changed on the run step.
662+
663+
## Assistant stream events
664+
665+
Represents an event emitted when streaming a Run. Each event in a server-sent events stream has an event and data property:
666+
667+
```json
668+
event: thread.created
669+
data: {"id": "thread_123", "object": "thread", ...}
670+
```
671+
672+
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.
673+
674+
|Name | Type | Description |
675+
|--- |--- |--- |
676+
| `thread.created` | `data` is a thread. | Occurs when a new thread is created. |
677+
| `thread.run.created` | `data` is a run. | Occurs when a new run is created. |
678+
| `thread.run.queued` | `data` is a run. | Occurs when a run moves to a queued status. |
679+
| `thread.run.in_progress` | `data` is a run. | Occurs when a run moves to an in_progress status. |
680+
| `thread.run.requires_action` | `data` is a run. | Occurs when a run moves to a `requires_action` status. |
681+
| `thread.run.completed` | `data` is a run. | Occurs when a run is completed. |
682+
| `thread.run.failed` | `data` is a run. | Occurs when a run fails. |
683+
| `thread.run.cancelling` | `data` is a run. | Occurs when a run moves to a `cancelling` status. |
684+
| `thread.run.cancelled` | `data` is a run. | Occurs when a run is cancelled. |
685+
| `thread.run.expired` | `data` is a run. | Occurs when a run expires. |
686+
| `thread.run.step.created` | `data` is a run step. | Occurs when a run step is created. |
687+
| `thread.run.step.in_progress` | `data` is a run step. | Occurs when a run step moves to an `in_progress` state. |
688+
| `thread.run.step.delta` | `data` is a run step delta. | Occurs when parts of a run step are being streamed. |
689+
| `thread.run.step.completed` | `data` is a run step. | Occurs when a run step is completed. |
690+
| `thread.run.step.failed` | `data` is a run step. | Occurs when a run step fails. |
691+
| `thread.run.step.cancelled` | `data` is a run step. | Occurs when a run step is cancelled. |
692+
| `thread.run.step.expired` | `data` is a run step. | Occurs when a run step expires. |
693+
| `thread.message.created` | `data` is a message. | Occurs when a message is created. |
694+
| `thread.message.in_progress` | `data` is a message. | Occurs when a message moves to an in_progress state. |
695+
| `thread.message.delta` | `data` is a message delta. | Occurs when parts of a Message are being streamed. |
696+
| `thread.message.completed` | `data` is a message. | Occurs when a message is completed. |
697+
| `thread.message.incomplete` | `data` is a message. | Occurs when a message ends before it is completed. |
698+
| `error` | `data` is an error. | Occurs when an error occurs. This can happen due to an internal server error or a timeout. |
699+
| `done` | `data` is `[DONE]` | Occurs when a stream ends. |

0 commit comments

Comments
 (0)