[Security solution] Bedrock streaming and token tracking#170815
[Security solution] Bedrock streaming and token tracking#170815stephmilovic merged 38 commits intoelastic:mainfrom
Conversation
| // make sure the request is only triggered once, | ||
| // even with multiple subscribers | ||
| shareReplay(1), |
There was a problem hiding this comment.
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.
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
semd
left a comment
There was a problem hiding this comment.
Awesome! LGTM. Thanks Steph
pmuellr
left a comment
There was a problem hiding this comment.
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.
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!
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)
invokeStreamsubaction 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.tsinvokeAImethod 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-utfTo test:
export.ndjsonand upload to your saved objectsA note on
api_alerting_integrationtestsI 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 tosupertestit 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