mypiper commited on
Commit
164e803
·
verified ·
1 Parent(s): a269b97

Add anthropic nodes

Browse files
Files changed (1) hide show
  1. anthropic.yaml +81 -0
anthropic.yaml ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ _id: anthropic
2
+ description: Ask Claude agent
3
+ readme: The first base version
4
+ title: Anthropic
5
+ url: https://raw.githubusercontent.com/breslavsky/piper-packages/main/packages/anthropic.yaml
6
+ version: 1
7
+ nodes:
8
+ ask_claude:
9
+ _id: ask_claude
10
+ arrange:
11
+ x: 140
12
+ y: 90
13
+ category:
14
+ id: llm_agents
15
+ title: en=Language Agents;ru=Языковые агенты
16
+ environment:
17
+ ANTHROPIC_API_KEY:
18
+ title: Anthropic API key
19
+ type: string
20
+ scope: global
21
+ inputs:
22
+ question:
23
+ order: 2
24
+ title: en=Question;ru=Вопрос
25
+ type: string
26
+ required: true
27
+ multiline: true
28
+ model:
29
+ order: 2
30
+ title: en=Model;ru=Модель
31
+ type: string
32
+ required: true
33
+ default: claude-3-5-sonnet-latest
34
+ enum:
35
+ - claude-3-5-sonnet-latest|Claude 3.5 Sonnet
36
+ outputs:
37
+ answer:
38
+ title: en=Answer;ru=Ответ
39
+ type: string
40
+ json:
41
+ title: JSON
42
+ type: json
43
+ package: anthropic
44
+ script: |
45
+ const Anthropic = require('@anthropic-ai/sdk');
46
+
47
+ const ANTHROPIC_API_KEY = env?.variables?.get('ANTHROPIC_API_KEY');
48
+ if(!ANTHROPIC_API_KEY) {
49
+ error.fatal('Please, set your API key for Anthropic');
50
+ }
51
+
52
+ const client = new Anthropic({ apiKey: ANTHROPIC_API_KEY });
53
+
54
+ (async () => {
55
+ const { model, question } = inputs;
56
+
57
+ const message = await client.messages.create({
58
+ max_tokens: 1024,
59
+ messages: [{ role: 'user', content: question }],
60
+ model
61
+ });
62
+
63
+ const { content: [{type, text: answer}] } = message;
64
+ log({type});
65
+
66
+ switch(type) {
67
+ case "text":
68
+ default:
69
+ try {
70
+ const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
71
+ return next({outputs: { json: JSON.parse(json) }});
72
+ } catch(e) {
73
+ log(e);
74
+ // plain text
75
+ }
76
+ return next({outputs: { answer }});
77
+ }
78
+ })();
79
+ source: catalog
80
+ title: en=Ask Claude;ru=Спросить Claude
81
+ version: 1