Skip to content

support bedrock config #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions app/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def model_dump(self, *args, **kwargs):

# Main client class for interacting with Amazon Bedrock
class BedrockClient:
def __init__(self):
def __init__(
self,
aws_access_key_id: str | None = None,
aws_secret_access_key: str | None = None,
aws_region_name: str | None = None,
):
# Initialize Bedrock client, you need to configure AWS env first
try:
self.client = boto3.client("bedrock-runtime")
self.client = boto3.client(
"bedrock-runtime",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region_name,
)
self.chat = Chat(self.client)
except Exception as e:
print(f"Error initializing Bedrock client: {e}")
Expand Down Expand Up @@ -173,9 +183,9 @@ def _convert_bedrock_response_to_openai_format(self, bedrock_response):
"role": bedrock_response.get("output", {})
.get("message", {})
.get("role", "assistant"),
"tool_calls": openai_tool_calls
if openai_tool_calls != []
else None,
"tool_calls": (
openai_tool_calls if openai_tool_calls != [] else None
),
"function_call": None,
},
}
Expand Down
10 changes: 9 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ class LLMSettings(BaseModel):
description="Maximum input tokens to use across all requests (None for unlimited)",
)
temperature: float = Field(1.0, description="Sampling temperature")
api_type: str = Field(..., description="Azure, Openai, or Ollama")
api_type: str = Field(..., description="Azure, Openai, Ollama or Bedrock")
api_version: str = Field(..., description="Azure Openai version if AzureOpenai")
aws_access_key_id: str = Field(..., description="Aws access key id if Bedrock")
aws_secret_access_key: str = Field(
..., description="Aws secret access key if Bedrock"
)
aws_region_name: str = Field(..., description="Aws region name if Bedrock")


class ProxySettings(BaseModel):
Expand Down Expand Up @@ -175,6 +180,9 @@ def _load_initial_config(self):
"temperature": base_llm.get("temperature", 1.0),
"api_type": base_llm.get("api_type", ""),
"api_version": base_llm.get("api_version", ""),
"aws_access_key_id": base_llm.get("aws_access_key_id", ""),
"aws_secret_access_key": base_llm.get("aws_secret_access_key", ""),
"aws_region_name": base_llm.get("aws_region_name", ""),
}

# handle browser config.
Expand Down
9 changes: 8 additions & 1 deletion app/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def __init__(
self.api_key = llm_config.api_key
self.api_version = llm_config.api_version
self.base_url = llm_config.base_url
self.aws_access_key_id = llm_config.aws_access_key_id
self.aws_secret_access_key = llm_config.aws_secret_access_key
self.aws_region_name = llm_config.aws_region_name

# Add token counting related attributes
self.total_input_tokens = 0
Expand All @@ -227,7 +230,11 @@ def __init__(
api_version=self.api_version,
)
elif self.api_type == "aws":
self.client = BedrockClient()
self.client = BedrockClient(
aws_access_key_id=self.aws_access_key_id,
aws_secret_access_key=self.aws_secret_access_key,
aws_region_name=self.aws_region_name,
)
else:
self.client = AsyncOpenAI(api_key=self.api_key, base_url=self.base_url)

Expand Down
3 changes: 3 additions & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ temperature = 0.0 # Controls randomness
# max_tokens = 8192
# temperature = 1.0
# api_key = "bear" # Required but not used for Bedrock
# aws_access_key_id = "" # Required
# aws_secret_access_key = "" # Required
# aws_region_name = "us-west-2" # Required

# [llm] #AZURE OPENAI:
# api_type= 'azure'
Expand Down