llm/bedrock.py aktualisiert
This commit is contained in:
+35
-14
@@ -1,14 +1,20 @@
|
|||||||
|
# llm/bedrock.py
|
||||||
import boto3
|
import boto3
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import time
|
||||||
from config import AWS_REGION, BEDROCK_MODEL
|
from config import AWS_REGION, BEDROCK_MODEL
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
|
from util.logger import get_logger
|
||||||
|
|
||||||
|
logger = get_logger("BEDROCK")
|
||||||
|
|
||||||
|
|
||||||
class BedrockClient:
|
class BedrockClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.client = boto3.client(
|
self.client = boto3.client(
|
||||||
"bedrock-runtime",
|
"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):
|
async def chat(self, messages, tools=None):
|
||||||
@@ -16,27 +22,42 @@ class BedrockClient:
|
|||||||
"modelId": BEDROCK_MODEL,
|
"modelId": BEDROCK_MODEL,
|
||||||
"messages": messages,
|
"messages": messages,
|
||||||
"inferenceConfig": {
|
"inferenceConfig": {
|
||||||
"maxTokens": 1000,
|
"maxTokens": 800,
|
||||||
"temperature": 0.7,
|
"temperature": 0.6,
|
||||||
"topP": 0.9
|
"topP": 0.95
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tools:
|
if tools and len(tools) > 0:
|
||||||
request["toolConfig"] = {
|
request["toolConfig"] = {"tools": tools}
|
||||||
"tools": tools
|
|
||||||
}
|
start_time = time.time()
|
||||||
|
logger.info(f"→ Bedrock Aufruf gestartet (Model: {BEDROCK_MODEL})")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Synchronen boto3-Aufruf in Executor auslagern
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
response = await loop.run_in_executor(
|
|
||||||
None,
|
# Mit Timeout (15 Sekunden)
|
||||||
lambda: self.client.converse(**request)
|
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
|
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:
|
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:
|
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