bedrock.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """AWS Bedrock Converse API transport.
  2. Delegates format conversion to agent/bedrock_adapter.py. Bedrock uses its own
  3. boto3 client, so client construction and calls stay on AIAgent.
  4. """
  5. from typing import Any, Dict, List, Optional
  6. from agent.transports.base import ProviderTransport
  7. from agent.transports.types import NormalizedResponse, ToolCall, Usage
  8. class BedrockTransport(ProviderTransport):
  9. """Transport for api_mode='bedrock_converse'."""
  10. # The adapter already maps inside normalize_converse_response; this serves raw-response access.
  11. _STOP_REASON_MAP = {
  12. "end_turn": "stop", "tool_use": "tool_calls", "max_tokens": "length", "stop_sequence": "stop",
  13. "guardrail_intervened": "content_filter", "content_filtered": "content_filter",
  14. }
  15. @property
  16. def api_mode(self) -> str:
  17. return "bedrock_converse"
  18. def convert_messages(self, messages: list[dict[str, Any]], **kwargs) -> Any:
  19. """Convert OpenAI messages to Bedrock Converse format."""
  20. from agent.bedrock_adapter import convert_messages_to_converse
  21. return convert_messages_to_converse(messages, model=kwargs.get("model"))
  22. def convert_tools(self, tools: list[dict[str, Any]]) -> Any:
  23. """Convert OpenAI tool schemas to Bedrock Converse toolConfig."""
  24. from agent.bedrock_adapter import convert_tools_to_converse
  25. return convert_tools_to_converse(tools)
  26. def build_kwargs(
  27. self, model: str, messages: list[dict[str, Any]],
  28. tools: Optional[list[dict[str, Any]]] = None, **params,
  29. ) -> dict[str, Any]:
  30. """Build Converse kwargs, leaving the optional output limit to the provider."""
  31. from agent.bedrock_adapter import build_converse_kwargs
  32. self._model = model # stamps the turns normalize_response captures as this model's own
  33. kwargs = build_converse_kwargs(
  34. model=model, messages=messages, tools=tools, max_tokens=params.get("max_tokens"),
  35. temperature=params.get("temperature"), guardrail_config=params.get("guardrail_config"),
  36. reasoning_config=params.get("reasoning_config"),
  37. )
  38. # Sentinel keys for dispatch — agent pops these before the boto3 call
  39. kwargs["__bedrock_converse__"] = True
  40. kwargs["__bedrock_region__"] = params.get("region", "us-east-1")
  41. return kwargs
  42. def normalize_response(self, response: Any, **kwargs) -> NormalizedResponse:
  43. """Normalize either a raw boto3 dict or an already-normalized SimpleNamespace with .choices."""
  44. from agent.bedrock_adapter import normalize_converse_response
  45. ns = response if hasattr(response, "choices") and response.choices else normalize_converse_response(response)
  46. choice = ns.choices[0]
  47. msg = choice.message
  48. tool_calls = (
  49. [ToolCall(id=tc.id, name=tc.function.name, arguments=tc.function.arguments) for tc in msg.tool_calls]
  50. if msg.tool_calls else None
  51. )
  52. provider_data = {
  53. key: getattr(msg, key) for key in ("reasoning_details", "bedrock_content_blocks") if getattr(msg, key, None)
  54. }
  55. if provider_data.get("reasoning_details") and getattr(self, "_model", None):
  56. from agent.bedrock_adapter import CONVERSE_ISSUER_TYPE
  57. provider_data["reasoning_details"] = [*provider_data["reasoning_details"],
  58. {"type": CONVERSE_ISSUER_TYPE, "model": self._model}]
  59. return NormalizedResponse(
  60. content=msg.content, tool_calls=tool_calls,
  61. finish_reason=choice.finish_reason or "stop",
  62. reasoning=getattr(msg, "reasoning", None) or getattr(msg, "reasoning_content", None),
  63. usage=Usage.from_openai(ns.usage) if getattr(ns, "usage", None) else None, provider_data=provider_data or None,
  64. )
  65. def validate_response(self, response: Any) -> bool:
  66. """Raw Bedrock dict needs an 'output' key; a normalized namespace needs non-empty .choices."""
  67. if isinstance(response, dict):
  68. return "output" in response
  69. return bool(getattr(response, "choices", None)) if response is not None else False
  70. from agent.transports import register_transport
  71. register_transport("bedrock_converse", BedrockTransport)