Assistants

Solution Overview

OpenAI Assistants provide a powerful business solution for integrating AI-driven interactions into applications and workflows. By defining specific roles, instructions, and tools, businesses can create tailored AI assistants capable of handling customer support, data analysis, content generation, and more. With structured thread-based conversations and API-driven automation, Assistants streamline complex processes, enhance user engagement, and improve operational efficiency. Whether used for internal productivity or customer-facing interactions, they offer a scalable way to incorporate advanced AI capabilities into any business environment.


Main Entities in Assistants' Workflow

Here's a simple explanation of how Messages, Threads, Assistants, Runs, and Tools work in OpenAI's Assistants' workflow:

  • A Message is a single exchange in a conversation. It can be something a user sends to the Assistant (like a question or command) or something the Assistant replies with. Think of it as one text bubble in a chat.

  • A Thread is a collection of Messages that belong to the same conversation. It keeps all the back-and-forth exchanges together, so the Assistant can remember what was said earlier in that conversation. Each time a user starts a new discussion, it creates a new Thread. There is no limit to the number of Messages you can store in a Thread. When the size of the Messages exceeds the model's context window, the Thread will smartly truncate messages before fully dropping the least important ones.

  • An Assistant is the AI itselfβ€”its personality, skills, and behavior. When you set up an Assistant, you define what it knows, how it should respond, and whether it can use extra Tools (like APIs or file uploads). One Assistant can be used across multiple Threads and users.

  • A Run is the process that executes the Assistant’s response within a Thread. When a user sends a Message, a Run is created to generate the Assistant’s reply based on the conversation history and its predefined behavior. Runs manage the execution flow, including any tool calls the Assistant might need to make. A Run goes through different statusesβ€”such as queued, in_progress, completed, or failedβ€”indicating its current state. Each Thread can have multiple Runs, ensuring smooth and structured interactions between the user and the Assistant.

  • A Tool is an additional capability that an Assistant can use to enhance its responses. Tools allow the Assistant to perform external actions, such as calling APIs, retrieving files, or running custom functions. (You may have already encountered this concept by using Function Calling when calling text models without creating Assistants.) When a Tool is enabled, the Assistant can decide when to use it based on the conversation context. If a Tool is required during a Run, the process pauses until the necessary output is provided. This makes Tools essential for handling complex tasks that go beyond simple text-based interactions. Currently, three Tool options are available:

    • Code Interpreter, which can write or debug code for you in different programming languages,

    • File Search, which can analyze the file you provided and is capable of discussing its contents,

    • Function Calling, which can call your custom functions.


How to Use Assistant API

1

Create an Assistant. To set up an Assistant, you need to choose an AI model that will handle chat completion. The selected model determines the Assistant’s capabilities and response quality. Additionally, you should define the Assistant’s role and behavior by providing instructionsβ€”this will guide how it interacts with users. Additionally, you can add files to further train the Assistant on specific materials, enhancing its ability to provide more tailored responses. Enabling tools like Code Interpreter, File Search, and Function Calling can further improve its functionality. Once created, the Assistant will be assigned a unique ID, which you’ll use in further interactions. The method returns the ID of the created Assistant, which you can use later to link it with Threads and Runs.

2

Create a Thread where the interaction between the created Assistant and the user (a human) will take place. This method returns the ID of the created Thread.

3

Create the first user message either directly in the code or by passing it from a form. This message will be the first in your Thread. You must specify role: 'user' and the corresponding thread_id.

4

Create a Run to initiate the Chat Completion process for messages within the specified Thread using the specified Assistant. This is very similar to calling a model without using the Assistants and Threads framework. If external Tool calls might be needed during processing, you must predefine the available Tools in the tools parameter and set tool_choice to 'auto'.

Example #1: Chat With Assistant (Without Streaming)

Full Step-by-Step Explanation

This Python script implements a console-based chat using OpenAI's Assistant API, with gpt-4o as the default model. However, you can replace it with any OpenAI text model of your choice.

Unlike the streaming version, this version waits for the full response before displaying it. Below is a detailed breakdown of its execution.


1. Initialization Phase

Objects Created:

  1. OpenAI Client (client)

    • Connects to OpenAI's API using an AIMLAPI key and base URL.

  2. Assistant (my_assistant)

    • Created using client.beta.assistants.create().

    • Receives predefined instructions ("You are a helpful assistant.").

    • Assigned a model ("gpt-4o").

    • Generates an Assistant ID (assistant_id), which will be used to process Messages.

  3. Thread (thread)

    • Created using client.beta.threads.create().

    • The conversation takes place within this Thread, ensuring context persistence.

    • Generates a Thread ID (thread_id), which links all messages together.


2. Chat Loop Execution

The script enters a while loop, allowing continuous conversation:

  1. User Input (user_input)

    • The user types a Message (stored in user_input).

    • If the user types "exit" or "quit", the loop terminates.

  2. Message Sent to the Assistant (send_message(user_message))

    • If the Message is empty, a warning is displayed.

    • The Message is added to the Thread using:

  3. Assistant Processes the Message (run = client.beta.threads.runs.create_and_poll())

    • This function:

      • Starts processing the Message.

      • Waits until the Assistant completes its response.

      • Returns the Run status.

  4. Fetching the Assistant’s Response (client.beta.threads.messages.list())

    • If the Run status is "completed", the script retrieves all Messages from the Thread history.

    • The last Assistant Message is extracted and displayed:

  5. Error Handling

    • If something goes wrong, an error message is displayed:

  6. Loop Repeats Until User Exits


Summary of Identifiers Passed Between Functions

Identifier
Purpose
Where It's Used

assistant_id

Identifies the Assistant instance

Created during initialization, used in send_message()

thread_id

Links all Messages in the conversation

Created during initialization, used in send_message() and fetching responses

user_message

Stores user input

Passed from the chat loop to send_message()

run.status

Tracks response completion

Checked after create_and_poll()

message.content[0].text.value

Holds the Assistant's response

Extracted from messages.list()


Final Execution Flow Summary

  1. Initialize API client, Assistant, and Thread (IDs stored).

  2. Enter chat loop β†’ user types a Message.

  3. Send Message to Assistant (send_message()).

  4. Wait for Assistant to generate the full response.

  5. Fetch and display the response.

  6. Loop repeats until user types "exit".

πŸš€ This version provides a more structured, response-driven approach to interacting with the assistant.

Assistant Interaction Example

```` πŸ€– AI Assistant is ready! Type 'exit' to quit.

You > Hi! Could you write me a python function that squares the sum of two numbers?

Assistant > Certainly! Here's a simple Python function that squares the sum of two numbers:

This function takes two arguments, a and b, calculates their sum, and then returns the square of that sum. Π’Ρ‹ > Wow! Thanks a lot!

Assistant > You're welcome! If you have any more questions or need further assistance, feel free to ask. Happy coding!

You > exit

Assistant > πŸ‘‹ Chat session ended. See you next time!

Last updated

Was this helpful?