agent.py aktualisiert
This commit is contained in:
@@ -1,32 +1,29 @@
|
|||||||
import json
|
from llm.bedrock import BedrockClient
|
||||||
|
from mcp.client import MCPClient
|
||||||
from llm.base import LLMBase
|
|
||||||
from client import MCPClient
|
|
||||||
from config import MCP_URL
|
from config import MCP_URL
|
||||||
|
from util.logger import get_logger
|
||||||
|
|
||||||
|
|
||||||
class Agent:
|
class Agent:
|
||||||
def __init__(self, llm: LLMBase):
|
def __init__(self):
|
||||||
self.llm = llm
|
self.logger = get_logger("NORA")
|
||||||
|
|
||||||
|
self.llm = BedrockClient()
|
||||||
self.mcp = MCPClient(MCP_URL)
|
self.mcp = MCPClient(MCP_URL)
|
||||||
|
|
||||||
self.tools = []
|
self.tools = []
|
||||||
self.messages = [
|
self.messages = [
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": (
|
"content": [
|
||||||
"Du bist JARVIS, ein Smart-Home Assistent. "
|
{
|
||||||
"Du steuerst ein Haus über MCP Tools. "
|
"text": "Du bist N.O.R.A, ein Smart-Home Assistent. Nutze Tools wenn nötig."
|
||||||
"Wenn nötig, verwende Tools."
|
}
|
||||||
)
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
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()
|
res = await self.mcp.list_tools()
|
||||||
|
|
||||||
self.tools = []
|
self.tools = []
|
||||||
@@ -36,29 +33,27 @@ class Agent:
|
|||||||
"toolSpec": {
|
"toolSpec": {
|
||||||
"name": t.name,
|
"name": t.name,
|
||||||
"description": t.description,
|
"description": t.description,
|
||||||
"inputSchema": {
|
"inputSchema": t.inputSchema
|
||||||
"json": t.inputSchema
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
print(f"[JARVIS] {len(self.tools)} Tools geladen")
|
self.logger.info(f"{len(self.tools)} Tools geladen")
|
||||||
async def _run_tool(self, tool_use):
|
|
||||||
"""
|
|
||||||
Führt MCP Tool aus
|
|
||||||
"""
|
|
||||||
|
|
||||||
name = tool_use["name"]
|
async def _run_tool(self, tool):
|
||||||
args = tool_use.get("input", {})
|
name = tool["name"]
|
||||||
|
args = tool.get("input", {})
|
||||||
|
|
||||||
|
self.logger.info(f"Tool Call: {name} -> {args}")
|
||||||
|
|
||||||
result = await self.mcp.call_tool(name, args)
|
result = await self.mcp.call_tool(name, args)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def run(self, user_input: str):
|
async def run(self, user_input: str):
|
||||||
|
|
||||||
self.messages.append({
|
self.messages.append({
|
||||||
"role": "user",
|
"role": "user",
|
||||||
"content": user_input
|
"content": [{"text": user_input}]
|
||||||
})
|
})
|
||||||
|
|
||||||
for _ in range(8):
|
for _ in range(8):
|
||||||
@@ -68,17 +63,21 @@ class Agent:
|
|||||||
tools=self.tools
|
tools=self.tools
|
||||||
)
|
)
|
||||||
|
|
||||||
# 🧠 Bedrock Nova Response parsing
|
|
||||||
output = response.get("output", {})
|
output = response.get("output", {})
|
||||||
|
|
||||||
message = output.get("message", {})
|
message = output.get("message", {})
|
||||||
|
|
||||||
content = message.get("content", [])
|
content = message.get("content", [])
|
||||||
|
|
||||||
tool_uses = []
|
tool_uses = []
|
||||||
|
final_text = ""
|
||||||
|
|
||||||
for c in content:
|
for c in content:
|
||||||
if "toolUse" in c:
|
tool = c.get("toolUse")
|
||||||
tool_uses.append(c["toolUse"])
|
if tool:
|
||||||
|
tool_uses.append(tool)
|
||||||
|
|
||||||
|
if "text" in c:
|
||||||
|
final_text += c["text"]
|
||||||
|
|
||||||
if tool_uses:
|
if tool_uses:
|
||||||
|
|
||||||
self.messages.append({
|
self.messages.append({
|
||||||
@@ -89,7 +88,6 @@ class Agent:
|
|||||||
tool_results = []
|
tool_results = []
|
||||||
|
|
||||||
for tool in tool_uses:
|
for tool in tool_uses:
|
||||||
|
|
||||||
result = await self._run_tool(tool)
|
result = await self._run_tool(tool)
|
||||||
|
|
||||||
tool_results.append({
|
tool_results.append({
|
||||||
@@ -109,16 +107,11 @@ class Agent:
|
|||||||
})
|
})
|
||||||
|
|
||||||
continue
|
continue
|
||||||
final_text = ""
|
|
||||||
|
|
||||||
for c in content:
|
|
||||||
if "text" in c:
|
|
||||||
final_text += c["text"]
|
|
||||||
|
|
||||||
if final_text:
|
if final_text:
|
||||||
self.messages.append({
|
self.messages.append({
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
"content": content
|
"content": [{"text": final_text}]
|
||||||
})
|
})
|
||||||
|
|
||||||
return final_text
|
return final_text
|
||||||
|
|||||||
Reference in New Issue
Block a user