| _id: anthropic | |
| description: Ask Claude agent | |
| readme: The first base version | |
| title: Anthropic | |
| url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/anthropic.yaml | |
| version: 2 | |
| nodes: | |
| ask_claude: | |
| _id: ask_claude | |
| arrange: | |
| x: 150 | |
| y: 100 | |
| category: | |
| _id: llm_agents | |
| title: en=Language Agents;ru=Языковые агенты | |
| environment: | |
| ANTHROPIC_API_KEY: | |
| title: Anthropic 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: claude-3-5-sonnet-latest | |
| enum: | |
| - claude-3-5-sonnet-latest|Claude 3.5 Sonnet | |
| 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: anthropic | |
| script: | | |
| export async function costs({ inputs }) { | |
| return 0.01; | |
| } | |
| export async function run({ inputs }) { | |
| const { FatalError, NextNode } = DEFINITIONS; | |
| const Anthropic = require('@anthropic-ai/sdk'); | |
| const ANTHROPIC_API_KEY = env?.variables?.get('ANTHROPIC_API_KEY'); | |
| if (!ANTHROPIC_API_KEY) { | |
| throw new FatalError('Please, set your API key for Anthropic'); | |
| } | |
| const client = new Anthropic({ apiKey: ANTHROPIC_API_KEY }); | |
| const { model, question, answerFormat } = inputs; | |
| const message = await client.messages.create({ | |
| max_tokens: 1024, | |
| messages: [{ role: 'user', content: question }], | |
| model | |
| }); | |
| const { content: [{ type, text: answer }] } = message; | |
| switch (answerFormat) { | |
| case 'text': | |
| return NextNode.from({ outputs: { answer }, costs: await costs({ inputs }) }); | |
| case 'json': | |
| try { | |
| const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, ''); | |
| return NextNode.from({ outputs: { json: JSON.parse(json) }, costs: await costs({ inputs }) }); | |
| } 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 Claude;ru=Спросить Claude | |
| version: 1 | |