Hi,
how do I make an api call to an assistant V2 that has the streaming option in python.
eg
Send message ‘myText’ to assistant ID ‘xyz111’
Print response.
Anyone have a basic example to get started with.
Hi,
how do I make an api call to an assistant V2 that has the streaming option in python.
eg
Send message ‘myText’ to assistant ID ‘xyz111’
Print response.
Anyone have a basic example to get started with.
OpenAI documentation has a basic example.
https://platform.openai.com/docs/assistants/overview/step-4-create-a-run?context=with-streaming
Thanks for the info.
I have the following working (using ChatGPT) but the result is only
Thread created successfully: thread_Nd7CbZrvFtDptcZoQsMqSfZA
Message sent successfully: msg_dS6Sgz0321dfOPmcnBhpiZLP
Message from user: [TextContentBlock(text=Text(annotations=[], value='I need to solve the equation `3x + 11 = 14`. Can you help me?'), type='text')]
[Finished in 11.9s]type or paste code here
Code:
from openai import OpenAI
# Initialize the OpenAI client
client = OpenAI()
# Existing assistant ID
assistant_id = "MyAssisID"
# Create a new thread for interaction using the existing assistant
try:
thread = client.beta.threads.create()
print("Thread created successfully:", thread.id)
except Exception as e:
print("Failed to create thread:", e)
# Function to send a message using the existing assistant and the created thread
def send_message(thread_id, content):
try:
message = client.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=content
)
print("Message sent successfully:", message.id)
return message.id
except Exception as e:
print("Failed to send message:", e)
return None
# Function to fetch and display all messages from the thread, with detailed logging
def fetch_messages(thread_id):
try:
messages = client.beta.threads.messages.list(thread_id=thread_id)
if messages.data:
for message in messages.data:
print(f"Message from {message.role}: {message.content}")
else:
print("No data in messages response.")
except Exception as e:
print("Failed to fetch messages:", e)
# Example usage: send a message to the thread and fetch responses
message_id = send_message(thread.id, "I need to solve the equation `3x + 11 = 14`. Can you help me?")
if message_id:
# Wait a bit longer for the assistant to respond
import time
time.sleep(10) # Increase sleep time if necessary
fetch_messages(thread.id)
What piece of the jigsaw am I missing please?
Finally got it working. If anyone needs this
import os
from openai import OpenAI
# Initialize the OpenAI client
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')# stored If not add your key
# Specify the ID of the existing assistant
existing_assistant_id = "asst_myID"
# Step 1: Retrieve the Existing Assistant
existing_assistant = client.beta.assistants.retrieve(existing_assistant_id)
print(f"This is the existing assistant object: {existing_assistant} \n")
# Step 2: Create a Thread
my_thread = client.beta.threads.create()
print(f"This is the thread object: {my_thread} \n")
# Step 3: Add a Message to a Thread
my_thread_message = client.beta.threads.messages.create(
thread_id=my_thread.id,
role="user",
content="Can we meet up ",
)
print(f"This is the message object: {my_thread_message} \n")
# Step 4: Run the Assistant
my_run = client.beta.threads.runs.create(
thread_id=my_thread.id,
assistant_id=existing_assistant_id,
instructions=existing_assistant.instructions
)
print(f"This is the run object: {my_run} \n")
# Step 5: Periodically retrieve the Run to check on its status to see if it has moved to completed
while my_run.status in ["queued", "in_progress"]:
keep_retrieving_run = client.beta.threads.runs.retrieve(
thread_id=my_thread.id,
run_id=my_run.id
)
print(f"Run status: {keep_retrieving_run.status}")
if keep_retrieving_run.status == "completed":
print("\n")
# Step 6: Retrieve the Messages added by the Assistant to the Thread
all_messages = client.beta.threads.messages.list(
thread_id=my_thread.id
)
print("------------------------------------------------------------ \n")
print(f"User: {my_thread_message.content[0].text.value}")
print(f"Assistant: {all_messages.data[0].content[0].text.value}")
break
elif keep_retrieving_run.status == "queued" or keep_retrieving_run.status == "in_progress":
pass
else:
print(f"Run status: {keep_retrieving_run.status}")
break
The stream does not come from listing messages. It comes from the run.
You never execute a run in the initial code. The code you just pasted does not stream.
What is missing is that you need an iterator “extractor” on the generator Python object in the return. A “for” loop. Collecting the chunks as they are received, extracting and displaying those delta objects with response to the user, or watching for tools. And a display that can be updated by displaying the new text or the appended text so far.
The linked example uses an event handler and the python module’s events openai-python/helpers.md at main · openai/openai-python · GitHub
The API dox show the type of chunks you get out, the event types that precede data:
https://platform.openai.com/docs/api-reference/assistants-streaming/events
Thank you so much for posting this, @parkforce! I was going in circles and this put me back on track.
i’m trying to create an interactive script. I see no examples for this on the openai doc site. what i mean by interactive is a script where the user chats with the assistant to do things like file searching and manipulation.