mypiper commited on
Commit
223d0d4
·
verified ·
1 Parent(s): ad8e561

Update openai.yaml

Browse files
Files changed (1) hide show
  1. openai.yaml +39 -19
openai.yaml CHANGED
@@ -1,9 +1,10 @@
1
  _id: openai
 
2
  description: Includes Chat GPT
 
3
  readme: The first base version
4
- title: Open AI
5
  url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/openai.yaml
6
- version: 1
7
  nodes:
8
  ask_chat_gpt:
9
  _id: ask_chat_gpt
@@ -34,6 +35,16 @@ nodes:
34
  enum:
35
  - gpt-4o-mini|4o mini
36
  - gpt-4o|4o
 
 
 
 
 
 
 
 
 
 
37
  outputs:
38
  answer:
39
  title: en=Answer;ru=Ответ
@@ -43,15 +54,18 @@ nodes:
43
  type: json
44
  package: openai
45
  script: |
46
- const OpenAI = require('openai');
47
 
48
- const OPEN_API_KEY = env?.variables?.get('OPEN_API_KEY');
49
- if(!OPEN_API_KEY) {
50
- error.fatal('Please, set your API key for Open AI');
51
- }
 
 
 
 
52
 
53
- (async () => {
54
- const { model, question } = inputs;
55
 
56
  const openai = new OpenAI({
57
  apiKey: OPEN_API_KEY,
@@ -61,22 +75,28 @@ nodes:
61
  model: model,
62
  store: true,
63
  messages: [
64
- {"role": "user", "content": question},
65
  ],
66
  });
67
 
68
  const { content: answer } = result.choices[0].message;
69
 
70
- try {
71
- const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
72
- return next({outputs: { json: JSON.parse(json) }});
73
- } catch(e) {
74
- log(e);
75
- // plain text
 
 
 
 
 
 
 
 
76
  }
77
-
78
- return next({outputs: { answer }});
79
- })();
80
  source: catalog
81
  title: en=Ask Chat GPT;ru=Спросить Chat GPT
82
  version: 1
 
1
  _id: openai
2
+ title: Open AI
3
  description: Includes Chat GPT
4
+ version: 1
5
  readme: The first base version
 
6
  url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/openai.yaml
7
+ author: Anton Breslavskii | https://github.com/breslavsky
8
  nodes:
9
  ask_chat_gpt:
10
  _id: ask_chat_gpt
 
35
  enum:
36
  - gpt-4o-mini|4o mini
37
  - gpt-4o|4o
38
+ answerFormat:
39
+ order: 3
40
+ title: en=Answer format;ru=Формат ответа
41
+ description: Don't forget add instructions for LLM
42
+ type: string
43
+ required: true
44
+ default: text
45
+ enum:
46
+ - text|Text
47
+ - json|JSON
48
  outputs:
49
  answer:
50
  title: en=Answer;ru=Ответ
 
54
  type: json
55
  package: openai
56
  script: |
57
+ export async function run({ inputs }) {
58
 
59
+ const { FatalError, NextNode } = DEFINITIONS;
60
+
61
+ const OpenAI = require('openai');
62
+
63
+ const OPEN_API_KEY = env?.variables?.get('OPEN_API_KEY');
64
+ if (!OPEN_API_KEY) {
65
+ throw new FatalError('Please, set your API key for Open AI');
66
+ }
67
 
68
+ const { question, model, answerFormat } = inputs;
 
69
 
70
  const openai = new OpenAI({
71
  apiKey: OPEN_API_KEY,
 
75
  model: model,
76
  store: true,
77
  messages: [
78
+ { "role": "user", "content": question },
79
  ],
80
  });
81
 
82
  const { content: answer } = result.choices[0].message;
83
 
84
+ switch (answerFormat) {
85
+ case 'text':
86
+ return next({ outputs: { answer } });
87
+ case 'json':
88
+ try {
89
+ const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
90
+ return NextNode.from({ outputs: { json: JSON.parse(json) } });
91
+ } catch (e) {
92
+ console.log(e);
93
+ message(`Wrong JSON for question \`\`\`text\n${question}\n\`\`\`\nnanswer from LLM\n\`\`\`text${answer}\n\`\`\``, 'defect');
94
+ throw new FatalError("Can't parse JSON asnwer from LLM");
95
+ }
96
+ default:
97
+ throw new FatalError(`Wrong answer format ${answerFormat}`);
98
  }
99
+ }
 
 
100
  source: catalog
101
  title: en=Ask Chat GPT;ru=Спросить Chat GPT
102
  version: 1