Skip to content
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

[ref] use one method to get boto client for aws bedrock #11506

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import boto3
from botocore.config import Config


def get_bedrock_client(service_name, credentials=None):
client_config = Config(region_name=credentials["aws_region"])
aws_access_key_id = (credentials["aws_access_key_id"],)
aws_secret_access_key = credentials["aws_secret_access_key"]
if aws_access_key_id and aws_secret_access_key:
# 使用 AKSK 方式
crazywoola marked this conversation as resolved.
Show resolved Hide resolved
client = boto3.client(
service_name=service_name,
config=client_config,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
)
else:
# 使用 IAM 角色方式
crazywoola marked this conversation as resolved.
Show resolved Hide resolved
client = boto3.client(service_name=service_name, config=client_config)

return client
9 changes: 2 additions & 7 deletions api/core/model_runtime/model_providers/bedrock/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# 3rd import
import boto3
from api.core.model_runtime.model_providers.bedrock.get_bedrock_client import get_bedrock_client
from botocore.config import Config
from botocore.exceptions import (
ClientError,
Expand Down Expand Up @@ -173,13 +174,7 @@ def _generate_with_converse(
:param stream: is stream response
:return: full response or stream response chunk generator result
"""
bedrock_client = boto3.client(
service_name="bedrock-runtime",
aws_access_key_id=credentials.get("aws_access_key_id"),
aws_secret_access_key=credentials.get("aws_secret_access_key"),
region_name=credentials["aws_region"],
)

bedrock_client = get_bedrock_client("bedrock-runtime", credentials)
system, prompt_message_dicts = self._convert_converse_prompt_messages(prompt_messages)
inference_config, additional_model_fields = self._convert_converse_api_model_parameters(model_parameters, stop)

Expand Down
11 changes: 2 additions & 9 deletions api/core/model_runtime/model_providers/bedrock/rerank/rerank.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional

import boto3
from botocore.config import Config
from api.core.model_runtime.model_providers.bedrock.get_bedrock_client import get_bedrock_client

from core.model_runtime.entities.rerank_entities import RerankDocument, RerankResult
from core.model_runtime.errors.invoke import (
Expand Down Expand Up @@ -48,13 +47,7 @@ def _invoke(
return RerankResult(model=model, docs=docs)

# initialize client
client_config = Config(region_name=credentials["aws_region"])
bedrock_runtime = boto3.client(
service_name="bedrock-agent-runtime",
config=client_config,
aws_access_key_id=credentials.get("aws_access_key_id", ""),
aws_secret_access_key=credentials.get("aws_secret_access_key"),
)
bedrock_runtime = get_bedrock_client("bedrock-agent-runtime", credentials)
queries = [{"type": "TEXT", "textQuery": {"text": query}}]
text_sources = []
for text in docs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import time
from typing import Optional

import boto3
from botocore.config import Config
from api.core.model_runtime.model_providers.bedrock.get_bedrock_client import get_bedrock_client
from botocore.exceptions import (
ClientError,
EndpointConnectionError,
Expand Down Expand Up @@ -48,14 +47,7 @@ def _invoke(
:param input_type: input type
:return: embeddings result
"""
client_config = Config(region_name=credentials["aws_region"])

bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
config=client_config,
aws_access_key_id=credentials.get("aws_access_key_id"),
aws_secret_access_key=credentials.get("aws_secret_access_key"),
)
bedrock_runtime = get_bedrock_client("bedrock-runtime", credentials)

embeddings = []
token_usage = 0
Expand Down
Loading