Add support for streaming responses for Amazon Bedrock LLM#2367
Add support for streaming responses for Amazon Bedrock LLM#2367Jacob Lee (jacoblee93) merged 11 commits intolangchain-ai:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| @@ -590,9 +593,11 @@ | |||
| "@aws-sdk/client-sagemaker-runtime": "^3.310.0", | |||
There was a problem hiding this comment.
Great work on the PR! I've noticed that this diff introduces two new dependencies, "@aws-sdk/eventstream-marshaller" and "@aws-sdk/util-utf8-universal", which may be peer/dev/hard dependencies. I'm flagging this for maintainers to review.
| @@ -0,0 +1,267 @@ | |||
| import { SignatureV4 } from "@aws-sdk/signature-v4"; | |||
There was a problem hiding this comment.
This comment is flagging the addition of a new HTTP request using the fetch function in the BedrockChat class, and it should be reviewed by maintainers.
| @@ -0,0 +1,267 @@ | |||
| import { SignatureV4 } from "@aws-sdk/signature-v4"; | |||
There was a problem hiding this comment.
This PR adds a dependency on an environment variable AWS_DEFAULT_REGION via the getEnvironmentVariable function on line 97. Please review this change to ensure it is handled correctly.
|
This looks really fantastic, but I don't think it really fits as a chat model (there's no dialog turns/messages, for example). The We can just add streaming to the LLM version I think? |
| ); | ||
| } | ||
|
|
||
| if (this.streaming) { |
There was a problem hiding this comment.
We now have a new interface for .stream():
https://js.langchain.com/docs/guides/expression_language/interface
Also, it'd be more elegant to always use the streaming call and implement non-streaming requests as just concatenating the entire stream:
|
Let me know if you need help using the newer package! I'm happy to dig in tomorrow. |
…ew, before removing deprecated module and looking into stream interface
|
Thanks for the quick updates! To get I can do this later today though - it should be very similar to what you have, and we can share the streaming logic. |
…aws-bedrock-streaming
…aws-bedrock-streaming
|
Ben Pleasanton (@Benyuel) Refactored a bit so we can support the I also added retries and the ability to pass an abort signal to cancel in flight requests (hackily). Also CC Vitaly Neyman (@vitaly-ps) for a second look? |
|
Thanks so much Jacob Lee (@jacoblee93)! I made a minor change to get the tests working for me, and also added passing in I think this is ready from my perspective
I tested this works as well 👍 |
| chunks.push(chunk); | ||
| } | ||
| expect(chunks.length).toBeGreaterThan(1); | ||
| expect(chunks.length).toBeGreaterThanOrEqual(1); |
There was a problem hiding this comment.
Shouldn't this be greater than 1?
There was a problem hiding this comment.
The mock I added does streaming, but just 1 chunk.
https://github.com/hwchase17/langchainjs/blob/7f2271769488152259791226d4c3767f60919ae9/langchain/src/llms/tests/bedrock.int.test.ts#L321-L337
That's why I swapped this.
| maxTokens: 20, | ||
| region, | ||
| model, | ||
| async fetchFn( |
There was a problem hiding this comment.
Ah I intended this one to test that this works with a live Bedrock instance, without a fetch function mock. Can you try that out?
There was a problem hiding this comment.
oh sorry, you can revert the mock I added. I just assumed no connection to bedrock so mocked it.
To answer your question though, yes I tried this .stream against a live bedrock endpoint too.
|
Hey Ben Pleasanton (@Benyuel), I think you changed the test I added to use a mocked function - does this work with a live Bedrock instance? |
| return responseBody.completion; | ||
| } else if (provider === "ai21") { | ||
| return responseBody.completions[0].data.text; | ||
| return responseBody.data.text; |
There was a problem hiding this comment.
This was wrong before?
There was a problem hiding this comment.
This was correct before for the non-streaming bedrock api. But when I change to the bedrock invoke-with-response-stream endpoint, these change to what's in this PR currently.
|
Looks great! Thanks again for your patience! |
|
Hey both, I've found this PR from one of the linked issues -- I'm still confused about how to implement streaming with Bedrock similar to what is shown in the docs: https://js.langchain.com/docs/modules/model_io/models/chat/how_to/streaming. Simply changing |
|
Ah apologies, we really need to update that - instead use: https://js.langchain.com/docs/expression_language/interface#stream |
|
Awesome thanks Jacob Lee (@jacoblee93), I'm now able to stream with Bedrock too 👍 |
This PR adds support for using the Amazon Bedrock LLM with streaming. See the added test for an example use.
Fixes #2321