openai-node

Форк
0
/
assistants.ts 
49 строк · 1.3 Кб
1
#!/usr/bin/env -S npm run tsn -T
2

3
import OpenAI from 'openai';
4

5
/**
6
 * Example of polling for a complete response from an assistant
7
 */
8

9
const openai = new OpenAI();
10

11
async function main() {
12
  const assistant = await openai.beta.assistants.create({
13
    model: 'gpt-4-1106-preview',
14
    name: 'Math Tutor',
15
    instructions: 'You are a personal math tutor. Write and run code to answer math questions.',
16
    // tools = [],
17
  });
18

19
  let assistantId = assistant.id;
20
  console.log('Created Assistant with Id: ' + assistantId);
21

22
  const thread = await openai.beta.threads.create({
23
    messages: [
24
      {
25
        role: 'user',
26
        content: '"I need to solve the equation `3x + 11 = 14`. Can you help me?"',
27
      },
28
    ],
29
  });
30

31
  let threadId = thread.id;
32
  console.log('Created thread with Id: ' + threadId);
33

34
  const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
35
    assistant_id: assistantId,
36
    additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.',
37
  });
38

39
  console.log('Run finished with status: ' + run.status);
40

41
  if (run.status == 'completed') {
42
    const messages = await openai.beta.threads.messages.list(thread.id);
43
    for (const message of messages.getPaginatedItems()) {
44
      console.log(message);
45
    }
46
  }
47
}
48

49
main();
50

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

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

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

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