| | """ |
| | Gradio front-end + smolagents CodeAgent |
| | --------------------------------------- |
| | β’ If you set an OPENAI_API_KEY the agent will call OpenAI (GPT-4o by default). |
| | β’ Otherwise it falls back to a free Hugging Face chat-completion model |
| | (defaults to microsoft/Phi-3-mini-4k-instruct which the public Inference |
| | API exposes for the Chat Completion task). |
| | β’ You can override the fallback by defining HF_MODEL_ID and, if needed, |
| | HF_API_TOKEN in the Space β Settings β Secrets. |
| | """ |
| |
|
| | import os, pathlib, gradio as gr |
| | from mcp import StdioServerParameters |
| | from smolagents import MCPClient, CodeAgent, InferenceClientModel |
| |
|
| | |
| | SERVER_PATH = pathlib.Path(__file__).with_name("mcp_server.py") |
| |
|
| | |
| | OPENAI_KEY = os.getenv("OPENAI_API_KEY") |
| | HF_MODEL_ID = os.getenv("HF_MODEL_ID", "microsoft/Phi-3-mini-4k-instruct") |
| |
|
| | if OPENAI_KEY: |
| | from smolagents.models import OpenAIChat |
| | BASE_MODEL = OpenAIChat( |
| | model=os.getenv("OPENAI_MODEL", "gpt-4o-preview"), |
| | temperature=0.3, |
| | ) |
| | else: |
| | BASE_MODEL = InferenceClientModel( |
| | model_id=HF_MODEL_ID, |
| | hf_api_token=os.getenv("HF_API_TOKEN"), |
| | timeout=90, |
| | ) |
| |
|
| | |
| | def respond(message: str, history: list): |
| | """Send user prompt β CodeAgent β SQL tools β natural-language answer.""" |
| | params = StdioServerParameters(command="python", args=[str(SERVER_PATH)]) |
| | with MCPClient(params) as tools: |
| | agent = CodeAgent(tools=tools, model=BASE_MODEL) |
| | answer = agent.run(message) |
| |
|
| | history += [ |
| | {"role": "user", "content": message}, |
| | {"role": "assistant", "content": answer}, |
| | ] |
| | return history, history |
| |
|
| | |
| | with gr.Blocks(title="Enterprise SQL Agent") as demo: |
| | state = gr.State([]) |
| | gr.Markdown("## Enterprise SQL Agent β ask questions about your data π’β‘οΈπ") |
| |
|
| | chat = gr.Chatbot(type="messages", label="Chat") |
| | box = gr.Textbox( |
| | placeholder="e.g. Who are my inactive Northeast customers?", |
| | show_label=False, |
| | ) |
| | box.submit(respond, [box, state], [chat, state]) |
| |
|
| | with gr.Accordion("Example prompts"): |
| | 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." |
| | ) |
| |
|
| | footer = ( |
| | f"_Powered by MCP + smolagents + Gradio β’ Model: " |
| | f"{'OpenAI' if OPENAI_KEY else HF_MODEL_ID}_" |
| | ) |
| | gr.Markdown(footer) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |
| |
|