66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
# llm/bedrock.py
|
|
import boto3
|
|
import asyncio
|
|
import time
|
|
from config import AWS_REGION, BEDROCK_MODEL
|
|
from botocore.exceptions import ClientError
|
|
from util.logger import get_logger
|
|
|
|
logger = get_logger("BEDROCK")
|
|
|
|
|
|
class BedrockClient:
|
|
def __init__(self):
|
|
self.client = boto3.client(
|
|
"bedrock-runtime",
|
|
region_name=AWS_REGION,
|
|
# aws_access_key_id und aws_secret_access_key werden aus ENV geladen
|
|
)
|
|
|
|
async def chat(self, messages, tools=None):
|
|
request = {
|
|
"modelId": BEDROCK_MODEL,
|
|
"messages": messages,
|
|
"inferenceConfig": {
|
|
"maxTokens": 800,
|
|
"temperature": 0.6,
|
|
"topP": 0.95
|
|
}
|
|
}
|
|
|
|
if tools and len(tools) > 0:
|
|
request["toolConfig"] = {
|
|
"tools": tools,
|
|
"toolChoice": {"auto": {}} # oder "any" falls du Tool-Nutzung erzwingen willst
|
|
}
|
|
|
|
start_time = time.time()
|
|
logger.info(f"→ Bedrock Aufruf gestartet (Model: {BEDROCK_MODEL})")
|
|
|
|
try:
|
|
loop = asyncio.get_running_loop()
|
|
|
|
# Mit Timeout (15 Sekunden)
|
|
response = await asyncio.wait_for(
|
|
loop.run_in_executor(
|
|
None,
|
|
lambda: self.client.converse(**request)
|
|
),
|
|
timeout=15.0
|
|
)
|
|
|
|
duration = time.time() - start_time
|
|
logger.info(f"← Bedrock Antwort erhalten in {duration:.2f}s")
|
|
|
|
return response
|
|
|
|
except asyncio.TimeoutError:
|
|
logger.error("❌ Bedrock Timeout nach 15 Sekunden")
|
|
raise Exception("Bedrock-Anfrage hat zu lange gedauert (Timeout)")
|
|
except ClientError as e:
|
|
error_code = e.response['Error']['Code']
|
|
logger.error(f"❌ Bedrock ClientError: {error_code} - {e}")
|
|
raise Exception(f"Bedrock Fehler: {e}") from e
|
|
except Exception as e:
|
|
logger.error(f"❌ Unerwarteter Fehler bei Bedrock: {e}")
|
|
raise |