Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ from dotenv import load_dotenv
|
|
| 6 |
from langchain_core.tools import Tool
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langgraph.prebuilt import create_react_agent
|
|
|
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
@@ -70,18 +72,24 @@ class MCPClient:
|
|
| 70 |
_agent_executor = None
|
| 71 |
|
| 72 |
async def get_agent_executor():
|
| 73 |
-
"""Initializes and returns the agent executor, ensuring it's a singleton."""
|
| 74 |
global _agent_executor
|
| 75 |
if _agent_executor is None:
|
| 76 |
if not os.getenv("GROQ_API_KEY"):
|
| 77 |
raise ValueError("GROQ_API_KEY secret not set.")
|
|
|
|
| 78 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
model = ChatGroq(model="openai/gpt-oss-20b")
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
return _agent_executor
|
| 83 |
|
| 84 |
|
|
|
|
| 85 |
# --- Gradio Chat Logic ---
|
| 86 |
async def respond_to_chat(message: str, history: list):
|
| 87 |
agent = await get_agent_executor()
|
|
|
|
| 6 |
from langchain_core.tools import Tool
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langgraph.prebuilt import create_react_agent
|
| 9 |
+
from langchain.agents import create_tool_calling_agent
|
| 10 |
+
from langchain.tools import convert_to_openai_tool
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
|
|
|
| 72 |
_agent_executor = None
|
| 73 |
|
| 74 |
async def get_agent_executor():
|
|
|
|
| 75 |
global _agent_executor
|
| 76 |
if _agent_executor is None:
|
| 77 |
if not os.getenv("GROQ_API_KEY"):
|
| 78 |
raise ValueError("GROQ_API_KEY secret not set.")
|
| 79 |
+
|
| 80 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 81 |
+
|
| 82 |
+
tools_raw = await client.get_tools()
|
| 83 |
+
tools = [convert_to_openai_tool(t) for t in tools_raw] # 🟢 Key Fix
|
| 84 |
+
|
| 85 |
model = ChatGroq(model="openai/gpt-oss-20b")
|
| 86 |
+
|
| 87 |
+
_agent_executor = create_tool_calling_agent(model, tools) # 🟢 Key Fix
|
| 88 |
+
|
| 89 |
return _agent_executor
|
| 90 |
|
| 91 |
|
| 92 |
+
|
| 93 |
# --- Gradio Chat Logic ---
|
| 94 |
async def respond_to_chat(message: str, history: list):
|
| 95 |
agent = await get_agent_executor()
|