Site icon TestingDocs.com

OpenAI Assistant API

Overview

The OpenAI Assistants API allows developers to build and integrate AI assistants
into the application workflows. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user prompts. Note that Assistant API is in the beta phase at this particular time.

OpenAI Assistant API

Assistant API is an evolution of Completions API, now considered a legacy. The Assistants API supports the following tools:

This tutorial uses Python language and requires proper OpenAI Python SDK setup.

The best way to get the API flow is to interact with the Assistant using the
Platform Playground. Enable the logs to see the API calls executed
under the hood.

 

Pre-requisites

Things you need for this tutorial:

Assistants API Integration

The steps to follow when working with the Assistant API are as follows:

🤖 Create an AI Assistant

Steps to create an assistant using API can be found here:

https://www.testingdocs.com/create-assistant-api-request/

This is a POST call on the following API endpoint.

🔀 Create a Thread

👩🏻‍💻 Add User message to Thread

Users and assistants can add messages to the thread. Add User Message to a Thread. This simulates the user, prompting the AI assistant.

POST https://api.openai.com/v1/threads/{thread_id}/messages

🚀 Run the Assistant on the Thread

POST https://api.openai.com/v1/threads/{thread_id}/runs

🗒 Get Assistant Message on Thread

Poll the Run to check if the status is completed

GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}

Retrieve the response messages added by the Assistant to
the Thread

GET https://api.openai.com/v1/threads/{thread_id}/messages

 

💡 Code Example

Let’s write a simple code snippet to illustrate the concepts we have learned and a simple user prompt to solve an equation.

“””
Interact with Assistant API Calls
— Python OpenAI SDK
— OpenAI Tutorials — www.TestingDocs.com
“””

from openai import OpenAI
client = OpenAI()

# create a thread
thread = client.beta.threads.create()

# add a user message to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role=”user”,
    content=”Solve `5x + 14 = 109`. Find x?”
)
print(“User prompt:”, message.content[0].text.value)

# Run the assistant on the thread
run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
    instructions=””
)

# poll for Assistant message on the thread
while run.status == ‘queued’ or run.status == ‘in_progress’:
    run = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
        )
    if run.status == ‘completed’:
       messages = client.beta.threads.messages.list(
       thread_id=thread.id
        )
        print(“Assistant Response: “,messages.data[0].content[0].text.value)

💻 Output

Execute the code and check the model response. Thread can be in several states like:

 

 

That’s it. 👏  You have successfully interacted with the model using the API.

OpenAI API Tutorials

OpenAI tutorials on this website can be found at:

Exit mobile version