42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import boto3
|
|
import asyncio
|
|
from config import AWS_REGION, BEDROCK_MODEL
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
|
class BedrockClient:
|
|
def __init__(self):
|
|
self.client = boto3.client(
|
|
"bedrock-runtime",
|
|
region_name=AWS_REGION
|
|
)
|
|
|
|
async def chat(self, messages, tools=None):
|
|
request = {
|
|
"modelId": BEDROCK_MODEL,
|
|
"messages": messages,
|
|
"inferenceConfig": {
|
|
"maxTokens": 1000,
|
|
"temperature": 0.7,
|
|
"topP": 0.9
|
|
}
|
|
}
|
|
|
|
if tools:
|
|
request["toolConfig"] = {
|
|
"tools": tools
|
|
}
|
|
|
|
try:
|
|
# Synchronen boto3-Aufruf in Executor auslagern
|
|
loop = asyncio.get_running_loop()
|
|
response = await loop.run_in_executor(
|
|
None,
|
|
lambda: self.client.converse(**request)
|
|
)
|
|
return response
|
|
|
|
except ClientError as e:
|
|
raise Exception(f"Bedrock API Error: {e}") from e
|
|
except Exception as e:
|
|
raise Exception(f"Unexpected error in Bedrock chat: {e}") from e |