openai-node
49 строк · 1.3 Кб
1#!/usr/bin/env -S npm run tsn -T
2
3import OpenAI from 'openai';4
5/**
6* Example of polling for a complete response from an assistant
7*/
8
9const openai = new OpenAI();10
11async function main() {12const assistant = await openai.beta.assistants.create({13model: 'gpt-4-1106-preview',14name: 'Math Tutor',15instructions: 'You are a personal math tutor. Write and run code to answer math questions.',16// tools = [],17});18
19let assistantId = assistant.id;20console.log('Created Assistant with Id: ' + assistantId);21
22const thread = await openai.beta.threads.create({23messages: [24{25role: 'user',26content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"',27},28],29});30
31let threadId = thread.id;32console.log('Created thread with Id: ' + threadId);33
34const run = await openai.beta.threads.runs.createAndPoll(thread.id, {35assistant_id: assistantId,36additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.',37});38
39console.log('Run finished with status: ' + run.status);40
41if (run.status == 'completed') {42const messages = await openai.beta.threads.messages.list(thread.id);43for (const message of messages.getPaginatedItems()) {44console.log(message);45}46}47}
48
49main();50