Skip to content

Commit 20e7abe

Browse files
committed
[feat] bedrock anthropic
1 parent 49a402d commit 20e7abe

File tree

3 files changed

+57
-44
lines changed

3 files changed

+57
-44
lines changed

libs/core/llmstudio_core/config.yaml

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,51 @@ providers:
8181
- BEDROCK_ACCESS_KEY
8282
- BEDROCK_REGION
8383
models:
84+
anthropic.claude-3-sonnet-20240229-v1:0:
85+
mode: chat
86+
max_tokens: 200000
87+
input_token_cost: 0.000003
88+
output_token_cost: 0.000015
8489
anthropic.claude-3-5-sonnet-20240620-v1:0:
90+
mode: chat
91+
max_tokens: 200000
92+
input_token_cost: 0.000003
93+
output_token_cost: 0.000015
94+
anthropic.claude-3-5-sonnet-20241022-v2:0:
95+
mode: chat
96+
max_tokens: 200000
97+
input_token_cost: 0.000003
98+
output_token_cost: 0.000015
99+
anthropic.claude-3-haiku-20240307-v1:0:
100+
mode: chat
101+
max_tokens: 200000
102+
input_token_cost: 0.00000025
103+
output_token_cost: 0.00000125
104+
anthropic.claude-3-5-haiku-20241022-v1:0:
105+
mode: chat
106+
max_tokens: 200000
107+
input_token_cost: 0.000001
108+
output_token_cost: 0.000005
109+
anthropic.claude-3-opus-20240229-v1:0:
110+
mode: chat
111+
max_tokens: 200000
112+
input_token_cost: 0.000015
113+
output_token_cost: 0.000075
114+
anthropic.claude-instant-v1:
85115
mode: chat
86116
max_tokens: 100000
87-
input_token_cost: 0.00000163
88-
output_token_cost: 0.00000551
117+
input_token_cost: 0.0000008
118+
output_token_cost: 0.000024
119+
anthropic.claude-v2:
120+
mode: chat
121+
max_tokens: 100000
122+
input_token_cost: 0.000008
123+
output_token_cost: 0.000024
124+
anthropic.claude-v2:1:
125+
mode: chat
126+
max_tokens: 100000
127+
input_token_cost: 0.000008
128+
output_token_cost: 0.000024
89129
parameters:
90130
temperature:
91131
name: "Temperature"

libs/core/llmstudio_core/providers/bedrock_providers/antropic.py renamed to libs/core/llmstudio_core/providers/bedrock/anthropic.py

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
)
1616

1717
import boto3
18-
from fastapi import HTTPException
1918
from llmstudio_core.exceptions import ProviderError
2019
from llmstudio_core.providers.provider import ChatRequest, ProviderCore, provider
2120

@@ -30,18 +29,23 @@
3029
)
3130
from pydantic import ValidationError
3231

32+
SERVICE = "bedrock-runtime"
33+
3334

3435
@provider
35-
class BedrockAntropicProvider(ProviderCore):
36+
class BedrockAnthropicProvider(ProviderCore):
3637
def __init__(self, config, **kwargs):
3738
super().__init__(config, **kwargs)
38-
self.access_key = (
39-
self.access_key if self.access_key else os.getenv("BEDROCK_ACCESS_KEY")
40-
)
41-
self.secret_key = (
42-
self.secret_key if self.secret_key else os.getenv("BEDROCK_SECRET_KEY")
39+
self._client = boto3.client(
40+
SERVICE,
41+
region_name=self.region if self.region else os.getenv("BEDROCK_REGION"),
42+
aws_access_key_id=self.access_key
43+
if self.access_key
44+
else os.getenv("BEDROCK_ACCESS_KEY"),
45+
aws_secret_access_key=self.secret_key
46+
if self.secret_key
47+
else os.getenv("BEDROCK_SECRET_KEY"),
4348
)
44-
self.region = self.region if self.region else os.getenv("BEDROCK_REGION")
4549

4650
@staticmethod
4751
def _provider_config_name():
@@ -57,26 +61,6 @@ async def agenerate_client(self, request: ChatRequest) -> Coroutine[Any, Any, An
5761
def generate_client(self, request: ChatRequest) -> Coroutine[Any, Any, Generator]:
5862
"""Generate an AWS Bedrock client"""
5963
try:
60-
61-
service = "bedrock-runtime"
62-
63-
if (
64-
self.access_key is None
65-
or self.secret_key is None
66-
or self.region is None
67-
):
68-
raise HTTPException(
69-
status_code=400,
70-
detail="AWS credentials were not given or not set in environment variables.",
71-
)
72-
73-
client = boto3.client(
74-
service,
75-
region_name=self.region,
76-
aws_access_key_id=self.access_key,
77-
aws_secret_access_key=self.secret_key,
78-
)
79-
8064
messages, system_prompt = self._process_messages(request.chat_input)
8165
tools = self._process_tools(request.parameters)
8266

@@ -95,7 +79,7 @@ def generate_client(self, request: ChatRequest) -> Coroutine[Any, Any, Generator
9579
if tools:
9680
client_params["toolConfig"] = tools
9781

98-
return client.converse_stream(**client_params)
82+
return self._client.converse_stream(**client_params)
9983
except Exception as e:
10084
raise ProviderError(str(e))
10185

@@ -249,18 +233,7 @@ def parse_response(self, response: AsyncGenerator[Any, None], **kwargs) -> Any:
249233
def _process_messages(
250234
chat_input: Union[str, List[Dict[str, str]]]
251235
) -> List[Dict[str, Union[List[Dict[str, str]], str]]]:
252-
"""
253-
Generate input text for the Bedrock API based on the provided chat input.
254-
255-
Args:
256-
chat_input (Union[str, List[Dict[str, str]]]): The input text or a list of message dictionaries.
257-
258-
Returns:
259-
List[Dict[str, Union[List[Dict[str, str]], str]]]: A list of formatted messages for the Bedrock API.
260236

261-
Raises:
262-
HTTPException: If the input is invalid.
263-
"""
264237
if isinstance(chat_input, str):
265238
return [
266239
{

libs/core/llmstudio_core/providers/bedrock.py renamed to libs/core/llmstudio_core/providers/bedrock/provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, AsyncGenerator, Coroutine, Generator
22

3-
from llmstudio_core.providers.bedrock_providers.antropic import BedrockAntropicProvider
3+
from llmstudio_core.providers.bedrock.anthropic import BedrockAnthropicProvider
44
from llmstudio_core.providers.provider import ChatRequest, ProviderCore, provider
55

66

@@ -13,7 +13,7 @@ def __init__(self, config, **kwargs):
1313

1414
def _get_provider(self, model):
1515
if "anthropic." in model:
16-
return BedrockAntropicProvider(config=self.config, **self.kwargs)
16+
return BedrockAnthropicProvider(config=self.config, **self.kwargs)
1717

1818
raise ValueError(f" provider is not yet supported.")
1919

0 commit comments

Comments
 (0)