llm/bedrock.py aktualisiert
This commit is contained in:
+20
-3
@@ -1,6 +1,7 @@
|
||||
import boto3
|
||||
|
||||
import asyncio
|
||||
from config import AWS_REGION, BEDROCK_MODEL
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
|
||||
class BedrockClient:
|
||||
@@ -11,10 +12,14 @@ class BedrockClient:
|
||||
)
|
||||
|
||||
async def chat(self, messages, tools=None):
|
||||
|
||||
request = {
|
||||
"modelId": BEDROCK_MODEL,
|
||||
"messages": messages,
|
||||
"inferenceConfig": {
|
||||
"maxTokens": 1000,
|
||||
"temperature": 0.7,
|
||||
"topP": 0.9
|
||||
}
|
||||
}
|
||||
|
||||
if tools:
|
||||
@@ -22,4 +27,16 @@ class BedrockClient:
|
||||
"tools": tools
|
||||
}
|
||||
|
||||
return self.client.converse(**request)
|
||||
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
|
||||
Reference in New Issue
Block a user