mgbam commited on
Commit
05f1269
·
verified ·
1 Parent(s): 9f3eca4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -34
app.py CHANGED
@@ -1,58 +1,92 @@
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(message: str, history: list):
 
 
34
  params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
 
35
  with MCPClient(params) as tools:
36
  agent = CodeAgent(tools=tools, model=BASE_MODEL)
37
- answer = agent.run(message)
38
- history += [
39
- {"role": "user", "content": message},
40
- {"role": "assistant", "content": answer},
 
41
  ]
42
- return history, history
43
 
44
- # ---------- UI ----------
 
 
45
  with gr.Blocks(title="Enterprise SQL Agent") as demo:
46
- state = gr.State([])
47
- gr.Markdown("## Enterprise SQL Agent naturallanguage to SQL via MCP")
48
- chat = gr.Chatbot(type="messages", label="Chat")
49
- box = gr.Textbox(show_label=False, placeholder="Ask: Who are my inactive Northeast customers?")
 
 
 
 
50
  box.submit(respond, [box, state], [chat, state])
51
 
52
- with gr.Accordion("Example prompts"):
53
- 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.""")
 
 
 
 
54
 
55
- gr.Markdown(f"_Powered by MCP + smolagents Model: **{MODEL_NAME}**_")
56
 
57
  if __name__ == "__main__":
58
  demo.launch()
 
1
  """
2
+ app.py – Enterprise SQL Agent (Gradio + smolagents + MCP)
3
+
4
+ ENV VARS SUPPORTED
5
+ ------------------
6
+ OPENAI_API_KEY → use any OpenAI chat model (default = gpt-4o)
7
+ OPENAI_MODEL → override the OpenAI model ID (e.g. gpt-4-turbo)
8
+ GOOGLE_API_KEY → use Gemini-Pro via Google AI
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
+ # 1. Resolve the base LLM model, preferring OpenAI → Gemini → HF
29
+ # --------------------------------------------------------------------- #
30
  OPENAI_KEY = os.getenv("OPENAI_API_KEY")
31
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o") # change if not whitelisted
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=f"openai/{OPENAI_MODEL}", api_key=OPENAI_KEY)
39
+ ACTIVE = f"OpenAI · {OPENAI_MODEL}"
40
  elif GEMINI_KEY:
41
+ BASE_MODEL = LiteLLMModel(model_id=f"google/{GEM_MODEL}", api_key=GEMINI_KEY)
42
+ ACTIVE = f"Gemini · {GEM_MODEL}"
43
  else:
44
+ BASE_MODEL = InferenceClientModel(model_id=HF_MODEL_ID, hf_api_token=HF_TOKEN)
45
+ ACTIVE = f"Hugging Face · {HF_MODEL_ID}"
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
+ reply = agent.run(msg)
61
+
62
+ chat_history += [
63
+ {"role": "user", "content": msg},
64
+ {"role": "assistant", "content": reply},
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 = gr.State([])
73
+ gr.Markdown("## 🏢 Enterprise SQL Agent ask natural-language questions about your data")
74
+
75
+ chat = gr.Chatbot(type="messages", label="Chat")
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", open=False):
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 tools + smolagents · Active model → **{ACTIVE}**_")
90
 
91
  if __name__ == "__main__":
92
  demo.launch()