43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 | @dataclass
class BedrockProvider(LLMProvider):
"""
Amazon Bedrock LLM Provider using LiteLLM.
Supports Claude, Llama, Mistral, and Nova.
"""
region: str = "us-east-1"
default_model: str = "bedrock/us.anthropic.claude-opus-4-5-20251101-v1:0"
_name: str = "bedrock"
@property
def name(self) -> str:
return self._name
@property
def description(self) -> str:
return "Neural routing via Amazon Bedrock (Claude, Llama, DeepSeek). No specialized RAG knowledge."
def list_models(self) -> list[ModelInfo]:
return [
ModelInfo(
name="bedrock/us.anthropic.claude-opus-4-5-20251101-v1:0",
family="claude-opus-4-5",
context_window=200_000,
supports_tools=True,
supports_json=True,
supports_stream=True,
),
ModelInfo(
name="bedrock/us.deepseek.r1-v1:0",
family="deepseek-r1",
context_window=128_000,
supports_tools=True,
supports_json=True,
supports_stream=True,
),
ModelInfo(
name="bedrock/meta.llama3-1-70b-instruct-v1:0",
family="llama3",
context_window=128_000,
supports_tools=True,
supports_json=True,
supports_stream=True,
),
]
def chat(self, req: ChatRequest) -> ChatResponse:
model_id = req.model or self.default_model
# litellm expects bedrock/ prefix for models if not already present
if not model_id.startswith("bedrock/"):
model_id = f"bedrock/{model_id}"
messages = [m.__dict__ for m in req.messages]
tools_payload = None
if req.tools:
tools_payload = [
{
"type": "function",
"function": {
"name": t.name,
"description": t.description,
"parameters": t.input_schema,
},
}
for t in req.tools
]
start = time.time()
try:
response = litellm.completion(
model=model_id,
messages=messages,
temperature=float(req.temperature),
max_tokens=req.max_tokens or 1024,
tools=tools_payload,
tool_choice=req.tool_choice if tools_payload else None,
aws_region_name=self.region,
)
except Exception as e:
raise RuntimeError(f"LiteLLM Bedrock completion failed: {e}") from e
choice = response.choices[0]
content_text = choice.message.content or ""
tool_calls = []
if hasattr(choice.message, "tool_calls") and choice.message.tool_calls:
for tc in choice.message.tool_calls:
tool_calls.append(
ToolCall(
id=tc.id,
name=tc.function.name,
arguments=tc.function.arguments,
)
)
usage = getattr(response, "usage", {})
latency = int((time.time() - start) * 1000)
return ChatResponse(
content=content_text,
tool_calls=tool_calls,
model=model_id,
provider=self.name,
prompt_tokens=getattr(usage, "prompt_tokens", 0),
completion_tokens=getattr(usage, "completion_tokens", 0),
latency_ms=latency,
)
def embed(
self, texts: Iterable[str], model: Optional[str] = None
) -> list[list[float]]:
return [[0.0] * 1024 for _ in texts]
def count_tokens(self, text: str, model: Optional[str] = None) -> int:
return max(1, len(text) // 4)
|