Skip to content

Commit

Permalink
updated lambda python example
Browse files Browse the repository at this point in the history
  • Loading branch information
brnaba-aws committed Oct 15, 2024
1 parent f7833b7 commit 98aa5ae
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions docs/src/content/docs/deployment/aws-lambda-py.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,35 @@ import asyncio
from typing import Dict, Any
from multi_agent_orchestrator.orchestrator import MultiAgentOrchestrator, OrchestratorConfig
from multi_agent_orchestrator.agents import BedrockLLMAgent, BedrockLLMAgentOptions, AgentResponse
from multi_agent_orchestrator.types import ConversationMessage
# Initialize orchestrator
orchestrator = MultiAgentOrchestrator(OrchestratorConfig(
# Configuration options
))
# Add agents
# Add agents e.g Tech Agent
tech_agent = BedrockLLMAgent(BedrockLLMAgentOptions(
# Agent configuration
name="Tech Agent",
streaming=False,
description="Specializes in technology areas including software development, hardware, AI, \
cybersecurity, blockchain, cloud computing, emerging tech innovations, and pricing/costs \
related to technology products and services.",
model_id="anthropic.claude-3-sonnet-20240229-v1:0",
))
orchestrator.add_agent(tech_agent)
def serialize_agent_response(response: AgentResponse) -> Dict[str, Any]:
def serialize_agent_response(response: Any) -> Dict[str, Any]:
text_response = ''
if isinstance(response, AgentResponse) and response.streaming is False:
# Handle regular response
if isinstance(response.output, str):
text_response = response.output
elif isinstance(response.output, ConversationMessage):
text_response = response.output.content[0].get('text')
"""Convert AgentResponse into a JSON-serializable dictionary."""
return {
"metadata": {
Expand All @@ -70,7 +85,7 @@ def serialize_agent_response(response: AgentResponse) -> Dict[str, Any]:
"user_input": response.metadata.user_input,
"session_id": response.metadata.session_id,
},
"output": response.output,
"output": text_response,
"streaming": response.streaming,
}
Expand All @@ -79,7 +94,7 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]:
user_input = event.get('query')
user_id = event.get('userId')
session_id = event.get('sessionId')
response:AgentResponse = asyncio.run(orchestrator.route_request(user_input, user_id, session_id))
response = asyncio.run(orchestrator.route_request(user_input, user_id, session_id))
# Serialize the AgentResponse to a JSON-compatible format
serialized_response = serialize_agent_response(response)
Expand Down

0 comments on commit 98aa5ae

Please sign in to comment.