File size: 2,262 Bytes
164e803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
_id: anthropic
description: Ask Claude agent
readme: The first base version
title: Anthropic
url: https://raw.githubusercontent.com/breslavsky/piper-packages/main/packages/anthropic.yaml
version: 1
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
    outputs:
      answer:
        title: en=Answer;ru=Ответ
        type: string
      json:
        title: JSON
        type: json
    package: anthropic
    script: |
      const Anthropic = require('@anthropic-ai/sdk');

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

      const client = new Anthropic({ apiKey: ANTHROPIC_API_KEY });

      (async () => {
         const { model, question } = inputs;

          const message = await client.messages.create({
              max_tokens: 1024,
              messages: [{ role: 'user', content: question }],
              model
          });

          const { content: [{type, text: answer}] } = message;
          log({type});
          
          switch(type) {
              case "text":
              default:
                  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 Claude;ru=Спросить Claude
    version: 1