| _id: openai |
| author: Anton Breslavskii | https://github.com/breslavsky |
| description: Includes Chat GPT |
| readme: The first base version |
| title: Open AI |
| url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/openai.yaml |
| version: 2 |
| nodes: |
| ask_chat_gpt: |
| _id: ask_chat_gpt |
| arrange: |
| x: 120 |
| y: 110 |
| 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 |
| default: What time is it now? Only JSON ready to parse. |
| 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 NextNode.from({ 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 |
|
|