From de46e9182d4740db1418fd529a10155e65c742c7 Mon Sep 17 00:00:00 2001 From: Till Immer Date: Sun, 5 Jul 2026 10:02:07 +0000 Subject: [PATCH] llm/bedrock.py aktualisiert --- llm/bedrock.py | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/llm/bedrock.py b/llm/bedrock.py index 0df9069..37debc4 100644 --- a/llm/bedrock.py +++ b/llm/bedrock.py @@ -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 \ No newline at end of file + logger.error(f"❌ Unerwarteter Fehler bei Bedrock: {e}") + raise \ No newline at end of file