openai-node
41 строка · 1.0 Кб
1#!/usr/bin/env -S npm run tsn -T
2
3import OpenAI from 'openai';4
5const openai = new OpenAI();6
7async function main() {8const assistant = await openai.beta.assistants.create({9model: 'gpt-4-1106-preview',10name: 'Math Tutor',11instructions: 'You are a personal math tutor. Write and run code to answer math questions.',12});13
14const thread = await openai.beta.threads.create({15messages: [16{17role: 'user',18content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"',19},20],21});22
23const stream = await openai.beta.threads.runs.create(thread.id, {24assistant_id: assistant.id,25additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.',26stream: true,27});28
29for await (const event of stream) {30if (event.event === 'thread.message.delta') {31const chunk = event.data.delta.content?.[0];32if (chunk && 'text' in chunk && chunk.text.value) {33process.stdout.write(chunk.text.value);34}35}36}37
38console.log();39}
40
41main();42