File size: 2,142 Bytes
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
_id: openai
description: Includes Chat GPT
readme: The first base version
title: Open AI
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/openai.yaml
version: 1
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
    outputs:
      answer:
        title: en=Answer;ru=Ответ
        type: string
      json:
        title: JSON
        type: json
    package: openai
    script: |
      const OpenAI = require('openai');

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

      (async () => {
         const { model, question } = 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;

          try {
              const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
              return next({outputs: { json: JSON.parse(json) }});
          } catch(e) {
              log(e);
              // plain text
          }

          return next({outputs: { answer }});
      })();
    source: catalog
    title: en=Ask Chat GPT;ru=Спросить Chat GPT
    version: 1
    costs: |-
      (() => {
          return 0.001;
      })();