How to send my prompt to my assistant?

Im a bit confused with the documentation regarding the API client and how to use it in order to send message to my existing assistant.

I have created an assistant with assistant_id, and already have added the required instructions, now i need to send my content or message as user, how to do so

 client = OpenAI(api_key=os.getenv("OPENAI_API_KEY", key))

completion = client.beta.threads.messages.create(
            assistant_id="asst_ze4dxsdsExIB9",
            role= "user",
            content= f"{json_data}"


        )

Hi!
You can find a complete example in the OpenAI cookbook: Assistants API Overview (Python SDK

If you have more questions, feel free to ask!

Great!.

Thank you so much will check it out and get back to you if i have further questions

Seems working now.

But i was wondering if there is an approach where i can get the entire response all at once similar to the same approach being used with text completion

        completion = client.chat.completions.create(
            model="gpt-4-turbo",
            messages=[{"role": "user", "content": prompt}]
        )
        if completion.choices:
            response_content = completion.choices[0].message.content
            return response_content
        else:
            return "No response was returned by the model."

Currently im getting multiple lines of responses

def submit_message(user_message):
    print(thread.id)
    print(assistant_id)
    message_response = client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    message_id = message_response.id
    print(f"message_id: {message_id}")
    # Create the run
    client.beta.threads.messages.list(
        thread_id=thread.id, order="asc", after=message_id
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id="asst_ze4dxOglVj95dxIB9"
    )

The message object has a content array and all messages are stored as list on a thread. You can retrieve a message object like this:

message = client.beta.threads.messages.retrieve(
  thread_id="...",
  message_id="..."
)

Yes but seems the output bieng streamed, when i should be able to call this function ?

After adding the messages to the thread you run it with your assistant.
The assistant while then add ’ assistant messages to the thread. You can use the ‘AssistantEventHandler’ class as demonstrated here to stream the response.

As you are just getting started you can opt to poll for updates as in the second example (there is a little toggle for with and without streaming).

1 Like