File size: 3,161 Bytes
164e803
 
446d7e5
164e803
446d7e5
 
164e803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446d7e5
 
 
 
 
 
 
 
 
 
164e803
 
 
 
 
 
 
 
 
446d7e5
 
164e803
 
446d7e5
 
 
 
164e803
446d7e5
 
 
 
 
 
 
 
164e803
 
 
 
 
 
 
446d7e5
 
 
 
 
 
164e803
 
446d7e5
 
164e803
446d7e5
 
164e803
446d7e5
 
164e803
446d7e5
164e803
 
 
446d7e5
 
 
 
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
_id: anthropic
title: Anthropic
description: Ask Claude agent
version: 1
readme: The first base version
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/anthropic.yaml
nodes:
  ask_claude:
    _id: ask_claude
    arrange:
      x: 140
      y: 90
    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
      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.05;
      }

      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) {
                      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
    costs: |-
      (() => {
          return 0.001;
      })();