TechnologyMarch 19, 2024

The Astra Assistants API Now Supports Streaming: Because Who Wants to Wait?

Sebastian Estevez
Sebastian EstevezDataStax
The Astra Assistants API Now Supports Streaming: Because Who Wants to Wait?
python
from openai import OpenAI
from streaming_assistants import patch

client = patch(OpenAI())
…
python
print(f"creating run")
with client.beta.threads.runs.create_and_stream(
   thread_id=thread.id,
   assistant_id=assistant.id,
) as stream:
   for text in stream.text_deltas:
       print(text, end="", flush=True)
       print()
python
print(f"creating run")
with client.beta.threads.runs.create_and_stream(
   thread_id=thread.id,
   assistant_id=assistant.id,
) as stream:
   for event in stream:
       print(text, end="", flush=True)
       print()
python
class EventHandler(AssistantEventHandler):
   def __init__(self):
       super().__init__()

   @override
   def on_exception(self, exception: Exception):
       logger.error(exception)
       raise exception

   @override
   def on_tool_call_done(self, toolCall: ToolCall):
       logger.debug(toolCall)
       tool_outputs = []
       tool_outputs.append({"tool_call_id": toolCall.id, "output": "75 degrees F and sunny"}) # actually call out to your function here

       with client.beta.threads.runs.submit_tool_outputs_stream(
           thread_id=self.current_run.thread_id,
           run_id=self.current_run.id,
           tool_outputs=tool_outputs,
           event_handler=EventHandler(),
       ) as stream:
           #for part in stream:
           #    logger.info(part)
           for text in stream.text_deltas:
               print(text, end="", flush=True)
           print()

with client.beta.threads.runs.create_and_stream(
   thread_id=thread.id,
   assistant_id=assistant.id,
   event_handler=EventHandler()
) as stream:
   stream.until_done()
Discover more
Astra StreamingDataStax Astra DB
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.