Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,7 @@ import asyncio, json, os, sys
|
|
| 5 |
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 |
-
from langchain.agents import create_tool_calling_agent
|
| 10 |
-
from langchain.tools import convert_to_openai_tool
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
|
@@ -30,7 +28,7 @@ class MCPClient:
|
|
| 30 |
"params": params or {},
|
| 31 |
"id": self._req_id,
|
| 32 |
}
|
| 33 |
-
self.process.stdin.write(json.dumps(request).encode() + b
|
| 34 |
await self.process.stdin.drain()
|
| 35 |
|
| 36 |
while line := await self.process.stdout.readline():
|
|
@@ -52,7 +50,7 @@ class MCPClient:
|
|
| 52 |
Tool(
|
| 53 |
name=s['name'],
|
| 54 |
description=s['description'],
|
| 55 |
-
func=None,
|
| 56 |
coroutine=self._create_tool_coro(s['name']),
|
| 57 |
args_schema=s['args_schema']
|
| 58 |
)
|
|
@@ -62,8 +60,7 @@ class MCPClient:
|
|
| 62 |
def _create_tool_coro(self, tool_name: str):
|
| 63 |
async def _tool_coro(tool_input):
|
| 64 |
return await self._send_request(
|
| 65 |
-
"execute",
|
| 66 |
-
{"tool_name": tool_name, "tool_args": tool_input}
|
| 67 |
)
|
| 68 |
return _tool_coro
|
| 69 |
|
|
@@ -78,25 +75,20 @@ async def get_agent_executor():
|
|
| 78 |
raise ValueError("GROQ_API_KEY secret not set.")
|
| 79 |
|
| 80 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Convert to OpenAI-compatible structured tools (CRITICAL FIX)
|
| 84 |
-
tools = [convert_to_openai_tool(t) for t in tools_raw]
|
| 85 |
|
| 86 |
model = ChatGroq(model="openai/gpt-oss-20b")
|
| 87 |
-
|
| 88 |
-
#
|
| 89 |
_agent_executor = create_react_agent(model, tools)
|
| 90 |
|
| 91 |
return _agent_executor
|
| 92 |
|
| 93 |
|
| 94 |
-
|
| 95 |
# --- Gradio Chat Logic ---
|
| 96 |
async def respond_to_chat(message: str, history: list):
|
| 97 |
agent = await get_agent_executor()
|
| 98 |
|
| 99 |
-
# Convert history to LangChain format
|
| 100 |
history_langchain_format = []
|
| 101 |
for human, ai in history:
|
| 102 |
history_langchain_format.append({"role": "user", "content": human})
|
|
@@ -112,7 +104,7 @@ async def respond_to_chat(message: str, history: list):
|
|
| 112 |
return "Sorry, an error occurred while processing your request."
|
| 113 |
|
| 114 |
|
| 115 |
-
# ---
|
| 116 |
demo = gr.ChatInterface(
|
| 117 |
fn=respond_to_chat,
|
| 118 |
title="Gold & Silver AI Forecast",
|
|
@@ -123,7 +115,5 @@ demo = gr.ChatInterface(
|
|
| 123 |
]
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
# --- Start the App ---
|
| 128 |
if __name__ == "__main__":
|
| 129 |
demo.launch()
|
|
|
|
| 5 |
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 # Keep this (works fine)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
|
|
| 28 |
"params": params or {},
|
| 29 |
"id": self._req_id,
|
| 30 |
}
|
| 31 |
+
self.process.stdin.write(json.dumps(request).encode() + b"\n")
|
| 32 |
await self.process.stdin.drain()
|
| 33 |
|
| 34 |
while line := await self.process.stdout.readline():
|
|
|
|
| 50 |
Tool(
|
| 51 |
name=s['name'],
|
| 52 |
description=s['description'],
|
| 53 |
+
func=None,
|
| 54 |
coroutine=self._create_tool_coro(s['name']),
|
| 55 |
args_schema=s['args_schema']
|
| 56 |
)
|
|
|
|
| 60 |
def _create_tool_coro(self, tool_name: str):
|
| 61 |
async def _tool_coro(tool_input):
|
| 62 |
return await self._send_request(
|
| 63 |
+
"execute", {"tool_name": tool_name, "tool_args": tool_input}
|
|
|
|
| 64 |
)
|
| 65 |
return _tool_coro
|
| 66 |
|
|
|
|
| 75 |
raise ValueError("GROQ_API_KEY secret not set.")
|
| 76 |
|
| 77 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 78 |
+
tools = await client.get_tools()
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
model = ChatGroq(model="openai/gpt-oss-20b")
|
| 81 |
+
|
| 82 |
+
# Keep create_react_agent (compatible with your environment)
|
| 83 |
_agent_executor = create_react_agent(model, tools)
|
| 84 |
|
| 85 |
return _agent_executor
|
| 86 |
|
| 87 |
|
|
|
|
| 88 |
# --- Gradio Chat Logic ---
|
| 89 |
async def respond_to_chat(message: str, history: list):
|
| 90 |
agent = await get_agent_executor()
|
| 91 |
|
|
|
|
| 92 |
history_langchain_format = []
|
| 93 |
for human, ai in history:
|
| 94 |
history_langchain_format.append({"role": "user", "content": human})
|
|
|
|
| 104 |
return "Sorry, an error occurred while processing your request."
|
| 105 |
|
| 106 |
|
| 107 |
+
# --- UI ---
|
| 108 |
demo = gr.ChatInterface(
|
| 109 |
fn=respond_to_chat,
|
| 110 |
title="Gold & Silver AI Forecast",
|
|
|
|
| 115 |
]
|
| 116 |
)
|
| 117 |
|
|
|
|
|
|
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch()
|