-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meta Llama2 support for BedrockChat #3260
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,85 +2,162 @@ | |
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
import { test, expect } from "@jest/globals"; | ||
import { ChatBedrock } from "../bedrock/web.js"; | ||
import { BedrockChat } from "../bedrock/web.js"; | ||
import { HumanMessage } from "../../schema/index.js"; | ||
|
||
test("Test Bedrock chat model: Claude-v2", async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? "us-east-1"; | ||
const model = "anthropic.claude-v2"; | ||
void testChatModel( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do something like |
||
"Test Bedrock chat model: Llama2 13B v1", | ||
"us-east-1", | ||
"meta.llama2-13b-chat-v1", | ||
"What is your name?" | ||
); | ||
void testChatStreamingModel( | ||
"Test Bedrock streaming chat model: Llama2 13B v1", | ||
"us-east-1", | ||
"meta.llama2-13b-chat-v1", | ||
"What is your name and something about yourself?" | ||
); | ||
|
||
const bedrock = new ChatBedrock({ | ||
maxTokens: 20, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
}, | ||
}); | ||
void testChatModel( | ||
"Test Bedrock chat model: Claude-v2", | ||
"us-east-1", | ||
"anthropic.claude-v2", | ||
"What is your name?" | ||
); | ||
void testChatStreamingModel( | ||
"Test Bedrock chat model streaming: Claude-v2", | ||
"us-east-1", | ||
"anthropic.claude-v2", | ||
"What is your name and something about yourself?" | ||
); | ||
|
||
const res = await bedrock.call([new HumanMessage("What is your name?")]); | ||
console.log(res); | ||
}); | ||
void testChatHandleLLMNewToken( | ||
"Test Bedrock chat model HandleLLMNewToken: Claude-v2", | ||
"us-east-1", | ||
"anthropic.claude-v2", | ||
"What is your name and something about yourself?" | ||
); | ||
void testChatHandleLLMNewToken( | ||
"Test Bedrock chat model HandleLLMNewToken: Llama2 13B v1", | ||
"us-east-1", | ||
"meta.llama2-13b-chat-v1", | ||
"What is your name and something about yourself?" | ||
); | ||
|
||
test("Test Bedrock chat model streaming: Claude-v2", async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? "us-east-1"; | ||
const model = "anthropic.claude-v2"; | ||
/** | ||
* Tests a BedrockChat model | ||
* @param title The name of the test to run | ||
* @param defaultRegion The AWS region to default back to if not set via environment | ||
* @param model The model string to test | ||
* @param message The prompt test to send to the LLM | ||
*/ | ||
async function testChatModel( | ||
title: string, | ||
defaultRegion: string, | ||
model: string, | ||
message: string | ||
) { | ||
test(title, async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion; | ||
|
||
const bedrock = new ChatBedrock({ | ||
maxTokens: 200, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
}, | ||
const bedrock = new BedrockChat({ | ||
maxTokens: 20, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
sessionToken: process.env.BEDROCK_AWS_SESSION_TOKEN, | ||
}, | ||
}); | ||
|
||
const res = await bedrock.call([new HumanMessage(message)]); | ||
console.log(res); | ||
}); | ||
} | ||
/** | ||
* Tests a BedrockChat model with a streaming response | ||
* @param title The name of the test to run | ||
* @param defaultRegion The AWS region to default back to if not set via environment | ||
* @param model The model string to test | ||
* @param message The prompt test to send to the LLM | ||
*/ | ||
async function testChatStreamingModel( | ||
title: string, | ||
defaultRegion: string, | ||
model: string, | ||
message: string | ||
) { | ||
test(title, async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion; | ||
|
||
const stream = await bedrock.stream([ | ||
new HumanMessage({ | ||
content: "What is your name and something about yourself?", | ||
}), | ||
]); | ||
const chunks = []; | ||
for await (const chunk of stream) { | ||
console.log(chunk); | ||
chunks.push(chunk); | ||
} | ||
expect(chunks.length).toBeGreaterThan(1); | ||
}); | ||
const bedrock = new BedrockChat({ | ||
maxTokens: 200, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
sessionToken: process.env.BEDROCK_AWS_SESSION_TOKEN, | ||
}, | ||
}); | ||
|
||
test("Test Bedrock chat model handleLLMNewToken: Claude-v2", async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? "us-east-1"; | ||
const model = "anthropic.claude-v2"; | ||
const tokens: string[] = []; | ||
const stream = await bedrock.stream([ | ||
new HumanMessage({ | ||
content: message, | ||
}), | ||
]); | ||
const chunks = []; | ||
for await (const chunk of stream) { | ||
console.log(chunk); | ||
chunks.push(chunk); | ||
} | ||
expect(chunks.length).toBeGreaterThan(1); | ||
}); | ||
} | ||
/** | ||
* Tests a BedrockChat model with a streaming response using a new token callback | ||
* @param title The name of the test to run | ||
* @param defaultRegion The AWS region to default back to if not set via environment | ||
* @param model The model string to test | ||
* @param message The prompt test to send to the LLM | ||
*/ | ||
async function testChatHandleLLMNewToken( | ||
title: string, | ||
defaultRegion: string, | ||
model: string, | ||
message: string | ||
) { | ||
test(title, async () => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion; | ||
const tokens: string[] = []; | ||
|
||
const bedrock = new ChatBedrock({ | ||
maxTokens: 200, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
}, | ||
streaming: true, | ||
callbacks: [ | ||
{ | ||
handleLLMNewToken: (token) => { | ||
tokens.push(token); | ||
}, | ||
const bedrock = new BedrockChat({ | ||
maxTokens: 200, | ||
region, | ||
model, | ||
maxRetries: 0, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
sessionToken: process.env.BEDROCK_AWS_SESSION_TOKEN, | ||
}, | ||
], | ||
streaming: true, | ||
callbacks: [ | ||
{ | ||
handleLLMNewToken: (token) => { | ||
tokens.push(token); | ||
}, | ||
}, | ||
], | ||
}); | ||
const stream = await bedrock.call([new HumanMessage(message)]); | ||
expect(tokens.length).toBeGreaterThan(1); | ||
expect(stream.content).toEqual(tokens.join("")); | ||
}); | ||
const stream = await bedrock.call([ | ||
new HumanMessage("What is your name and something about yourself?"), | ||
]); | ||
expect(tokens.length).toBeGreaterThan(1); | ||
expect(stream.content).toEqual(tokens.join("")); | ||
}); | ||
} | ||
|
||
test.skip.each([ | ||
"amazon.titan-text-express-v1", | ||
|
@@ -90,14 +167,15 @@ test.skip.each([ | |
])("Test Bedrock base chat model: %s", async (model) => { | ||
const region = process.env.BEDROCK_AWS_REGION ?? "us-east-1"; | ||
|
||
const bedrock = new ChatBedrock({ | ||
const bedrock = new BedrockChat({ | ||
region, | ||
model, | ||
maxRetries: 0, | ||
modelKwargs: {}, | ||
credentials: { | ||
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!, | ||
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!, | ||
sessionToken: process.env.BEDROCK_AWS_SESSION_TOKEN, | ||
}, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR introduces changes that access environment variables via
process.env
orgetEnvironmentVariable
. Please review these changes to ensure they are handled correctly and securely.