How to create and connect ASSISTANTS ChatGPT API

Saenwaet
2 min readJul 11, 2024

--

Step 1 : Create Assistants for chatGPT.

Create instructions , name and model

Learn more https://platform.openai.com/playground/assistants

Step 2: Threads

Create Threads

const OpenAI = require("openai");

const openai = new OpenAI({
apiKey: "sk-proj-API-KEY"
});

async function main() {
const emptyThread = await openai.beta.threads.create();

console.log(emptyThread);
}

main();
Response: 

{
"id": "thread_abc123",
"object": "thread",
"created_at": 1699012949,
"metadata": {},
"tool_resources": {}
}

Step 3: Message

Create Message

async function main() {
const threadMessages = await openai.beta.threads.messages.create(
"thread_abc123",
{ role: "user", content: "How does AI work? Explain it in simple terms." }
);

console.log(threadMessages);
}

main();
Response:

{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1713226573,
"assistant_id": null,
"thread_id": "thread_abc123",
"run_id": null,
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "How does AI work? Explain it in simple terms.",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
}

Step 4: Run

Create a run

async function main() {
const run = await openai.beta.threads.runs.create(
"thread_abc123",
{ assistant_id: "asst_abc123" }
);

console.log(run);
}

main();
Respones:

{
"id": "run_abc123",
"object": "thread.run",
"created_at": 1699063290,
"assistant_id": "asst_abc123",
"thread_id": "thread_abc123",
"status": "queued",
"started_at": 1699063290,
"expires_at": null,
"cancelled_at": null,
"failed_at": null,
"completed_at": 1699063291,
"last_error": null,
"model": "gpt-4-turbo",
"instructions": null,
"incomplete_details": null,
"tools": [
{
"type": "code_interpreter"
}
],
"metadata": {},
"usage": null,
"temperature": 1.0,
"top_p": 1.0,
"max_prompt_tokens": 1000,
"max_completion_tokens": 1000,
"truncation_strategy": {
"type": "auto",
"last_messages": null
},
"response_format": "auto",
"tool_choice": "auto",
"parallel_tool_calls": true
}

Step 5: List messages

Returns a list of messages for a given thread.

async function main() {
const threadMessages = await openai.beta.threads.messages.list(
"thread_abc123"
);

console.log(threadMessages.data);
}

main();
Response:

{
"object": "list",
"data": [
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1699016383,
"assistant_id": null,
"thread_id": "thread_abc123",
"run_id": null,
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "How does AI work? Explain it in simple terms.",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
},
{
"id": "msg_abc456",
"object": "thread.message",
"created_at": 1699016383,
"assistant_id": null,
"thread_id": "thread_abc123",
"run_id": null,
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "Hello, what is AI?",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
}
],
"first_id": "msg_abc123",
"last_id": "msg_abc456",
"has_more": false
}

List messages FAQ and Anwer for javascript

async function main() {
const threadMessages = await openai.beta.threads.messages.list(
"thread_abc123"
);

threadMessages.data.map((m) => {
console.log(m.content)
})
}

main();

Good Luck !!!!

--

--