#Thread Appending
1 messages · Page 1 of 1 (latest)
Hi @plain comet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To append messages to a thread in OpenAI's Assistant API, you can use the client.beta.threads.messages.create method. Here's an example of how you can do it in Python:
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
In this example, thread_id is the ID of the thread you want to append a message to, role is the role of the message sender (it can be "user" or "assistant"), and content is the actual content of the message.
Once all the user messages have been added to the thread, you can run the thread with any assistant using the client.beta.threads.runs.stream method. Here's an example:
from typing_extensions import override
from openai import AssistantEventHandler
class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text) -> None:
print(f"\nassistant > ", end="", flush=True)
@override
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)
def on_tool_call_created(self, tool_call):
print(f"\nassistant > {tool_call.type}\n", flush=True)
def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
event_handler=EventHandler(),
) as stream:
stream.until_done()
In this example, thread_id is the ID of the thread you want to run, assistant_id is the ID of the assistant you want to use, instructions are the instructions for the assistant, and event_handler is an instance of a class that handles the events in the response stream.
You can find more information in the OpenAI's Assistant API Quickstart guide (https://platform.openai.com/docs/assistants/quickstart).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@plain comet kapa.ai is still learning and improving, please let us know how it did by reacting below
@grave raft You don't quite get me.
What i want to do is the following:
Create a python app, then when started it creates an empty thread first with a specific assistant, and waits for input from the user as input() from terminal. This input, will be the new message to be added to the thread, we get the response from the thread, and the user can send something new. If they type 'X', the conversation ends.
Let me check the knowledge base for you. One moment! :books:
Sure, I understand your requirement now. Here's a simple Python script that does what you're asking for. It creates a new thread, waits for user input, adds the user's input as a message to the thread, gets the assistant's response, and repeats this process until the user types 'X'.
from openai import OpenAI
import os
import time
# Replace with your OpenAI API key
OPENAI_API_KEY = "<your OpenAI API key>"
# Replace with your assistant's ID
ASSISTANT_ID = "<your assistant's ID>"
client = OpenAI(api_key=OPENAI_API_KEY)
def submit_message(assistant_id, thread, user_message):
client.beta.threads.messages.create(
thread_id=thread.id, role="user", content=user_message
)
return client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id,
)
def get_response(thread):
return client.beta.threads.messages.list(thread_id=thread.id, order="asc")
def wait_on_run(run, thread):
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.5)
return run
# Create a new thread
thread = client.beta.threads.create()
while True:
# Get user input
user_input = input("Enter your message (or 'X' to quit): ")
if user_input == 'X':
break
# Submit the user's message and start a new run
run = submit_message(ASSISTANT_ID, thread, user_input)
# Wait for the run to complete
run = wait_on_run(run, thread)
# Get the assistant's response
response = get_response(thread)
# Print the assistant's response
print("Assistant:", response.data[-1].content[0].text.value)
This script uses the OpenAI Python client to interact with the Assistant API. It creates a new thread, then enters a loop where it waits for user input, submits the user's message to the thread, waits for the assistant to generate a response, retrieves the assistant's response, and prints it. The loop continues until the user types 'X'.
Please replace <your OpenAI API key> and <your assistant's ID> with your actual OpenAI API key and your assistant's ID.