openai-node

Форк
0
/
assistant-stream-raw.ts 
41 строка · 1.0 Кб
1
#!/usr/bin/env -S npm run tsn -T
2

3
import OpenAI from 'openai';
4

5
const openai = new OpenAI();
6

7
async function main() {
8
  const assistant = await openai.beta.assistants.create({
9
    model: 'gpt-4-1106-preview',
10
    name: 'Math Tutor',
11
    instructions: 'You are a personal math tutor. Write and run code to answer math questions.',
12
  });
13

14
  const thread = await openai.beta.threads.create({
15
    messages: [
16
      {
17
        role: 'user',
18
        content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"',
19
      },
20
    ],
21
  });
22

23
  const stream = await openai.beta.threads.runs.create(thread.id, {
24
    assistant_id: assistant.id,
25
    additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.',
26
    stream: true,
27
  });
28

29
  for await (const event of stream) {
30
    if (event.event === 'thread.message.delta') {
31
      const chunk = event.data.delta.content?.[0];
32
      if (chunk && 'text' in chunk && chunk.text.value) {
33
        process.stdout.write(chunk.text.value);
34
      }
35
    }
36
  }
37

38
  console.log();
39
}
40

41
main();
42

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.