llm/bedrock.py aktualisiert
This commit is contained in:
+35
-14
@@ -1,14 +1,20 @@
|
||||
# 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
|
||||
region_name=AWS_REGION,
|
||||
# aws_access_key_id und aws_secret_access_key werden aus ENV geladen
|
||||
)
|
||||
|
||||
async def chat(self, messages, tools=None):
|
||||
@@ -16,27 +22,42 @@ class BedrockClient:
|
||||
"modelId": BEDROCK_MODEL,
|
||||
"messages": messages,
|
||||
"inferenceConfig": {
|
||||
"maxTokens": 1000,
|
||||
"temperature": 0.7,
|
||||
"topP": 0.9
|
||||
"maxTokens": 800,
|
||||
"temperature": 0.6,
|
||||
"topP": 0.95
|
||||
}
|
||||
}
|
||||
|
||||
if tools:
|
||||
request["toolConfig"] = {
|
||||
"tools": tools
|
||||
}
|
||||
if tools and len(tools) > 0:
|
||||
request["toolConfig"] = {"tools": tools}
|
||||
|
||||
start_time = time.time()
|
||||
logger.info(f"→ Bedrock Aufruf gestartet (Model: {BEDROCK_MODEL})")
|
||||
|
||||
try:
|
||||
# Synchronen boto3-Aufruf in Executor auslagern
|
||||
loop = asyncio.get_running_loop()
|
||||
response = await loop.run_in_executor(
|
||||
None,
|
||||
lambda: self.client.converse(**request)
|
||||
|
||||
# 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:
|
||||
raise Exception(f"Bedrock API Error: {e}") from 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:
|
||||
raise Exception(f"Unexpected error in Bedrock chat: {e}") from e
|
||||
logger.error(f"❌ Unerwarteter Fehler bei Bedrock: {e}")
|
||||
raise
|
||||
Reference in New Issue
Block a user