reset coding_agent to non dynamic tool choice
This commit is contained in:
parent
582e0bd711
commit
4e0d3a7438
@ -24,10 +24,11 @@ from pathlib import Path
|
|||||||
import requests
|
import requests
|
||||||
import httpx
|
import httpx
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from backend.agent.mcp_server_adapter import MCPToolRAGAdapter
|
#from backend.agent.mcp_server_adapter import MCPToolRAGAdapter
|
||||||
|
from backend.agent.mcp_server_adapter import MCPToolAdapter
|
||||||
|
|
||||||
# ── mcp server initialization ────────────────────────────────────────────────────────────────
|
# ── mcp server initialization ────────────────────────────────────────────────────────────────
|
||||||
adapter = MCPToolRAGAdapter()
|
adapter = MCPToolAdapter()
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@ -193,10 +194,10 @@ MAX_HISTORY_CHARS = 80_000
|
|||||||
# return f"ERROR in {tool_name}: {type(e).__name__}: {e}"
|
# return f"ERROR in {tool_name}: {type(e).__name__}: {e}"
|
||||||
#
|
#
|
||||||
|
|
||||||
async def get_tools_for_prompt(query: str) -> str:
|
def build_all_tool_description() -> str:
|
||||||
"""Get relevant tools from the MCP servers based on the query."""
|
"""Get relevant tools from the MCP servers based on the query."""
|
||||||
|
|
||||||
relevant_tools = adapter.get_relevant_tools(query, top_k=3)
|
relevant_tools = adapter.get_all_tools()
|
||||||
|
|
||||||
descriptions = []
|
descriptions = []
|
||||||
for tool in relevant_tools:
|
for tool in relevant_tools:
|
||||||
@ -250,12 +251,16 @@ dynamic set of tools via the Model Context Protocol (MCP).
|
|||||||
|
|
||||||
<capabilities>
|
<capabilities>
|
||||||
You have access to a workspace where you can manage files,
|
You have access to a workspace where you can manage files,
|
||||||
analyze code, and execute Python scripts.
|
analyze code, and execute Python scripts.
|
||||||
Your available tools are provided dynamically based on your current needs.
|
You can call tools to interact with the workspace and get feedback.
|
||||||
|
You can write and read files, list directory contents, search for patterns,
|
||||||
|
validate Python syntax, and run Python code.
|
||||||
|
Wou can access web search and page fetching tools to gather information from the internet.
|
||||||
|
You can use these capabilities to iteratively work towards completing the user's task.
|
||||||
</capabilities>
|
</capabilities>
|
||||||
|
|
||||||
<tools>
|
<tools>
|
||||||
tool will be provided dynamically based on the agent's current context and needs.
|
{build_all_tool_description()}
|
||||||
</tools>
|
</tools>
|
||||||
|
|
||||||
|
|
||||||
@ -383,7 +388,9 @@ class CodingAgent:
|
|||||||
self.api_key = os.getenv("API_KEY")
|
self.api_key = os.getenv("API_KEY")
|
||||||
self.model = os.getenv("MODEL")
|
self.model = os.getenv("MODEL")
|
||||||
|
|
||||||
async def _call_api(self, messages: list) -> str:
|
#async def _call_api(self, messages: list) -> str:
|
||||||
|
def _call_api(self, messages: list) -> str:
|
||||||
|
|
||||||
"""Make a raw API call and return the response content string."""
|
"""Make a raw API call and return the response content string."""
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
@ -398,13 +405,13 @@ class CodingAgent:
|
|||||||
"stream": False,
|
"stream": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=30) as client:
|
#async with httpx.AsyncClient(timeout=30) as client:
|
||||||
response = await client.post(
|
# response = await client.post(
|
||||||
self.api_url,
|
# self.api_url,
|
||||||
headers=headers,
|
# headers=headers,
|
||||||
json=payload,
|
# json=payload,
|
||||||
timeout=60)
|
# timeout=60)
|
||||||
#response = requests.post(self.api_url, headers=headers, json=payload, timeout=60)
|
response = requests.post(self.api_url, headers=headers, json=payload, timeout=60)
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise Exception(f"API Error {response.status_code}: {response.text}")
|
raise Exception(f"API Error {response.status_code}: {response.text}")
|
||||||
@ -443,28 +450,28 @@ class CodingAgent:
|
|||||||
return {"thought": "Max iterations reached.", "tool": "done",
|
return {"thought": "Max iterations reached.", "tool": "done",
|
||||||
"arguments": {"summary": "Stopped: max iterations reached."}}
|
"arguments": {"summary": "Stopped: max iterations reached."}}
|
||||||
|
|
||||||
current_context = self.messages[-1]["content"] if self.messages else ""
|
#current_context = self.messages[-1]["content"] if self.messages else ""
|
||||||
self.iteration += 1
|
self.iteration += 1
|
||||||
self.messages = trim_messages(self.messages)
|
self.messages = trim_messages(self.messages)
|
||||||
|
|
||||||
tool_prompt = await get_tools_for_prompt(current_context)
|
#tool_prompt = await get_tools_for_prompt(current_context)
|
||||||
|
#
|
||||||
enhanced_messages = self.messages.copy()
|
#enhanced_messages = self.messages.copy()
|
||||||
|
#
|
||||||
enhanced_messages.append({
|
#enhanced_messages.append({
|
||||||
"role": "system",
|
# "role": "system",
|
||||||
"content": f"""
|
# "content": f"""
|
||||||
Available tools for this step:
|
#Available tools for this step:
|
||||||
|
#
|
||||||
{tool_prompt}
|
#{tool_prompt}
|
||||||
|
#
|
||||||
You MUST choose one of these tools.
|
#You MUST choose one of these tools.
|
||||||
"""
|
#"""
|
||||||
})
|
#})
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw = await self._call_api(enhanced_messages)
|
raw = await self._call_api(self.messages)
|
||||||
raw = _strip_code_fences(raw)
|
raw = _strip_code_fences(raw)
|
||||||
action = json.loads(raw)
|
action = json.loads(raw)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user