Skip to content

Commit d8fb10b

Browse files
committed
initial
1 parent da54dfb commit d8fb10b

File tree

5 files changed

+736
-114
lines changed

5 files changed

+736
-114
lines changed

patchwork/common/client/llm/aio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ def create_aio_client(inputs) -> "AioLlmClient" | None:
221221
clients.append(client)
222222

223223
anthropic_key = inputs.get("anthropic_api_key")
224-
if anthropic_key is not None:
225-
client = AnthropicLlmClient(anthropic_key)
224+
if anthropic_key is not None or "is_aws" in client_args.keys():
225+
client = AnthropicLlmClient(anthropic_key, is_aws=client_args.get("is_aws", False))
226226
clients.append(client)
227227

228228
if len(clients) == 0:

patchwork/common/client/llm/anthropic.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import cached_property, lru_cache
66
from pathlib import Path
77

8-
from anthropic import Anthropic
8+
from anthropic import Anthropic, AnthropicBedrock
99
from anthropic.types import Message, MessageParam, TextBlockParam
1010
from openai.types.chat import (
1111
ChatCompletion,
@@ -24,10 +24,11 @@
2424
from pydantic_ai.messages import ModelMessage, ModelResponse
2525
from pydantic_ai.models import Model, ModelRequestParameters, StreamedResponse
2626
from pydantic_ai.models.anthropic import AnthropicModel
27+
from pydantic_ai.models.bedrock import BedrockConverseModel
2728
from pydantic_ai.settings import ModelSettings
2829
from pydantic_ai.usage import Usage
2930
from typing_extensions import AsyncIterator, Dict, Iterable, List, Optional, Union
30-
31+
import boto3
3132
from patchwork.common.client.llm.protocol import NOT_GIVEN, LlmClient, NotGiven
3233

3334

@@ -78,21 +79,29 @@ class AnthropicLlmClient(LlmClient):
7879
__definitely_allowed_models = {"claude-2.0", "claude-2.1", "claude-instant-1.2"}
7980
__100k_models = {"claude-2.0", "claude-instant-1.2"}
8081

81-
def __init__(self, api_key: str):
82+
def __init__(self, api_key: Optional[str] = None, is_aws: bool = False):
8283
self.__api_key = api_key
84+
self.__is_aws = is_aws
85+
if self.__api_key is None and not is_aws:
86+
raise ValueError("api_key is required if is_aws is False")
8387

8488
@cached_property
8589
def __client(self):
86-
return Anthropic(api_key=self.__api_key)
90+
if not self.__is_aws:
91+
return Anthropic(api_key=self.__api_key)
92+
else:
93+
return AnthropicBedrock()
8794

8895
def __get_pydantic_model(self, model_settings: ModelSettings | None) -> Model:
8996
if model_settings is None:
9097
raise ValueError("Model settings cannot be None")
9198
model_name = model_settings.get("model")
9299
if model_name is None:
93100
raise ValueError("Model must be set cannot be None")
94-
95-
return AnthropicModel(model_name, api_key=self.__api_key)
101+
if not self.__is_aws:
102+
return AnthropicModel(model_name, api_key=self.__api_key)
103+
else:
104+
return BedrockConverseModel(model_name)
96105

97106
async def request(
98107
self,
@@ -247,10 +256,23 @@ def __adapt_chat_completion_request(
247256

248257
@lru_cache(maxsize=None)
249258
def get_models(self) -> set[str]:
250-
return self.__definitely_allowed_models.union(set(f"{self.__allowed_model_prefix}*"))
259+
rv = set()
260+
if not self.__is_aws:
261+
for model_info in self.__client.models.list():
262+
rv.add(model_info.id)
263+
else:
264+
bedrock = boto3.client(service_name="bedrock")
265+
response = bedrock.list_foundation_models(byProvider="anthropic")
266+
for model_info in response["modelSummaries"]:
267+
rv.add(model_info["modelId"])
268+
269+
return rv
251270

252271
def is_model_supported(self, model: str) -> bool:
253-
return model in self.__definitely_allowed_models or model.startswith(self.__allowed_model_prefix)
272+
if not self.__is_aws:
273+
return model in self.get_models()
274+
else:
275+
return any(True for model_id in self.get_models() if model.endswith(model_id))
254276

255277
def is_prompt_supported(
256278
self,

patchwork/steps/FileAgent/FileAgent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, inputs):
2121
task = mustache_render(inputs["task"], data)
2222

2323
self.strat_kwargs = dict(
24-
model="claude-3-5-sonnet-latest",
24+
model="apac.anthropic.claude-3-5-sonnet-20241022-v2:0",
2525
llm_client=AioLlmClient.create_aio_client(inputs),
2626
template_data=dict(),
2727
system_prompt_template=f"""\
@@ -35,7 +35,8 @@ def __init__(self, inputs):
3535
agent_configs=[
3636
AgentConfig(
3737
name="Assistant",
38-
model="claude-3-7-sonnet-latest",
38+
model="apac.anthropic.claude-3-5-sonnet-20241022-v2:0",
39+
# model="anthropic.claude-3-7-sonnet-20250219-v1:0",
3940
tool_set=dict(),
4041
system_prompt="""\
4142
You are a assistant that is supposed to help me with a set of files. These files are commonly tabular formatted like csv, xls or xlsx.

0 commit comments

Comments
 (0)