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

Fix bug with Agents for the enable_trace events #254

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libs/aws/langchain_aws/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def parse_agent_response(response: Any) -> OutputType:
response_text = ""
event_stream = response["completion"]
session_id = response["sessionId"]
trace_log = ""
trace_log_elements = []
for event in event_stream:
if "trace" in event:
trace_log = json.dumps(event["trace"])
trace_log_elements.append(event["trace"])

if "returnControl" in event:
response_text = json.dumps(event)
Expand All @@ -72,6 +72,8 @@ def parse_agent_response(response: Any) -> OutputType:
if "chunk" in event:
response_text = event["chunk"]["bytes"].decode("utf-8")

trace_log = json.dumps(trace_log_elements)

agent_finish = BedrockAgentFinish(
return_values={"output": response_text},
log=response_text,
Expand Down
65 changes: 65 additions & 0 deletions libs/aws/tests/unit_tests/agents/test_bedrock_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest
from base64 import b64encode
from typing import Union

from langchain_aws.agents.base import (
BedrockAgentAction,
BedrockAgentFinish,
parse_agent_response,
)


class TestBedrockAgentResponseParser(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
# Mock successful response with function invocation
self.mock_success_return_of_control_response = {
"sessionId": "123",
"completion": [
{
"returnControl": {
"invocationInputs": [
{
"functionInvocationInput": {
"actionGroup": "price_tool_action_group",
"function": "PriceTool",
"parameters": [
{"name": "Symbol", "value": "XYZ"},
{"name": "Start_Date", "value": "20241020"},
{"name": "End_Date", "value": "20241020"},
],
}
}
]
}
}
],
}

self.mock_success_finish_response = {
"sessionId": "123",
"completion": [
{"chunk": {"bytes": b64encode("FAKE DATA HERE".encode())}},
{"trace": "This is a fake trace event."},
],
}

def test_parse_return_of_control_invocation(self) -> None:
response = self.mock_success_return_of_control_response
parsed_response: Union[list[BedrockAgentAction], BedrockAgentFinish]
parsed_response = parse_agent_response(response)
self.assertIsInstance(
parsed_response, list, "Expected a list of BedrockAgentAction."
)

def test_parse_finish_invocation(self) -> None:
response = self.mock_success_finish_response
parsed_response: Union[list[BedrockAgentAction], BedrockAgentFinish]
parsed_response = parse_agent_response(response)
# Type narrowing - now TypeScript knows parsed_response is BedrockAgentFinish
assert isinstance(parsed_response, BedrockAgentFinish)
assert parsed_response.trace_log is not None, "Expected trace_log"

self.assertGreater(
len(parsed_response.trace_log), 0, "Expected a trace log, none received."
)
Loading
Loading