mgbam commited on
Commit
866c379
·
verified ·
1 Parent(s): afe7fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
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, CodeAgent
6
- from smolagents.models.openai import OpenAIChatModel # add this
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's message to the MCP-powered agent and return reply."""
 
17
  params = StdioServerParameters(command="python", args=[str(SERVER_PATH)])
18
 
19
- # Launch the MCP server and create a CodeAgent that uses the REAL model object
20
  with MCPClient(params) as tools:
21
- agent = CodeAgent(tools=tools, model=openai_model)
 
 
 
 
 
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", type="messages")
 
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
  )