Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,50 @@
|
|
| 1 |
"""
|
| 2 |
-
app.py
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
GOOGLE_MODEL → override Gemini model ID (e.g. gemini-1.5-pro)
|
| 10 |
-
|
| 11 |
-
HF_MODEL_ID → fallback model repo (must expose Chat-Completion)
|
| 12 |
-
HF_API_TOKEN → token if that repo is gated
|
| 13 |
-
|
| 14 |
-
If no provider keys are set, the code falls back to a free HF model
|
| 15 |
-
`microsoft/Phi-3-mini-4k-instruct`.
|
| 16 |
-
|
| 17 |
-
MCP SERVER
|
| 18 |
-
----------
|
| 19 |
-
`mcp_server.py` must live in the same folder and expose your SQL tools.
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
import os, pathlib, gradio as gr
|
| 23 |
from mcp import StdioServerParameters
|
| 24 |
-
from smolagents import MCPClient, CodeAgent
|
| 25 |
-
from smolagents.models import InferenceClientModel, LiteLLMModel
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 31 |
-
|
| 32 |
-
GEMINI_KEY = os.getenv("GOOGLE_API_KEY")
|
| 33 |
-
GEM_MODEL = os.getenv("GOOGLE_MODEL", "gemini-pro")
|
| 34 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
| 35 |
-
HF_TOKEN = os.getenv("HF_API_TOKEN") or None # only needed for gated repos
|
| 36 |
|
| 37 |
if OPENAI_KEY:
|
| 38 |
-
BASE_MODEL = LiteLLMModel(model_id=
|
| 39 |
-
|
| 40 |
elif GEMINI_KEY:
|
| 41 |
-
BASE_MODEL = LiteLLMModel(model_id=
|
| 42 |
-
|
| 43 |
else:
|
| 44 |
-
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID,
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
# 2. Path to your MCP server (must be alongside this file)
|
| 49 |
-
# --------------------------------------------------------------------- #
|
| 50 |
-
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 51 |
-
|
| 52 |
-
# --------------------------------------------------------------------- #
|
| 53 |
-
# 3. Gradio callback – run prompt → CodeAgent → SQL tools
|
| 54 |
-
# --------------------------------------------------------------------- #
|
| 55 |
def respond(msg: str, chat_history: list):
|
|
|
|
| 56 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 57 |
|
| 58 |
with MCPClient(params) as tools:
|
| 59 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
chat_history += [
|
| 63 |
{"role": "user", "content": msg},
|
|
@@ -65,28 +52,18 @@ def respond(msg: str, chat_history: list):
|
|
| 65 |
]
|
| 66 |
return chat_history, chat_history
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
# 4. Build the UI
|
| 70 |
-
# --------------------------------------------------------------------- #
|
| 71 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 72 |
-
state
|
| 73 |
-
gr.Markdown("##
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
box = gr.Textbox(
|
| 77 |
-
show_label=False,
|
| 78 |
-
placeholder="e.g. Who are my Northeast customers with no orders in 6 months?",
|
| 79 |
-
)
|
| 80 |
box.submit(respond, [box, state], [chat, state])
|
| 81 |
|
| 82 |
-
with gr.Accordion("Example prompts"
|
| 83 |
-
gr.Markdown(
|
| 84 |
-
"* Who are my **Northeast** customers with no orders in 6 months?\n"
|
| 85 |
-
"* List customers sorted by **LastOrderDate**.\n"
|
| 86 |
-
"* Draft re-engagement emails for inactive accounts."
|
| 87 |
-
)
|
| 88 |
|
| 89 |
-
gr.Markdown(f"_Powered by MCP
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
app.py — robust multimodel agent
|
| 3 |
+
--------------------------------
|
| 4 |
+
* Supports **OpenAI** (if `OPENAI_API_KEY`) or **Gemini** (if `GOOGLE_API_KEY`) via
|
| 5 |
+
`LiteLLMModel`.
|
| 6 |
+
* Otherwise falls back to a free HF Inference chat model
|
| 7 |
+
(`microsoft/Phi-3-mini-4k-instruct`).
|
| 8 |
+
* No version‑specific imports (avoids `OpenAIChat` errors).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
|
|
|
| 10 |
import os, pathlib, gradio as gr
|
| 11 |
from mcp import StdioServerParameters
|
| 12 |
+
from smolagents import MCPClient, CodeAgent, InferenceClientModel, LiteLLMModel
|
|
|
|
| 13 |
|
| 14 |
+
# Path to mcp_server.py (must be beside this file)
|
| 15 |
+
SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py")
|
| 16 |
+
|
| 17 |
+
# ---------- Model resolution ----------
|
| 18 |
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
GEMINI_KEY = os.getenv("GOOGLE_API_KEY") # for Gemini via LiteLLM
|
|
|
|
|
|
|
| 20 |
HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct")
|
|
|
|
| 21 |
|
| 22 |
if OPENAI_KEY:
|
| 23 |
+
BASE_MODEL = LiteLLMModel(model_id="openai/gpt-4o-preview", api_key=OPENAI_KEY)
|
| 24 |
+
MODEL_NAME = "openai/gpt-4o-preview"
|
| 25 |
elif GEMINI_KEY:
|
| 26 |
+
BASE_MODEL = LiteLLMModel(model_id="google/gemini-pro", api_key=GEMINI_KEY)
|
| 27 |
+
MODEL_NAME = "google/gemini-pro"
|
| 28 |
else:
|
| 29 |
+
BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID, timeout=90)
|
| 30 |
+
MODEL_NAME = HF_MODEL_ID
|
| 31 |
|
| 32 |
+
# ---------- Callback ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def respond(msg: str, chat_history: list):
|
| 34 |
+
"""Run prompt → CodeAgent → string reply safe for Gradio Chatbot."""
|
| 35 |
params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
|
| 36 |
|
| 37 |
with MCPClient(params) as tools:
|
| 38 |
agent = CodeAgent(tools=tools, model=BASE_MODEL)
|
| 39 |
+
raw = agent.run(msg)
|
| 40 |
+
# Ensure the reply is always a string to avoid Gradio validation errors
|
| 41 |
+
if not isinstance(raw, str):
|
| 42 |
+
import json, pprint
|
| 43 |
+
try:
|
| 44 |
+
raw = json.dumps(raw, indent=2)
|
| 45 |
+
except TypeError:
|
| 46 |
+
raw = pprint.pformat(raw)
|
| 47 |
+
reply = raw
|
| 48 |
|
| 49 |
chat_history += [
|
| 50 |
{"role": "user", "content": msg},
|
|
|
|
| 52 |
]
|
| 53 |
return chat_history, chat_history
|
| 54 |
|
| 55 |
+
# ---------- UI ----------
|
|
|
|
|
|
|
| 56 |
with gr.Blocks(title="Enterprise SQL Agent") as demo:
|
| 57 |
+
state = gr.State([])
|
| 58 |
+
gr.Markdown("## Enterprise SQL Agent — natural‑language to SQL via MCP")
|
| 59 |
+
chat = gr.Chatbot(type="messages", label="Chat")
|
| 60 |
+
box = gr.Textbox(show_label=False, placeholder="Ask: Who are my inactive Northeast customers?")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
box.submit(respond, [box, state], [chat, state])
|
| 62 |
|
| 63 |
+
with gr.Accordion("Example prompts"):
|
| 64 |
+
gr.Markdown("""* Who are my **Northeast** customers with no orders in 6 months?\n* List customers sorted by **LastOrderDate**.\n* Draft re‑engagement emails for inactive accounts.""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
gr.Markdown(f"_Powered by MCP + smolagents • Model: **{MODEL_NAME}**_")
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|