agent.py aktualisiert

This commit is contained in:
2026-07-05 09:36:38 +00:00
parent 7830a45e0c
commit 75c4e8d7fa
+33 -40
View File
@@ -1,32 +1,29 @@
import json
from llm.base import LLMBase
from client import MCPClient
from llm.bedrock import BedrockClient
from mcp.client import MCPClient
from config import MCP_URL
from util.logger import get_logger
class Agent:
def __init__(self, llm: LLMBase):
self.llm = llm
def __init__(self):
self.logger = get_logger("NORA")
self.llm = BedrockClient()
self.mcp = MCPClient(MCP_URL)
self.tools = []
self.messages = [
{
"role": "system",
"content": (
"Du bist JARVIS, ein Smart-Home Assistent. "
"Du steuerst ein Haus über MCP Tools. "
"Wenn nötig, verwende Tools."
)
"content": [
{
"text": "Du bist N.O.R.A, ein Smart-Home Assistent. Nutze Tools wenn nötig."
}
]
}
]
async def load_tools(self):
"""
Holt MCP Tools und konvertiert sie für Amazon Nova (Bedrock Converse API)
"""
async def load_tools(self):
res = await self.mcp.list_tools()
self.tools = []
@@ -36,29 +33,27 @@ class Agent:
"toolSpec": {
"name": t.name,
"description": t.description,
"inputSchema": {
"json": t.inputSchema
}
"inputSchema": t.inputSchema
}
})
print(f"[JARVIS] {len(self.tools)} Tools geladen")
async def _run_tool(self, tool_use):
"""
Führt MCP Tool aus
"""
self.logger.info(f"{len(self.tools)} Tools geladen")
name = tool_use["name"]
args = tool_use.get("input", {})
async def _run_tool(self, tool):
name = tool["name"]
args = tool.get("input", {})
self.logger.info(f"Tool Call: {name} -> {args}")
result = await self.mcp.call_tool(name, args)
return result
async def run(self, user_input: str):
self.messages.append({
"role": "user",
"content": user_input
"content": [{"text": user_input}]
})
for _ in range(8):
@@ -68,17 +63,21 @@ class Agent:
tools=self.tools
)
# 🧠 Bedrock Nova Response parsing
output = response.get("output", {})
message = output.get("message", {})
content = message.get("content", [])
tool_uses = []
final_text = ""
for c in content:
if "toolUse" in c:
tool_uses.append(c["toolUse"])
tool = c.get("toolUse")
if tool:
tool_uses.append(tool)
if "text" in c:
final_text += c["text"]
if tool_uses:
self.messages.append({
@@ -89,7 +88,6 @@ class Agent:
tool_results = []
for tool in tool_uses:
result = await self._run_tool(tool)
tool_results.append({
@@ -108,17 +106,12 @@ class Agent:
"content": tool_results
})
continue
final_text = ""
for c in content:
if "text" in c:
final_text += c["text"]
continue
if final_text:
self.messages.append({
"role": "assistant",
"content": content
"content": [{"text": final_text}]
})
return final_text