-
Notifications
You must be signed in to change notification settings - Fork 125
Add Converse API support. DO NOT MERGE YET #1428
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
Open
umaannamalai
wants to merge
16
commits into
main
Choose a base branch
from
coverse-non-streaming-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3935592
Add Converse API support.
umaannamalai 72c10b6
Update aiobotocore instrumentation.
umaannamalai 2bcdc6a
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 00036ed
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 5bd7277
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] bdc5156
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 2e32984
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 75822f6
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 286c0eb
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 48fdbf7
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] ad37afa
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] cfda7fd
Add support for converse calls made with aioboto3 clients.
umaannamalai e5bc8af
Merge branch 'coverse-non-streaming-support' of github.com:newrelic/n…
umaannamalai 0b39c77
Linting fixes.
umaannamalai 1caf3fc
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] 50080ab
Merge branch 'main' into coverse-non-streaming-support
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
tests/external_botocore/_mock_external_bedrock_server_converse.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
|
||
from testing_support.mock_external_http_server import MockExternalHTTPServer | ||
|
||
RESPONSES = { | ||
"What is 212 degrees Fahrenheit converted to Celsius?": [ | ||
{"Content-Type": "application/json", "x-amzn-RequestId": "c20d345e-6878-4778-b674-6b187bae8ecf"}, | ||
200, | ||
{ | ||
"metrics": {"latencyMs": 1866}, | ||
"output": { | ||
"message": { | ||
"content": [ | ||
{ | ||
"text": "To convert 212°F to Celsius, we can use the formula:\n\nC = (F - 32) × 5/9\n\nWhere:\nC is the temperature in Celsius\nF is the temperature in Fahrenheit\n\nPlugging in 212°F, we get:\n\nC = (212 - 32) × 5/9\nC = 180 × 5/9\nC = 100\n\nTherefore, 212°" | ||
} | ||
], | ||
"role": "assistant", | ||
} | ||
}, | ||
"stopReason": "max_tokens", | ||
"usage": {"inputTokens": 26, "outputTokens": 100, "totalTokens": 126}, | ||
}, | ||
], | ||
"Invalid Token": [ | ||
{ | ||
"Content-Type": "application/json", | ||
"x-amzn-RequestId": "e1206e19-2318-4a9d-be98-017c73f06118", | ||
"x-amzn-ErrorType": "UnrecognizedClientException:http://internal.amazon.com/coral/com.amazon.coral.service/", | ||
}, | ||
403, | ||
{"message": "The security token included in the request is invalid."}, | ||
], | ||
"Model does not exist.": [ | ||
{ | ||
"Content-Type": "application/json", | ||
"x-amzn-RequestId": "f4908827-3db9-4742-9103-2bbc34578b03", | ||
"x-amzn-ErrorType": "ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/", | ||
}, | ||
400, | ||
{"message": "The provided model identifier is invalid."}, | ||
], | ||
} | ||
|
||
|
||
def simple_get(self): | ||
content_len = int(self.headers.get("content-length")) | ||
body = self.rfile.read(content_len).decode("utf-8") | ||
try: | ||
content = json.loads(body) | ||
except Exception: | ||
content = body | ||
|
||
prompt = extract_shortened_prompt_converse(content) | ||
if not prompt: | ||
self.send_response(500) | ||
self.end_headers() | ||
self.wfile.write(b"Could not parse prompt.") | ||
return | ||
|
||
headers, status_code, response = ({}, 0, "") | ||
|
||
for k, v in RESPONSES.items(): | ||
if prompt.startswith(k): | ||
headers, status_code, response = v | ||
break | ||
|
||
if not response: | ||
# If no matches found | ||
self.send_response(500) | ||
self.end_headers() | ||
self.wfile.write(f"Unknown Prompt:\n{prompt}".encode()) | ||
return | ||
|
||
# Send response code | ||
self.send_response(status_code) | ||
|
||
# Send headers | ||
for k, v in headers.items(): | ||
self.send_header(k, v) | ||
self.end_headers() | ||
|
||
# Send response body | ||
response_body = json.dumps(response).encode("utf-8") | ||
|
||
self.wfile.write(response_body) | ||
return | ||
|
||
|
||
def extract_shortened_prompt_converse(content): | ||
try: | ||
prompt = content["messages"][0].get("content")[0].get("text", None) | ||
# Sometimes there are leading whitespaces in the prompt. | ||
prompt = prompt.lstrip().split("\n")[0] | ||
except Exception: | ||
prompt = "" | ||
return prompt | ||
|
||
|
||
class MockExternalBedrockConverseServer(MockExternalHTTPServer): | ||
# To use this class in a test one needs to start and stop this server | ||
# before and after making requests to the test app that makes the external | ||
# calls. | ||
|
||
def __init__(self, handler=simple_get, port=None, *args, **kwargs): | ||
super().__init__(handler=handler, port=port, *args, **kwargs) # noqa: B026 |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A couple thoughts on the design of this:
I wonder if there's a way you could move this if is_converse block into a request_extractor? Have you explored that at all/do you think that's worth exploring?
OR
Maybe converse should not use this extractor concept at all since it doesn't need to.