Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,29 @@
|
|
| 1 |
-
import os
|
| 2 |
import pathlib
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from mcp import StdioServerParameters
|
| 5 |
-
from smolagents import MCPClient
|
| 6 |
-
from smolagents.
|
| 7 |
|
| 8 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 9 |
|
| 10 |
-
# Build an OpenAI model object once, reuse for all requests
|
| 11 |
-
# Ensure OPENAI_API_KEY is set in your HF Space secrets
|
| 12 |
-
openai_model = OpenAIChatModel(model="gpt-4o") # or "gpt-4o-mini", "gpt-3.5-turbo"
|
| 13 |
-
# choose any model your key can access
|
| 14 |
|
| 15 |
def respond(message: str, history: list):
|
| 16 |
-
"""Send the user
|
|
|
|
| 17 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 18 |
|
| 19 |
-
#
|
| 20 |
with MCPClient(params) as tools:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
answer = agent.run(message)
|
| 23 |
|
|
|
|
| 24 |
history.append({"role": "user", "content": message})
|
| 25 |
history.append({"role": "assistant", "content": answer})
|
| 26 |
return history, history
|
|
@@ -28,7 +31,8 @@ def respond(message: str, history: list):
|
|
| 28 |
|
| 29 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 30 |
chat_state = gr.State([])
|
| 31 |
-
chatbot = gr.Chatbot(label="Enterprise SQL Agent"
|
|
|
|
| 32 |
textbox = gr.Textbox(
|
| 33 |
placeholder="Ask: Who are my inactive Northeast customers?", show_label=False
|
| 34 |
)
|
|
|
|
|
|
|
| 1 |
import pathlib
|
| 2 |
+
import os
|
| 3 |
import gradio as gr
|
| 4 |
from mcp import StdioServerParameters
|
| 5 |
+
from smolagents import MCPClient
|
| 6 |
+
from smolagents.agents import OpenAIAgent # <-- use OpenAI-aware agent
|
| 7 |
|
| 8 |
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def respond(message: str, history: list):
|
| 12 |
+
"""Send the user prompt to an OpenAI-backed agent with MCP tools."""
|
| 13 |
+
# Spin up the MCP tool server
|
| 14 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 15 |
|
| 16 |
+
# Connect and get tools
|
| 17 |
with MCPClient(params) as tools:
|
| 18 |
+
# You can pass model name; defaults to gpt-4o if not provided
|
| 19 |
+
agent = OpenAIAgent(
|
| 20 |
+
tools=tools,
|
| 21 |
+
model=os.getenv("OPENAI_MODEL", "gpt-4o-preview"),
|
| 22 |
+
temperature=0.3,
|
| 23 |
+
)
|
| 24 |
answer = agent.run(message)
|
| 25 |
|
| 26 |
+
# Update chat history (messages format)
|
| 27 |
history.append({"role": "user", "content": message})
|
| 28 |
history.append({"role": "assistant", "content": answer})
|
| 29 |
return history, history
|
|
|
|
| 31 |
|
| 32 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 33 |
chat_state = gr.State([])
|
| 34 |
+
chatbot = gr.Chatbot(type="messages", label="Enterprise SQL Agent")
|
| 35 |
+
|
| 36 |
textbox = gr.Textbox(
|
| 37 |
placeholder="Ask: Who are my inactive Northeast customers?", show_label=False
|
| 38 |
)
|