File size: 3,085 Bytes
1050f6e
223d0d4
1050f6e
223d0d4
1050f6e
 
223d0d4
1050f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223d0d4
 
 
 
 
 
 
 
 
 
1050f6e
 
 
 
 
 
 
 
 
223d0d4
1050f6e
223d0d4
 
 
 
 
 
 
 
1050f6e
223d0d4
1050f6e
 
 
 
 
 
 
 
 
223d0d4
1050f6e
 
 
 
 
223d0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
1050f6e
223d0d4
1050f6e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
_id: openai
title: Open AI
description: Includes Chat GPT
version: 1
readme: The first base version
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/openai.yaml
author: Anton Breslavskii | https://github.com/breslavsky
nodes:
  ask_chat_gpt:
    _id: ask_chat_gpt
    arrange:
      x: 220
      y: 140
    category:
      id: llm_agents
      title: en=Language Agents;ru=Языковые агенты
    environment:
      OPEN_API_KEY:
        title: Open API key
        type: string
        scope: global
    inputs:
      question:
        order: 2
        title: en=Question;ru=Вопрос
        type: string
        required: true
        multiline: true
      model:
        order: 2
        title: en=Model;ru=Модель
        type: string
        required: true
        default: gpt-4o-mini
        enum:
          - gpt-4o-mini|4o mini
          - gpt-4o|4o
      answerFormat:
        order: 3
        title: en=Answer format;ru=Формат ответа
        description: Don't forget add instructions for LLM
        type: string
        required: true
        default: text
        enum:
          - text|Text
          - json|JSON
    outputs:
      answer:
        title: en=Answer;ru=Ответ
        type: string
      json:
        title: JSON
        type: json
    package: openai
    script: |
      export async function run({ inputs }) {

          const { FatalError, NextNode } = DEFINITIONS;

          const OpenAI = require('openai');

          const OPEN_API_KEY = env?.variables?.get('OPEN_API_KEY');
          if (!OPEN_API_KEY) {
              throw new FatalError('Please, set your API key for Open AI');
          }

          const { question, model, answerFormat } = inputs;

          const openai = new OpenAI({
              apiKey: OPEN_API_KEY,
          });

          const result = await openai.chat.completions.create({
              model: model,
              store: true,
              messages: [
                  { "role": "user", "content": question },
              ],
          });

          const { content: answer } = result.choices[0].message;

          switch (answerFormat) {
              case 'text':
                  return next({ outputs: { answer } });
              case 'json':
                  try {
                      const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
                      return NextNode.from({ outputs: { json: JSON.parse(json) } });
                  } catch (e) {
                      console.log(e);
                      message(`Wrong JSON for question \`\`\`text\n${question}\n\`\`\`\nnanswer from LLM\n\`\`\`text${answer}\n\`\`\``, 'defect');
                      throw new FatalError("Can't parse JSON asnwer from LLM");
                  }
              default:
                  throw new FatalError(`Wrong answer format ${answerFormat}`);
          }
      }
    source: catalog
    title: en=Ask Chat GPT;ru=Спросить Chat GPT
    version: 1
    costs: |-
      (() => {
          return 0.001;
      })();