-
Notifications
You must be signed in to change notification settings - Fork 15
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
Gen-AI python implementation #290
base: main
Are you sure you want to change the base?
Conversation
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_bedrock_patches.py
Outdated
Show resolved
Hide resolved
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_bedrock_patches.py
Outdated
Show resolved
Hide resolved
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_bedrock_patches.py
Outdated
Show resolved
Hide resolved
if "inputTextTokenCount" in response_body: | ||
span.set_attribute(GEN_AI_USAGE_INPUT_TOKENS, response_body["inputTextTokenCount"]) | ||
|
||
result = response_body["results"][0] |
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.
Trying to access element with [0]
will throw an error if the results
array doesn't exist. I think we need another conditional check here.
For example:
if 'results' in response_body:
result = response_body["results"][0]
or something more concise like:
result = response_body.get('results', [{}])
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 just saw this, I will fix this and make another commit
if "results" in response_body: | ||
result = response_body["results"][0] | ||
if "tokenCount" in result: | ||
span.set_attribute(GEN_AI_USAGE_OUTPUT_TOKENS, result["tokenCount"]) | ||
if "completionReason" in result: |
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 think this logic will still throw an error.
For example, if results
does not exist in the response_body
, the checks will still continue to the other conditionals like if "tokenCount" in result:
, which will then give an error since result
will be unbound.
You can do something like this instead:
if "results" in response_body and response_body["results"]:
result = response_body["results"][0]
if "tokenCount" in result:
span.set_attribute(GEN_AI_USAGE_OUTPUT_TOKENS, result["tokenCount"])
if "completionReason" in result:
...
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 fixed this
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.
Looks good. Thanks!
Issue #, if available:
Description of changes:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.