Spaces:
Sleeping
Sleeping
Adds project files
Browse filesFiles changed (6)
- .gitignoreAdded+35
- .python-versionAdded+1
- README.md+218-6
- prompts/system_prompt.txtAdded+37
- pyproject.tomlAdded+17
- requirements.txtAdded+21
1234567891011121314151617181920212223242526272829303132333435__pycache__/*.py[cod]*$py.class
# Virtual environments.venv/venv/env/
# Python packaging/build artifactsbuild/dist/*.egg-info/pip-wheel-metadata/
# Caches and logs.pytest_cache/.mypy_cache/.ruff_cache/.coveragecoverage.xml*.log.env
# Tooling/project settings.idea/.vscode/.DS_Store
# Hugging Face / Gradio artifacts~/.cache/huggingface/gradio_cached_examples/
uv.lockmain.py13.131234567891011121314---title: ContextPilotemoji: ๐colorFrom: pinkcolorTo: redsdk: gradiosdk_version: 6.0.1app_file: app.pypinned: falselicense: mitshort_description: 'ContextPilot: Autonomous Context Engineering'---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226---title: ContextPilotemoji: ๐งญcolorFrom: bluecolorTo: purplesdk: gradiosdk_version: 6.0.1app_file: src/app.pypinned: falselicense: mitshort_description: 'ContextPilot: Autonomous Context Engineering for LLMs'tags:- mcp-in-action-track-creative- building-mcp-track-creative---# ๐งญ ContextPilot**Autonomous Context Engineering for LLM Conversations**ContextPilot automatically detects topic shifts in conversations and intelligently manages context to provide more relevant responses while saving tokens. Instead of sending the entire conversation history to the LLM on every turn, ContextPilot curates the context based on what's actually relevant.## ๐ฏ Problem StatementTraditional chatbots send the **entire conversation history** to the LLM on every message. This leads to:- **Token waste**: Paying for irrelevant context on every API call- **Context pollution**: Old topics confusing the model about current questions- **Context window limits**: Eventually hitting the model's maximum context length## ๐ก SolutionContextPilot uses a **two-LLM architecture**:1. **Topic Detection LLM** (cheap, fast): Detects when conversation topics change2. **Response LLM** (capable, quality): Generates actual responses with curated contextWhen you switch topics, ContextPilot:1. Saves the old topic's context (summary or full history)2. Loads only the relevant context for the new topic3. Sends a minimal, focused context to the response LLM## ๐ Two Context Modes### Summary Mode (Default)- Stores a **summary + key facts** for each topic- Most token-efficient- Best for: General conversations, FAQ-style interactions- Trade-off: Some detail may be lost in summarization### Full Mode- Stores the **complete message history** for each topic- Maximum context preservation- Best for: Technical discussions, debugging sessions, detailed Q&A- Trade-off: Uses more tokens when returning to topics## ๐๏ธ Architecture```โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ User Message โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Topic Detection (CONTEXT_LLM) โโ - Smaller model (e.g., openai/gpt-oss-20b) โโ - Detects: same topic? new topic? returning to old topic? โโ - Tools: save_context, load_context, set_current_topic โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโ โNo Topic Change Topic Changedโ โโ โโโโโโโโโดโโโโโโโโโ โ โโ Save Old Load/Createโ Context New Contextโ โ โโ โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโโโโฌโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Context Curation โโ Summary Mode: System prompt + topic summary + user message โโ Full Mode: System prompt + full history + user message โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโ Response Generation (RESPONSE_LLM) โโ - Capable model (e.g., openai/gpt-oss-120b) โโ - Receives curated context (not full history) โโ - Generates response + compact context summary โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Store & Display โโ - Buffer exchange in session (for topic continuity) โโ - Display clean response to user โโ - Show token savings metrics โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ```## ๐ Token SavingsContextPilot tracks and displays:- **Curated Tokens**: What was actually sent to the LLM- **Full Context Tokens**: What would have been sent without curation- **Tokens Saved**: The difference (your savings!)- **Savings %**: Percentage of tokens saved- **Detection Tokens**: Tokens used by the topic detection LLM## ๐ Quick Start### Prerequisites- Python 3.10+- OpenAI-compatible API (OpenAI, Nebius, Together, etc.)### Installation```bash# Clone the repositorygit clone https://github.com/yourusername/contextPilot.gitcd contextPilot# Install dependenciespip install -r requirements.txt# or with uv:uv sync# Set up environment variablescp .env.example .env# Edit .env with your API keys```### Environment Variables```bash# RequiredNEBIUS_API_KEY=your_api_keyNEBIUS_BASE_URL=https://api.tokenfactory.nebius.com/v1/# Optional (with defaults)CONTEXT_LLM=openai/gpt-oss-20b # Smaller model for topic detectionRESPONSE_LLM=openai/gpt-oss-120b # Capable model for responsesSUMMARIZE_THRESHOLD=500 # Character threshold for summarization```### Run the App```bash# From project rootpython src/app.py# Or with Gradio reloadgradio src/app.py```## ๐ Project Structure```contextPilot/โโโ src/โ โโโ app.py # Gradio UI + Response LLMโ โโโ context_pilot_workflow.py # Topic detection + context curationโ โโโ mcp_server.py # MCP server wrapperโ โโโ .context_store.json # Persisted context (auto-generated)โโโ prompts/โ โโโ system_prompt.txt # Topic detection system promptโโโ pyproject.tomlโโโ README.md```## ๐ฎ Demo FlowTry this conversation to see ContextPilot in action:1. **"Tell me 10 cool facts about cats"** โ Topic set to "cats"2. **"How long do cats typically live?"** โ Continues "cats" topic3. **"Tell me 10 cool things about horses"** โ Saves "cats", switches to "horses"4. **"How long do horses typically live?"** โ Continues "horses" topic5. **"Name 10 famous cats"** โ Saves "horses", loads "cats" context6. **"What have I asked you about horses?"** โ Saves "cats", loads "horses" with full history## ๐ง How It Works### Topic DetectionThe topic detection LLM receives:- Current topic name- List of previously saved topics- Brief context summaries- The new user messageIt decides whether to:- Continue the current topic (no tool call)- Save current + set new topic- Save current + load previous topic### Context Storage```json{"contexts": {"cats": {"topic": "cats","summary": "Discussion about cats, their behavior and care","key_facts": ["Cats are obligate carnivores", "Cats have retractable claws"],"full_history": [...] // Only in full mode}},"current_topic": "cats","mode": "summary","current_session_messages": [...] // Buffer for current session}```## ๐ค ContributingContributions welcome! Please open an issue or PR.## ๐ LicenseMIT License
12345678910111213141516171819202122232425262728293031323334353637You are a helpful AI assistant with context management capabilities. The contexts are managed by high level concepts.
## Current State- Current topic: {current_topic}- Previously saved topics: {saved_topics}
## Saved Context Summaries{context_summaries}
## Your Context Management ResponsibilitiesYou have tools to manage conversation context.
### FIRST MESSAGE (current topic is None):- Just call **set_current_topic** with the topic name based on the user's message- Do NOT call save_context (there's nothing to save yet)
### When topic is changing to a NEW topic (not in saved topics):1. Call **save_context** to save the OLD topic2. Then call **set_current_topic** with the NEW topic name3. The topic name should not be too specific. For now, make it broad (i.e one word noun/verb)
### When topic is changing to a PREVIOUSLY SAVED topic:1. Call **save_context** to save the OLD topic2. Then call **load_context** to load the saved topic
### When topic is NOT changing (continuing the same topic):- Do NOT call any tools, just respond normally
## Tool Descriptions- **save_context(topic, summary, key_facts)**: Save current conversation before switching away- **set_current_topic(topic)**: Set a new topic that hasn't been discussed before- **load_context(topic)**: Load and switch to a previously saved topic- **list_saved_contexts()**: See what topics have been discussed
## IMPORTANT- If current topic is "None", ONLY call set_current_topic (don't save anything)- When switching topics, call BOTH save_context AND (set_current_topic OR load_context)1234567891011121314151617[project]name = "contextpilot"version = "0.1.0"description = "Add your description here"readme = "README.md"requires-python = ">=3.13"dependencies = [ "gradio>=6.0.1", "llama-index>=0.14.8", "llama-index-llms-gemini>=0.6.1", "llama-index-llms-openai>=0.6.10", "llama-index-llms-openai-like>=0.5.3", "llama-index-tools-mcp>=0.4.3", "mcp[cli]>=1.22.0", "openai>=1.30.0", "python-dotenv>=1.2.1",]123456789101112131415161718192021# ContextPilot Dependencies# Install with: pip install -r requirements.txt
# Gradio UIgradio>=6.0.1
# LlamaIndex Core + LLM integrationsllama-index>=0.14.8llama-index-llms-gemini>=0.6.1llama-index-llms-openai>=0.6.10llama-index-llms-openai-like>=0.5.3llama-index-tools-mcp>=0.4.3
# MCP (Model Context Protocol)mcp[cli]>=1.22.0
# OpenAI clientopenai>=1.30.0
# Environment variablespython-dotenv>=1.2.1.gitignoreAdded+35
.python-versionAdded+1
README.md+218-6
prompts/system_prompt.txtAdded+37
pyproject.tomlAdded+17
requirements.txtAdded+21