Using Assistants & Threads
Example of using the threads API
You can find the full code here.
The Threads API is very useful for creating a chat-like experience with a relatively large message history.
Usually, this task is accomplished by using the chat completion API, but it requires managing the state of the message history, which can't be done in all contexts. For example, in a serverless environment, you can't store the state, so you need to find another way to manage the state.
The Threads API does this state management for you, providing streaming and polling features out-of-the-box. You can use the Threads API using raw HTTP requests, but a simpler way is to use the Openal SDK.
Example
First, you need to import the OpenAI SDK:
Next, create your assistant:
And create a thread and write a user message in it:
This code will return a model answer based on your prompt and keep the thread with message history on the server, transferring all message state management from your code to the API.
Last updated