mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
3b77192ad0
* Pushing the bedrock related changes to the new branch after addressing the review comments * 2524 Addressed the second round review comments * 2524 Addressed few more minor comments on the PR * resolving the merge conflict * 2524 resolved the uv.lock conflicts * 2524 addressed more comments * 2524 removed the print statement to fix the checks failure * 2524 resolved the CI failure issues * 2524 fixing the CI breaks * 2524 Addressed the review comment * 2524 resolved conflict --------- Co-authored-by: Sunil Dutta <sunil.dutta@penske.com> Co-authored-by: budgetboardingai <apurva.sharma31@gmail.com>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from agent_framework import ChatMessage, ChatOptions, Role, TextContent
|
|
from agent_framework.exceptions import ServiceInitializationError
|
|
|
|
from agent_framework_bedrock import BedrockChatClient
|
|
|
|
|
|
class _StubBedrockRuntime:
|
|
def __init__(self) -> None:
|
|
self.calls: list[dict[str, Any]] = []
|
|
|
|
def converse(self, **kwargs: Any) -> dict[str, Any]:
|
|
self.calls.append(kwargs)
|
|
return {
|
|
"modelId": kwargs["modelId"],
|
|
"responseId": "resp-123",
|
|
"usage": {"inputTokens": 10, "outputTokens": 5, "totalTokens": 15},
|
|
"output": {
|
|
"completionReason": "end_turn",
|
|
"message": {
|
|
"id": "msg-1",
|
|
"role": "assistant",
|
|
"content": [{"text": "Bedrock says hi"}],
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def test_get_response_invokes_bedrock_runtime() -> None:
|
|
stub = _StubBedrockRuntime()
|
|
client = BedrockChatClient(
|
|
model_id="amazon.titan-text",
|
|
region="us-west-2",
|
|
client=stub,
|
|
)
|
|
|
|
messages = [
|
|
ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="You are concise.")]),
|
|
ChatMessage(role=Role.USER, contents=[TextContent(text="hello")]),
|
|
]
|
|
|
|
response = asyncio.run(client.get_response(messages=messages, chat_options=ChatOptions(max_tokens=32)))
|
|
|
|
assert stub.calls, "Expected the runtime client to be called"
|
|
payload = stub.calls[0]
|
|
assert payload["modelId"] == "amazon.titan-text"
|
|
assert payload["messages"][0]["content"][0]["text"] == "hello"
|
|
assert response.messages[0].contents[0].text == "Bedrock says hi"
|
|
assert response.usage_details and response.usage_details.input_token_count == 10
|
|
|
|
|
|
def test_build_request_requires_non_system_messages() -> None:
|
|
client = BedrockChatClient(
|
|
model_id="amazon.titan-text",
|
|
region="us-west-2",
|
|
client=_StubBedrockRuntime(),
|
|
)
|
|
|
|
messages = [ChatMessage(role=Role.SYSTEM, contents=[TextContent(text="Only system text")])]
|
|
|
|
with pytest.raises(ServiceInitializationError):
|
|
client._build_converse_request(messages, ChatOptions())
|