-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
55 lines (50 loc) · 1.3 KB
/
Copy pathexample.js
File metadata and controls
55 lines (50 loc) · 1.3 KB
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
import LLMJSON from "./src/index.js";
import {
BedrockRuntimeClient,
ConversationRole,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
// This is using bedrock but you can replace this with your own llm api
async function sendToLLM(
prompt,
modelId = "amazon.nova-lite-v1:0",
maxTokens = 500,
temperature = 0.5
) {
const bedrockClient = new BedrockRuntimeClient({ region: "us-east-1" });
const message = {
content: [{ text: prompt }],
role: ConversationRole.USER,
};
const request = {
modelId,
messages: [message],
inferenceConfig: {
maxTokens,
temperature,
},
};
try {
const response = await bedrockClient.send(new ConverseCommand(request));
const text = response.output?.message?.content?.[0]?.text;
return text || "[No response returned]";
} catch (error) {
console.error(`ERROR: Failed to invoke '${modelId}': ${error.message}`);
throw error;
}
}
const format = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "integer" },
hobbies: {
type: "array",
items: { type: "string" },
},
},
required: ["name", "age", "hobbies"],
}; // You ask your LLM to output in this format ;D
console.log(
await LLMJSON.getJson("What are your age and hobbies", format, sendToLLM)
);