Update anthropic.yaml
Browse files- anthropic.yaml +44 -22
anthropic.yaml
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
_id: anthropic
|
| 2 |
-
description: Ask Claude agent
|
| 3 |
-
readme: The second version
|
| 4 |
title: Anthropic
|
| 5 |
-
|
| 6 |
version: 1
|
|
|
|
|
|
|
| 7 |
nodes:
|
| 8 |
ask_claude:
|
| 9 |
_id: ask_claude
|
|
@@ -33,6 +33,16 @@ nodes:
|
|
| 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=Ответ
|
|
@@ -42,17 +52,23 @@ nodes:
|
|
| 42 |
type: json
|
| 43 |
package: anthropic
|
| 44 |
script: |
|
| 45 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
const message = await client.messages.create({
|
| 58 |
max_tokens: 1024,
|
|
@@ -60,22 +76,28 @@ nodes:
|
|
| 60 |
model
|
| 61 |
});
|
| 62 |
|
| 63 |
-
const { content: [{type, text: answer}] } = message;
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
try {
|
| 70 |
const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
|
| 71 |
-
return
|
| 72 |
-
} catch(e) {
|
| 73 |
log(e);
|
| 74 |
-
|
|
|
|
| 75 |
}
|
| 76 |
-
|
|
|
|
| 77 |
}
|
| 78 |
-
}
|
| 79 |
source: catalog
|
| 80 |
title: en=Ask Claude;ru=Спросить Claude
|
| 81 |
version: 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
_id: anthropic
|
|
|
|
|
|
|
| 2 |
title: Anthropic
|
| 3 |
+
description: Ask Claude agent
|
| 4 |
version: 1
|
| 5 |
+
readme: The first base version
|
| 6 |
+
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/anthropic.yaml
|
| 7 |
nodes:
|
| 8 |
ask_claude:
|
| 9 |
_id: ask_claude
|
|
|
|
| 33 |
default: claude-3-5-sonnet-latest
|
| 34 |
enum:
|
| 35 |
- claude-3-5-sonnet-latest|Claude 3.5 Sonnet
|
| 36 |
+
answerFormat:
|
| 37 |
+
order: 3
|
| 38 |
+
title: en=Answer format;ru=Формат ответа
|
| 39 |
+
description: Don't forget add instructions for LLM
|
| 40 |
+
type: string
|
| 41 |
+
required: true
|
| 42 |
+
default: text
|
| 43 |
+
enum:
|
| 44 |
+
- text|Text
|
| 45 |
+
- json|JSON
|
| 46 |
outputs:
|
| 47 |
answer:
|
| 48 |
title: en=Answer;ru=Ответ
|
|
|
|
| 52 |
type: json
|
| 53 |
package: anthropic
|
| 54 |
script: |
|
| 55 |
+
export async function costs({ inputs }) {
|
| 56 |
+
return 0.05;
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
+
export async function run({ inputs }) {
|
| 60 |
+
|
| 61 |
+
const { FatalError, NextNode } = DEFINITIONS;
|
| 62 |
+
const Anthropic = require('@anthropic-ai/sdk');
|
| 63 |
|
| 64 |
+
const ANTHROPIC_API_KEY = env?.variables?.get('ANTHROPIC_API_KEY');
|
| 65 |
+
if (!ANTHROPIC_API_KEY) {
|
| 66 |
+
throw new FatalError('Please, set your API key for Anthropic');
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
const client = new Anthropic({ apiKey: ANTHROPIC_API_KEY });
|
| 70 |
+
|
| 71 |
+
const { model, question, answerFormat } = inputs;
|
| 72 |
|
| 73 |
const message = await client.messages.create({
|
| 74 |
max_tokens: 1024,
|
|
|
|
| 76 |
model
|
| 77 |
});
|
| 78 |
|
| 79 |
+
const { content: [{ type, text: answer }] } = message;
|
| 80 |
+
|
| 81 |
+
switch (answerFormat) {
|
| 82 |
+
case 'text':
|
| 83 |
+
return NextNode.from({ outputs: { answer }, costs: await costs({ inputs }) });
|
| 84 |
+
case 'json':
|
| 85 |
try {
|
| 86 |
const json = answer.replace(/^\`\`\`json\s*/ig, '').replace(/\`\`\`\s*$/ig, '');
|
| 87 |
+
return NextNode.from({ outputs: { json: JSON.parse(json) }, costs: await costs({ inputs }) });
|
| 88 |
+
} catch (e) {
|
| 89 |
log(e);
|
| 90 |
+
message(`Wrong JSON for question \`\`\`text\n${question}\n\`\`\`\nnanswer from LLM\n\`\`\`text${answer}\n\`\`\``, 'defect');
|
| 91 |
+
throw new FatalError("Can't parse JSON asnwer from LLM");
|
| 92 |
}
|
| 93 |
+
default:
|
| 94 |
+
throw new FatalError(`Wrong answer format ${answerFormat}`);
|
| 95 |
}
|
| 96 |
+
}
|
| 97 |
source: catalog
|
| 98 |
title: en=Ask Claude;ru=Спросить Claude
|
| 99 |
version: 1
|
| 100 |
+
costs: |-
|
| 101 |
+
(() => {
|
| 102 |
+
return 0.001;
|
| 103 |
+
})();
|