-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Security solution] Bedrock streaming and token tracking #170815
Conversation
// make sure the request is only triggered once, | ||
// even with multiple subscribers | ||
shareReplay(1), |
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.
shareReplay
causes the observable to be hot, which means unsubscribe does not actually stop the stream
new Transform({ | ||
transform(chunk, encoding, callback) { | ||
const encoder = new TextEncoder(); | ||
const decoder = new EventStreamCodec(toUtf8, fromUtf8); |
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.
I could not find documentation on how to handle their codec. I did find awsdocs/aws-doc-sdk-examples#5541 for when documentation should be added.
I followed the bedrock extension in the langchainjs repo here: https://github.com/langchain-ai/langchainjs/pull/2367/files#diff-ead6be41360b3bf1c4559ea4d0069a015b69a57d5f8c51d0cce73253fde30aa2R246-R268
Pinging @elastic/security-threat-hunting (Team:Threat Hunting) |
Pinging @elastic/security-solution (Team: SecuritySolution) |
x-pack/plugins/actions/server/lib/get_token_count_from_bedrock_invoke.ts
Show resolved
Hide resolved
x-pack/plugins/actions/server/lib/get_token_count_from_invoke_stream.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/stack_connectors/server/connector_types/bedrock/bedrock.ts
Outdated
Show resolved
Hide resolved
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.
Awesome! LGTM. Thanks Steph
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.
code LGTM, left a comment about missing (trivial) jest test case
Regarding the functional test, can you use something other than supertest? I saw a thread somewhere of you asking about this, and I think someone else mentioned using node-fetch or such. If that can be made to work, no problem! If not, I guess there's something in our test framework that forces the use of supertest?
Worse case, create some kind of manual test in stack_connectors
like this one we have in the actions
plugin - so we have something to test with ... https://github.com/elastic/kibana/blob/main/x-pack/plugins/actions/server/manual_tests/forward_proxy.js
const passThrough = new PassThrough(); | ||
|
||
supertest | ||
.post(`/internal/elastic_assistant/actions/connector/${bedrockActionId}/_execute`) |
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.
My test wasn't working because I was trying to call execute route directly, instead of my special route that handles the stream.. duh! Thanks @dgieselaar for helping me figure this out!
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.
LGTM
x-pack/plugins/stack_connectors/server/connector_types/openai/openai.test.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/stack_connectors/server/connector_types/openai/openai.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/stack_connectors/server/connector_types/openai/openai.ts
Outdated
Show resolved
Hide resolved
💚 Build Succeeded
Metrics [docs]Async chunks
Page load bundle
History
To update your PR or re-run it, just comment with: |
Summary
Part 2 of 3 of Streaming in Security AI Assistant (Part 1)
invokeStream
subaction to OpenAI and Bedrock. Handles standardizing our assistant stream request to OpenAI and Bedrock formats, and parses the response from the OpenAI and Bedrock formats to a simple string for the consumer to streaminvokeStream
) and non streaming (run
,test
), in action_executor.tsinvokeAI
method of Bedrock does not have token tracking. I plan remove that method in part 3 of 3 of the streaming work. At that time I will add a link to the token tracking dashboard form the Bedrock connector edit, like we have for OpenAIpackage.json changes
I could not find documentation on how to handle the Bedrock codec. I did find awsdocs/aws-doc-sdk-examples#5541 that we can track for when documentation should be added.
I followed the bedrock extension in the langchainjs repo here: https://github.com/langchain-ai/langchainjs/pull/2367/files#diff-ead6be41360b3bf1c4559ea4d0069a015b69a57d5f8c51d0cce73253fde30aa2R246-R268. This is how I found that we'd need these packages:
@smithy/eventstream-codec
,@smithy/util-utf
To test:
export.ndjson
and upload to your saved objectsA note on
api_alerting_integration
testsI spent 2 full work days trying to get these working. I’m trying to get the stream response from
supertest
. I can see the stream running on the backend, but when it comes back tosupertest
it only returns thereadableState
, not the stream itself. If there is anyone who can pair, I’d appreciate it. Otherwise, the code is covered in jest byx-pack/plugins/stack_connectors/server/connector_types/bedrock/bedrock.test.ts