agent.py aktualisiert
This commit is contained in:
@@ -1,43 +1,53 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from config import OPENROUTER_MODEL
|
from llm.base import LLMBase
|
||||||
from openrouter_client import client
|
from mcp.client import MCPClient
|
||||||
from mcp_client import MCPClient
|
|
||||||
|
|
||||||
|
|
||||||
class Agent:
|
class Agent:
|
||||||
def __init__(self):
|
def __init__(self, llm: LLMBase):
|
||||||
|
self.llm = llm
|
||||||
self.mcp = MCPClient()
|
self.mcp = MCPClient()
|
||||||
|
|
||||||
self.tools = []
|
self.tools = []
|
||||||
self.messages = [
|
self.messages = [
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": (
|
"content": (
|
||||||
"Du bist N.O.R.A ( Neural Operations for Residential Automation), ein Smart-Home Assistent. Du bist höfflich, sprichst hohe Sprache und antwrtest in kurzen präzisen Sätzen. Auser es wird eine lange Erklärung erbittet. Sei sympatisch und leicht zynisch und ironisch\n"
|
"Du bist JARVIS, ein Smart-Home Assistent. "
|
||||||
"Du steuerst ein Haus über MCP Tools.\n"
|
"Du steuerst ein Haus über MCP Tools. "
|
||||||
"Antworte normal kurz und präzise oder nutze Tools wenn nötig."
|
"Wenn nötig, verwende Tools."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
async def load_tools(self):
|
async def load_tools(self):
|
||||||
|
"""
|
||||||
|
Holt MCP Tools und konvertiert sie für Amazon Nova (Bedrock Converse API)
|
||||||
|
"""
|
||||||
|
|
||||||
res = await self.mcp.list_tools()
|
res = await self.mcp.list_tools()
|
||||||
|
|
||||||
self.tools = []
|
self.tools = []
|
||||||
|
|
||||||
for t in res.tools:
|
for t in res.tools:
|
||||||
self.tools.append({
|
self.tools.append({
|
||||||
"type": "function",
|
"toolSpec": {
|
||||||
"function": {
|
|
||||||
"name": t.name,
|
"name": t.name,
|
||||||
"description": t.description,
|
"description": t.description,
|
||||||
"parameters": t.inputSchema
|
"inputSchema": {
|
||||||
|
"json": t.inputSchema
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
print(f"[JARVIS] {len(self.tools)} Tools geladen")
|
print(f"[JARVIS] {len(self.tools)} Tools geladen")
|
||||||
async def _run_tool(self, tool_call):
|
async def _run_tool(self, tool_use):
|
||||||
name = tool_call.function.name
|
"""
|
||||||
args = json.loads(tool_call.function.arguments or "{}")
|
Führt MCP Tool aus
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = tool_use["name"]
|
||||||
|
args = tool_use.get("input", {})
|
||||||
|
|
||||||
result = await self.mcp.call_tool(name, args)
|
result = await self.mcp.call_tool(name, args)
|
||||||
|
|
||||||
@@ -51,42 +61,64 @@ class Agent:
|
|||||||
|
|
||||||
for _ in range(8):
|
for _ in range(8):
|
||||||
|
|
||||||
response = await client.chat.completions.create(
|
response = await self.llm.chat(
|
||||||
model=OPENROUTER_MODEL,
|
|
||||||
messages=self.messages,
|
messages=self.messages,
|
||||||
tools=self.tools
|
tools=self.tools
|
||||||
)
|
)
|
||||||
|
|
||||||
message = response.choices[0].message
|
# 🧠 Bedrock Nova Response parsing
|
||||||
|
output = response.get("output", {})
|
||||||
|
|
||||||
# 🧠 TOOL CALL PATH
|
message = output.get("message", {})
|
||||||
if getattr(message, "tool_calls", None):
|
|
||||||
|
|
||||||
self.messages.append(message)
|
content = message.get("content", [])
|
||||||
|
tool_uses = []
|
||||||
|
|
||||||
for tool_call in message.tool_calls:
|
for c in content:
|
||||||
|
if "toolUse" in c:
|
||||||
result = await self._run_tool(tool_call)
|
tool_uses.append(c["toolUse"])
|
||||||
|
if tool_uses:
|
||||||
self.messages.append({
|
|
||||||
"role": "tool",
|
|
||||||
"tool_call_id": tool_call.id,
|
|
||||||
"content": str(result)
|
|
||||||
})
|
|
||||||
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 🧠 NORMAL RESPONSE PATH
|
|
||||||
if message.content:
|
|
||||||
|
|
||||||
self.messages.append({
|
self.messages.append({
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
"content": message.content
|
"content": content
|
||||||
})
|
})
|
||||||
|
|
||||||
return message.content
|
tool_results = []
|
||||||
|
|
||||||
# 🧠 SAFETY FALLBACK
|
for tool in tool_uses:
|
||||||
return "Ich konnte keine gültige Antwort generieren."
|
|
||||||
|
|
||||||
return "Tool Loop Limit erreicht."
|
result = await self._run_tool(tool)
|
||||||
|
|
||||||
|
tool_results.append({
|
||||||
|
"toolResult": {
|
||||||
|
"toolUseId": tool["toolUseId"],
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"text": str(result)
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
self.messages.append({
|
||||||
|
"role": "user",
|
||||||
|
"content": tool_results
|
||||||
|
})
|
||||||
|
|
||||||
|
continue
|
||||||
|
final_text = ""
|
||||||
|
|
||||||
|
for c in content:
|
||||||
|
if "text" in c:
|
||||||
|
final_text += c["text"]
|
||||||
|
|
||||||
|
if final_text:
|
||||||
|
self.messages.append({
|
||||||
|
"role": "assistant",
|
||||||
|
"content": content
|
||||||
|
})
|
||||||
|
|
||||||
|
return final_text
|
||||||
|
|
||||||
|
return "Tool Loop Limit erreicht"
|
||||||
Reference in New Issue
Block a user