Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import asyncio, json, os, sys
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain_core.tools import Tool
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 8 |
-
from langchain.agents import
|
|
|
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
|
| 11 |
load_dotenv()
|
|
@@ -29,9 +29,22 @@ class MCPClient:
|
|
| 29 |
raise ConnectionError("Server process closed unexpectedly.")
|
| 30 |
|
| 31 |
async def get_tools(self) -> list[Tool]:
|
| 32 |
-
self.process = await asyncio.create_subprocess_exec(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
tool_schemas = await self._send_request("discover")
|
| 34 |
-
return [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def _create_tool_coro(self, tool_name: str):
|
| 37 |
async def _tool_coro(tool_input):
|
|
@@ -44,27 +57,42 @@ _agent_executor = None
|
|
| 44 |
async def get_agent_executor():
|
| 45 |
global _agent_executor
|
| 46 |
if _agent_executor is None:
|
| 47 |
-
if not os.getenv("GROQ_API_KEY"):
|
|
|
|
| 48 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 49 |
tools = await client.get_tools()
|
| 50 |
model = ChatGroq(model="llama3-groq-70b-8192-tool-use-preview", temperature=0)
|
|
|
|
| 51 |
prompt = ChatPromptTemplate.from_messages([
|
| 52 |
("system", "You are a helpful assistant for gold and silver price information and forecasts."),
|
| 53 |
MessagesPlaceholder(variable_name="chat_history", optional=True),
|
| 54 |
("human", "{input}"),
|
| 55 |
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 56 |
])
|
|
|
|
|
|
|
| 57 |
agent = create_tool_calling_agent(llm=model, tools=tools, prompt=prompt)
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return _agent_executor
|
| 60 |
|
| 61 |
# --- Chat Handler ---
|
| 62 |
async def respond_to_chat(message: str, history: list):
|
| 63 |
agent = await get_agent_executor()
|
| 64 |
-
chat_history = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
try:
|
| 66 |
response = await agent.ainvoke({"input": message, "chat_history": chat_history})
|
| 67 |
-
return response[
|
| 68 |
except Exception as e:
|
| 69 |
print(f"ERROR: {e}", file=sys.stderr)
|
| 70 |
return "Sorry, an error occurred while processing your request."
|
|
@@ -78,4 +106,4 @@ demo = gr.ChatInterface(
|
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import asyncio, json, os, sys
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langchain_core.tools import Tool
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 6 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 7 |
+
from langchain.agents import create_tool_calling_agent
|
| 8 |
+
from langchain.agents.agent_executor import AgentExecutor
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
|
| 11 |
load_dotenv()
|
|
|
|
| 29 |
raise ConnectionError("Server process closed unexpectedly.")
|
| 30 |
|
| 31 |
async def get_tools(self) -> list[Tool]:
|
| 32 |
+
self.process = await asyncio.create_subprocess_exec(
|
| 33 |
+
*self._cmd,
|
| 34 |
+
stdin=asyncio.subprocess.PIPE,
|
| 35 |
+
stdout=asyncio.subprocess.PIPE
|
| 36 |
+
)
|
| 37 |
tool_schemas = await self._send_request("discover")
|
| 38 |
+
return [
|
| 39 |
+
Tool(
|
| 40 |
+
name=s['name'],
|
| 41 |
+
description=s['description'],
|
| 42 |
+
func=None,
|
| 43 |
+
coroutine=self._create_tool_coro(s['name']),
|
| 44 |
+
args_schema=s['args_schema']
|
| 45 |
+
)
|
| 46 |
+
for s in tool_schemas
|
| 47 |
+
]
|
| 48 |
|
| 49 |
def _create_tool_coro(self, tool_name: str):
|
| 50 |
async def _tool_coro(tool_input):
|
|
|
|
| 57 |
async def get_agent_executor():
|
| 58 |
global _agent_executor
|
| 59 |
if _agent_executor is None:
|
| 60 |
+
if not os.getenv("GROQ_API_KEY"):
|
| 61 |
+
raise ValueError("GROQ_API_KEY not set.")
|
| 62 |
client = MCPClient(command=sys.executable, args=["server.py"])
|
| 63 |
tools = await client.get_tools()
|
| 64 |
model = ChatGroq(model="llama3-groq-70b-8192-tool-use-preview", temperature=0)
|
| 65 |
+
|
| 66 |
prompt = ChatPromptTemplate.from_messages([
|
| 67 |
("system", "You are a helpful assistant for gold and silver price information and forecasts."),
|
| 68 |
MessagesPlaceholder(variable_name="chat_history", optional=True),
|
| 69 |
("human", "{input}"),
|
| 70 |
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 71 |
])
|
| 72 |
+
|
| 73 |
+
# 🔧 only this line changed from create_openai_functions_agent(...)
|
| 74 |
agent = create_tool_calling_agent(llm=model, tools=tools, prompt=prompt)
|
| 75 |
+
|
| 76 |
+
_agent_executor = AgentExecutor(
|
| 77 |
+
agent=agent,
|
| 78 |
+
tools=tools,
|
| 79 |
+
verbose=False,
|
| 80 |
+
handle_parsing_errors=True,
|
| 81 |
+
max_iterations=5,
|
| 82 |
+
)
|
| 83 |
return _agent_executor
|
| 84 |
|
| 85 |
# --- Chat Handler ---
|
| 86 |
async def respond_to_chat(message: str, history: list):
|
| 87 |
agent = await get_agent_executor()
|
| 88 |
+
chat_history = [
|
| 89 |
+
msg
|
| 90 |
+
for human, ai in history
|
| 91 |
+
for msg in (HumanMessage(content=human), AIMessage(content=ai))
|
| 92 |
+
]
|
| 93 |
try:
|
| 94 |
response = await agent.ainvoke({"input": message, "chat_history": chat_history})
|
| 95 |
+
return response["output"]
|
| 96 |
except Exception as e:
|
| 97 |
print(f"ERROR: {e}", file=sys.stderr)
|
| 98 |
return "Sorry, an error occurred while processing your request."
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.launch()
|