Update app.py
Browse files4 unmodified lines567891011121316 unmodified lines3031323334353615 unmodified lines525354555657583 unmodified lines62636465666768698 unmodified lines787980818283848586878889909192939495969798991001011029 unmodified lines1121131141151161171184 unmodified lines1231241251261271281294 unmodified linesfrom dotenv import load_dotenvfrom langchain_core.tools import Toolfrom langchain_groq import ChatGroqfrom langgraph.prebuilt import create_react_agentfrom langchain.agents import create_tool_calling_agentfrom langchain.tools import convert_to_openai_toolload_dotenv()16 unmodified lines"params": params or {},"id": self._req_id,}self.process.stdin.write(json.dumps(request).encode() + b'\n')await self.process.stdin.drain()while line := await self.process.stdout.readline():15 unmodified linesTool(name=s['name'],description=s['description'],func=None, # <-- CRITICAL FIX: Add func=None for async toolscoroutine=self._create_tool_coro(s['name']),args_schema=s['args_schema'])3 unmodified linesdef _create_tool_coro(self, tool_name: str):async def _tool_coro(tool_input):return await self._send_request("execute",{"tool_name": tool_name, "tool_args": tool_input})return _tool_coro8 unmodified linesraise ValueError("GROQ_API_KEY secret not set.")client = MCPClient(command=sys.executable, args=["server.py"])tools_raw = await client.get_tools()# Convert to OpenAI-compatible structured tools (CRITICAL FIX)tools = [convert_to_openai_tool(t) for t in tools_raw]model = ChatGroq(model="openai/gpt-oss-20b")# Stick to create_react_agent (compatible with your LangChain version)_agent_executor = create_react_agent(model, tools)return _agent_executor# --- Gradio Chat Logic ---async def respond_to_chat(message: str, history: list):agent = await get_agent_executor()# Convert history to LangChain formathistory_langchain_format = []for human, ai in history:history_langchain_format.append({"role": "user", "content": human})9 unmodified linesreturn "Sorry, an error occurred while processing your request."# --- Gradio User Interface ---demo = gr.ChatInterface(fn=respond_to_chat,title="Gold & Silver AI Forecast",4 unmodified lines])# --- Start the App ---if __name__ == "__main__":demo.launch()4 unmodified lines56789101116 unmodified lines2829303132333415 unmodified lines505152535455563 unmodified lines606162636465668 unmodified lines75767778798081828384858687888990919293949 unmodified lines1041051061071081091104 unmodified lines1151161171181194 unmodified linesfrom dotenv import load_dotenvfrom langchain_core.tools import Toolfrom langchain_groq import ChatGroqfrom langgraph.prebuilt import create_react_agent # Keep this (works fine)load_dotenv()16 unmodified lines"params": params or {},"id": self._req_id,}self.process.stdin.write(json.dumps(request).encode() + b"\n")await self.process.stdin.drain()while line := await self.process.stdout.readline():15 unmodified linesTool(name=s['name'],description=s['description'],func=None,coroutine=self._create_tool_coro(s['name']),args_schema=s['args_schema'])3 unmodified linesdef _create_tool_coro(self, tool_name: str):async def _tool_coro(tool_input):return await self._send_request("execute", {"tool_name": tool_name, "tool_args": tool_input})return _tool_coro8 unmodified linesraise ValueError("GROQ_API_KEY secret not set.")client = MCPClient(command=sys.executable, args=["server.py"])tools = await client.get_tools()model = ChatGroq(model="openai/gpt-oss-20b")# Keep create_react_agent (compatible with your environment)_agent_executor = create_react_agent(model, tools)return _agent_executor# --- Gradio Chat Logic ---async def respond_to_chat(message: str, history: list):agent = await get_agent_executor()history_langchain_format = []for human, ai in history:history_langchain_format.append({"role": "user", "content": human})9 unmodified linesreturn "Sorry, an error occurred while processing your request."# --- UI ---demo = gr.ChatInterface(fn=respond_to_chat,title="Gold & Silver AI Forecast",4 unmodified lines])if __name__ == "__main__":demo.launch()
app.py+8-18