Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,14 @@ import asyncio
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from langchain_mcp_adapters.client import MultiServerMCPClient
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# --- Initialize
|
| 7 |
agent_executor = None
|
| 8 |
|
| 9 |
async def initialize_agent():
|
| 10 |
-
from langchain.agents import initialize_agent, AgentType
|
| 11 |
-
from langchain.tools import Tool
|
| 12 |
-
|
| 13 |
print("π Initializing MCP Client and Tools...")
|
| 14 |
-
|
| 15 |
client = MultiServerMCPClient({
|
| 16 |
"price": {
|
| 17 |
"command": "python",
|
|
@@ -27,9 +25,6 @@ async def initialize_agent():
|
|
| 27 |
print(f"β
Tools loaded: {[t.name for t in tools]}")
|
| 28 |
|
| 29 |
llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0)
|
| 30 |
-
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 31 |
-
from langchain import hub
|
| 32 |
-
|
| 33 |
prompt = hub.pull("hwchase17/openai-tools-agent")
|
| 34 |
agent = create_openai_tools_agent(llm, tools, prompt)
|
| 35 |
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
|
@@ -40,11 +35,11 @@ async def initialize_agent():
|
|
| 40 |
async def metal_agent(message, history):
|
| 41 |
global agent_executor
|
| 42 |
if agent_executor is None:
|
| 43 |
-
return "β οΈ Agent not initialized. Please wait a
|
| 44 |
|
| 45 |
chat_history = []
|
|
|
|
| 46 |
for user_msg, ai_msg in history:
|
| 47 |
-
from langchain_core.messages import HumanMessage, AIMessage
|
| 48 |
chat_history.append(HumanMessage(content=user_msg))
|
| 49 |
chat_history.append(AIMessage(content=ai_msg))
|
| 50 |
|
|
@@ -52,15 +47,15 @@ async def metal_agent(message, history):
|
|
| 52 |
return result["output"]
|
| 53 |
|
| 54 |
|
| 55 |
-
# --- Launch
|
| 56 |
def launch_ui():
|
| 57 |
iface = gr.ChatInterface(
|
| 58 |
fn=metal_agent,
|
| 59 |
-
title="π° Metal Price Agent (Yahoo Finance + MCP
|
| 60 |
description="Ask for live & predicted gold/silver prices.",
|
| 61 |
examples=[
|
| 62 |
["Show me the report for gold"],
|
| 63 |
-
["What is the 3
|
| 64 |
]
|
| 65 |
)
|
| 66 |
iface.queue().launch(share=False)
|
|
@@ -69,7 +64,7 @@ def launch_ui():
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
try:
|
| 71 |
agent_executor = asyncio.run(initialize_agent())
|
| 72 |
-
print("β
Agent ready!")
|
| 73 |
launch_ui()
|
| 74 |
except Exception as e:
|
| 75 |
print(f"π₯ Initialization failed: {e}")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from langchain_mcp_adapters.client import MultiServerMCPClient
|
| 5 |
+
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 6 |
+
from langchain import hub
|
| 7 |
|
| 8 |
+
# --- Initialize MCP + Agent ---
|
| 9 |
agent_executor = None
|
| 10 |
|
| 11 |
async def initialize_agent():
|
|
|
|
|
|
|
|
|
|
| 12 |
print("π Initializing MCP Client and Tools...")
|
|
|
|
| 13 |
client = MultiServerMCPClient({
|
| 14 |
"price": {
|
| 15 |
"command": "python",
|
|
|
|
| 25 |
print(f"β
Tools loaded: {[t.name for t in tools]}")
|
| 26 |
|
| 27 |
llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0)
|
|
|
|
|
|
|
|
|
|
| 28 |
prompt = hub.pull("hwchase17/openai-tools-agent")
|
| 29 |
agent = create_openai_tools_agent(llm, tools, prompt)
|
| 30 |
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
|
|
|
| 35 |
async def metal_agent(message, history):
|
| 36 |
global agent_executor
|
| 37 |
if agent_executor is None:
|
| 38 |
+
return "β οΈ Agent not initialized yet. Please wait a moment."
|
| 39 |
|
| 40 |
chat_history = []
|
| 41 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 42 |
for user_msg, ai_msg in history:
|
|
|
|
| 43 |
chat_history.append(HumanMessage(content=user_msg))
|
| 44 |
chat_history.append(AIMessage(content=ai_msg))
|
| 45 |
|
|
|
|
| 47 |
return result["output"]
|
| 48 |
|
| 49 |
|
| 50 |
+
# --- Launch Gradio ---
|
| 51 |
def launch_ui():
|
| 52 |
iface = gr.ChatInterface(
|
| 53 |
fn=metal_agent,
|
| 54 |
+
title="π° Metal Price Agent (Yahoo Finance + MCP)",
|
| 55 |
description="Ask for live & predicted gold/silver prices.",
|
| 56 |
examples=[
|
| 57 |
["Show me the report for gold"],
|
| 58 |
+
["What is the 3-day forecast for silver?"],
|
| 59 |
]
|
| 60 |
)
|
| 61 |
iface.queue().launch(share=False)
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
try:
|
| 66 |
agent_executor = asyncio.run(initialize_agent())
|
| 67 |
+
print("β
Agent ready! Starting app...")
|
| 68 |
launch_ui()
|
| 69 |
except Exception as e:
|
| 70 |
print(f"π₯ Initialization failed: {e}")
|