OpenAI's Assistants API
On November 6, 2023, OpenAI announced its new Assistants API. It’s still in beta but allows for more functionalities than the current API.
What is a Thread?
The new API introduces the concept of a thread. With the current API, one needs to send the entire conversation to maintain the AI’s context. Threads enable sending a session ID and managing message size by truncating it when it’s too long.
How to Access the Assistants API?
The “Assistants” API is available to anyone with an OpenAI account who has obtained their API key.
How Does the New Assistants API Work?
The new “Assistants” API works with an assistant and threads. After declaring an assistant, one creates a thread, adds one or more messages to it, then launches the thread, retrieves its response, and displays the message list.
assistants.create
method creates the assistant.threads.create
method initializes the thread.threads.message.create
method creates a message.threads.runs.create
method creates the process to launch the thread.threads.runs.retrieve
method retrieves the thread’s response.threads.messages.list
method retrieves the message list.
Python Example of the Assistants API
Let’s explore the API together with some code.
Ensure you update your openai version:
pip install --upgrade openai
Here’s a simple Python example to create an assistant that helps beginners learn to code.
from openai import OpenAI
client = OpenAI('your_token')
assistant = client.beta.assistants.create(
name="Code Assistant",
instructions="You are a code helper for beginners. Write, execute, and explain code to best answer questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-1106-preview"
)
thread = openai.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="How does recursion work? Give an example in Python."
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Explain as simply as possible, the user is a beginner."
)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
messages = openai.beta.threads.messages.list(
thread_id=thread.id
)
assistant_response = messages.data[0].content[0].text.value
print(assistant_response)
JavaScript Example of the Assistants API
With npm, ensure you update your openai
package if not done yet:
npm update openai
Here’s a boilerplate to start using the new Assistants API:
require("dotenv").config()
const OpenAI = require("openai")
const secretKey = process.env.OPENAI_API_KEY
const openai = new OpenAI({
apiKey: secretKey,
})
const assistant = await openai.beta.assistants.create({
name: "Code Assistant",
instructions:
"You are a code helper for beginners. Write, execute, and explain code to best answer questions.",
tools: [{ type: "code_interpreter" }],
model: "gpt-4-1106-preview",
})
const thread = await openai.beta.threads.create()
const message = await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: "How does recursion work? Give an example in Python.",
})
const run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistant.id,
instructions: "Explain as simply as possible, the user is a beginner.",
})
const run = await openai.beta.threads.runs.retrieve(thread.id, run.id)
const messages = await openai.beta.threads.messages.list(thread.id)
Hey, I'm Thomas 👋 Here, you'll find articles about tech, and what I think about the world. I've been coding for 20+ years, built many projects including Startups. Check my About/Start here page to know more :).