-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
integration_bedrock.ts
83 lines (72 loc) · 1.63 KB
/
integration_bedrock.ts
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
import { BedrockChat } from "@langchain/community/chat_models/bedrock";
// Or, from web environments:
// import { BedrockChat } from "@langchain/community/chat_models/bedrock/web";
import { HumanMessage } from "@langchain/core/messages";
// If no credentials are provided, the default credentials from
// @aws-sdk/credential-provider-node will be used.
// modelKwargs are additional parameters passed to the model when it
// is invoked.
const model = new BedrockChat({
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: "us-east-1",
// endpointUrl: "custom.amazonaws.com",
// credentials: {
// accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
// secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
// },
// modelKwargs: {
// anthropic_version: "bedrock-2023-05-31",
// },
});
// Other model names include:
// "mistral.mistral-7b-instruct-v0:2"
// "mistral.mixtral-8x7b-instruct-v0:1"
//
// For a full list, see the Bedrock page in AWS.
const res = await model.invoke([
new HumanMessage({ content: "Tell me a joke" }),
]);
console.log(res);
/*
AIMessage {
content: "Here's a silly joke for you:\n" +
'\n' +
"Why can't a bicycle stand up by itself?\n" +
"Because it's two-tired!",
name: undefined,
additional_kwargs: { id: 'msg_01NYN7Rf39k4cgurqpZWYyDh' }
}
*/
const stream = await model.stream([
new HumanMessage({ content: "Tell me a joke" }),
]);
for await (const chunk of stream) {
console.log(chunk.content);
}
/*
Here
's
a
silly
joke
for
you
:
Why
can
't
a
bicycle
stand
up
by
itself
?
Because
it
's
two
-
tired
!
*/