Skip to content
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] Use BedrockRuntimeClient to interact with converse APIs #201046

Merged
merged 14 commits into from
Nov 21, 2024

Conversation

stephmilovic
Copy link
Contributor

@stephmilovic stephmilovic commented Nov 20, 2024

Summary

Amazon's documentation recommends using the converse API with @aws-sdk/client-bedrock-runtime. In my previous PR, I was able to implement using the APIs directly only by overriding the API request within BedrockRuntimeClient (NodeHttpHandler) and letting the class handle the decoding. However, when I needed to decode the stream in the actions plugin for token tracking I ran into blockers.

Instead of hitting the converse APIs directly with axios, it seems we should use the AWS SDK in order to rely on them to handle their own encoding. To accomplish this, I reverted my changes from the previous converse APIs PR and instead implement a sub action called bedrockClientSend which invokes the send method of BedrockRuntimeClient using ConverseCommand or ConverseStreamCommand.

Using this strategy, I was able to easily retrieve the final chunk from the stream to calculate usage tokens.

Schema validation

BedrockRuntimeClient.send takes a class as the sole parameter, which I was not able to define using config schema:

Request validation failed (Error: [command]: expected a plain object value, but found [Object] instead.)

So I did need to use schema.any() for the command arg.

@stephmilovic stephmilovic added release_note:skip Skip the PR/issue when compiling release notes v9.0.0 Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Security Generative AI Security Generative AI backport:version Backport to applied version labels v8.17.0 labels Nov 20, 2024
@stephmilovic stephmilovic marked this pull request as ready for review November 20, 2024 21:23
@stephmilovic stephmilovic requested review from a team as code owners November 20, 2024 21:23
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

Copy link
Member

@pmuellr pmuellr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponseOps changes LGTM, given the connector execution is not queued (run as alert actions).

As in a prior PR review, might want to change CODEOWNERS to mark the gen_ai* and get_token* files as owned by your team, as you won't need to wait for a largely rubber-stamped PR review from ResponseOps :-)

@stephmilovic stephmilovic enabled auto-merge (squash) November 20, 2024 22:33
@elasticmachine
Copy link
Contributor

elasticmachine commented Nov 20, 2024

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #8 / #getDataUsageMetricsFiltersFromUrlParams should use given relative startDate and endDate values URL params
  • [job] [logs] Jest Integration Tests #1 / Connector type config checks detect connector type changes for: .bedrock
  • [job] [logs] Jest Integration Tests #1 / Connector type config checks detect connector type changes for: .bedrock

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
stackConnectors 58.3KB 58.3KB -15.0B

History

Comment on lines +453 to +458
if ('stream' in res) {
const resultStream = res.stream as SmithyMessageDecoderStream<unknown>;
// splits the stream in two, [stream = consumer, tokenStream = token tracking]
const [stream, tokenStream] = tee(resultStream);
return { ...res, stream, tokenStream };
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patrykkopycinski I wrote my own tee function to split the iterator into two streams which can be independently read from at different speeds. 🎉

@stephmilovic stephmilovic merged commit e92ef08 into elastic:main Nov 21, 2024
26 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.17, 8.x

https://github.com/elastic/kibana/actions/runs/11961263329

@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.17
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Nov 21, 2024
…teract with converse APIs (#201046) (#201288)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[Security solution] Use &#x60;BedrockRuntimeClient&#x60; to interact
with converse APIs
(#201046)](#201046)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-21T20:38:24Z","message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:
SecuritySolution","Team:Security Generative
AI","backport:version","v8.17.0","v8.18.0"],"title":"[Security solution]
Use `BedrockRuntimeClient` to interact with converse
APIs","number":201046,"url":"https://github.com/elastic/kibana/pull/201046","mergeCommit":{"message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770"}},"sourceBranch":"main","suggestedTargetBranches":["8.17","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201046","number":201046,"mergeCommit":{"message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770"}},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Steph Milovic <[email protected]>
kibanamachine added a commit that referenced this pull request Nov 21, 2024
…eract with converse APIs (#201046) (#201289)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Security solution] Use &#x60;BedrockRuntimeClient&#x60; to interact
with converse APIs
(#201046)](#201046)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-21T20:38:24Z","message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:
SecuritySolution","Team:Security Generative
AI","backport:version","v8.17.0","v8.18.0"],"title":"[Security solution]
Use `BedrockRuntimeClient` to interact with converse
APIs","number":201046,"url":"https://github.com/elastic/kibana/pull/201046","mergeCommit":{"message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770"}},"sourceBranch":"main","suggestedTargetBranches":["8.17","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201046","number":201046,"mergeCommit":{"message":"[Security
solution] Use `BedrockRuntimeClient` to interact with converse APIs
(#201046)","sha":"e92ef08689e7821c5d9fc7d776f301b2ceead770"}},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Steph Milovic <[email protected]>
paulinashakirova pushed a commit to paulinashakirova/kibana that referenced this pull request Nov 26, 2024
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:version Backport to applied version labels release_note:skip Skip the PR/issue when compiling release notes Team:Security Generative AI Security Generative AI Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.17.0 v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants