augustosouza's picture
Update README.md
08a6a9a verified
|
Raw History Blame Contribute Delete
5.59 kB
---
title: Prompt Optimizer MCP Server
emoji: 💬
colorFrom: yellow
colorTo: purple
sdk: gradio
sdk_version: 5.37.0
app_file: app.py
pinned: false
license: mit
---
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
# Prompt Optimizer — MCP Server (Hugging Face Spaces)
This repo exposes a **Model Context Protocol (MCP)** server—backed by a Gradio app—that provides three prompt‑engineering tools your clients (e.g., agents, apps, or the companion Chrome extension via a proxy) can call. Under the hood it uses **Groq**’s `llama-3.3-70b-versatile` model.
---
## What it provides
Three tools (each returns plain text, **no commentary**):
1. **One‑shot Optimization**
**ID**: `one_shot_optimization`
**Input**: Free‑text prompt idea (optionally include a tone like “Technical writing tone”).
**Output**: A single optimized prompt.
2. **Five Q\&A → 3 Follow‑ups**
**ID**: `five_questions_analysis_and_followup_generation`
**Input**: Exactly 5 question–answer pairs describing the task.
**Output**: Exactly **three** numbered follow‑up questions.
3. **Eight Q\&A → Final Prompt**
**ID**: `eight_questions_analysis_and_prompt_generation`
**Input**: Exactly 8 question–answer pairs describing scope, constraints, audience, etc.
**Output**: A complete, ready‑to‑paste final prompt.
---
## Project structure
```
app.py # Gradio + MCP server with 3 tools
README.md # You are here
requirements.txt # gradio, groq, textblob, requests, (optional) python-dotenv
```
---
## Requirements
* Python 3.10+
* **Environment variable:** `GROQ_API_KEY`
* Python packages: `gradio`, `groq`, `textblob`, `requests` (and optionally `python-dotenv` for local dev)
Example `requirements.txt`:
```
gradio
textblob
requests
groq
python-dotenv
```
---
## Running locally
1. Install dependencies:
```bash
pip install -r requirements.txt
```
2. Set your key:
```bash
export GROQ_API_KEY=your_key
```
3. Start the server:
```bash
python app.py
```
This launches Gradio **and** an MCP server (`demo.launch(mcp_server=True)`). The console shows the local URL.
---
## Deploying on Hugging Face Spaces
1. Create a new **Gradio** Space.
2. Add files: `app.py`, `requirements.txt`, `README.md`.
3. In **Settings → Secrets**, add `GROQ_API_KEY`.
4. Commit and let the Space build. When it’s running, you’ll have:
* A **web UI** with three tabs (for quick testing).
* An **MCP server** endpoint exposed by Gradio (path printed in Space logs). Often available at `<SPACE_URL>/mcp`.
> If you use this server from a browser extension, route calls through a small proxy (e.g., Flask) so you don’t expose API keys.
---
## Calling via HTTP (Gradio REST API)
Each tool is given an `api_name`, so Gradio exposes simple JSON endpoints. The payload uses Gradio’s `{ "data": [ ... ] }` format.
### 1) One‑shot Optimization
```bash
curl -X POST https://<SPACE_URL>/api/one_shot_optimization \
-H "Content-Type: application/json" \
-d '{"data": ["Rewrite my prompt to draft a professional email about… (Technical writing tone)"]}'
```
**Response (abridged):**
```json
{"data": ["<optimized prompt here>"]}
```
### 2) Five Q\&A → 3 Follow‑ups
```bash
curl -X POST https://<SPACE_URL>/api/five_questions_analysis_and_followup_generation \
-H "Content-Type: application/json" \
-d '{"data": ["1) Q: … A: …\n2) Q: … A: …\n3) Q: … A: …\n4) Q: … A: …\n5) Q: … A: …"]}'
```
**Response (abridged):**
```json
{"data": ["1. …?\n2. …?\n3. …?"]}
```
### 3) Eight Q\&A → Final Prompt
```bash
curl -X POST https://<SPACE_URL>/api/eight_questions_analysis_and_prompt_generation \
-H "Content-Type: application/json" \
-d '{"data": ["1) Q: … A: …\n…\n8) Q: … A: …"]}'
```
**Response (abridged):**
```json
{"data": ["<final, ready-to-paste prompt>"]}
```
---
## Consuming as an MCP tool server
Use any MCP client that supports **SSE** connections to an MCP server. The URL is the Space URL (the exact MCP path is printed in the Space logs; commonly `/mcp`).
**Example (Python pseudo‑code with Agno):**
```python
from agno.tools.mcp import MCPTools, SSEClientParams
mcp = MCPTools(
server_params=SSEClientParams(server_url="https://<SPACE_URL>/mcp"),
transport="sse",
)
# Then call tools by ID, passing the text payload expected by each function
```
---
## Behavior guarantees
* **Deterministic output format:** Each tool returns only the text specified (no extra commentary).
* **Model:** `llama-3.3-70b-versatile` via Groq’s Chat Completions API.
* **Language:** English by default; content/tone follows user input.
---
## Troubleshooting
* **401/403 or empty output:** Check `GROQ_API_KEY` is set in your env/Space Secrets.
* **MCP client can’t connect:** Confirm the Space is running and that you’re using the correct MCP path (see Space logs). Some platforms require HTTPS and support only **Server‑Sent Events** for transport.
* **Gradio HTTP calls fail:** Ensure you’re posting to `/api/<api_name>` with JSON body `{"data": ["..."]}`.
* **Model quota/rate limit:** Verify your Groq account has sufficient quota.
---
## Privacy & data
This server is stateless. Inputs are sent to Groq to generate outputs; no data is persisted by the app. Consider adding your own logging/retention policy if needed.
---