AI_SQL / app.py
mgbam's picture
Update app.py
866c379 verified
raw
history blame
1.72 kB
import pathlib
import os
import gradio as gr
from mcp import StdioServerParameters
from smolagents import MCPClient
from smolagents.agents import OpenAIAgent # <-- use OpenAI-aware agent
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
def respond(message: str, history: list):
"""Send the user prompt to an OpenAI-backed agent with MCP tools."""
# Spin up the MCP tool server
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
# Connect and get tools
with MCPClient(params) as tools:
# You can pass model name; defaults to gpt-4o if not provided
agent = OpenAIAgent(
tools=tools,
model=os.getenv("OPENAI_MODEL", "gpt-4o-preview"),
temperature=0.3,
)
answer = agent.run(message)
# Update chat history (messages format)
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": answer})
return history, history
with gr.Blocks(title="Enterprise SQL Agent") as demo:
chat_state = gr.State([])
chatbot = gr.Chatbot(type="messages", label="Enterprise SQL Agent")
textbox = gr.Textbox(
placeholder="Ask: Who are my inactive Northeast customers?", show_label=False
)
textbox.submit(respond, [textbox, chat_state], [chatbot, chat_state])
gr.Markdown(
"""
### Example Prompts
- Who are my Northeast customers who haven’t ordered in 6 months?
- List customers sorted by last order date.
- Find clients from the West with recent orders.
_Built with MCP, smolagents, Gradio, and OpenAI_
"""
)
if __name__ == "__main__":
demo.launch()