# 🚀 AI Prompt Optimizer **AI Prompt Optimizer** is a Streamlit based tool that intelligently shortens and optimizes your AI prompts reducing token usage while maintaining meaning. This helps you **cut costs** on API calls for models like GPT-5, Claude, and LLaMA, without sacrificing output quality. --- ## 📸 Screenshots ### Main Interface ![Main UI](assets/01.png) ### Main Interface ![Detailed Results](assets/main_ui.png) ### Detailed Results ![Detailed Results](assets/detailed_results.png) --- ## ✨ Features - **Triple Optimization Modes** - **Local Optimizer:** Uses spaCy and Lemminflect for fast, rule-based optimization. - **LLM-based Optimizer (LangChain):** Uses GPT-5 for advanced, context-aware optimization with **8 specialized personas**. - **Agent-based Optimizer (LangGraph):** Uses a GPT-5 powered ReAct agent for intelligent, autonomous optimization with search capabilities. - **Enhanced UI/UX Design** - Modern gradient-based interface with professional styling - Side-by-side layout for optimizer options - Responsive design with clean visual hierarchy - **Advanced Analysis & Tokenization** - **Accurate Token Counting:** Uses `o200k_base` encoding for GPT-5 compatibility - **Tavily Search Integration:** The LangGraph agent can use Tavily to search the web and enhance prompts with real-time information. - **Cost Savings Calculation** - Model-specific cost rates for GPT-4, Claude Opus, Claude Sonnet, and LLaMA 2. - Custom cost inputs for other models. - Percentage and dollar savings displayed instantly. - **Download & Share** - Export optimized prompts. - Share savings stats. - **Interactive Control** - Adjustable optimization aggressiveness for the local optimizer. - Live token and cost reduction stats. --- ## 🛠 Tech Stack - [Streamlit](https://streamlit.io/) — UI framework - [spaCy](https://spacy.io/) — Linguistic processing - [tiktoken](https://github.com/openai/tiktoken) — Token counting - [Lemminflect](https://github.com/bjascob/LemmInflect) — Lemmatization - [OpenAI](https://github.com/openai/openai-python) — LLM integration - [LangChain](https://python.langchain.com/docs/get_started/introduction) — LLM framework - [LangGraph](https://langchain-ai.github.io/langgraph/) — Building stateful, multi-actor applications with LLMs - [Tavily](https://tavily.com/) — Search API for LLMs - Python 3.9+ --- ## 📦 Installation ### (Recommended) Installation with uv `uv` is a fast Python package installer and resolver, written in Rust. It can be used as a drop-in replacement for `pip` and `pip-tools`. ```bash # Install uv pip install uv # Create a virtual environment uv venv # Activate the virtual environment source .venv/bin/activate # Install dependencies from pyproject.toml uv pip install -e . ``` ### Installation with pip ```bash git clone https://github.com/yourusername/ai-prompt-optimizer.git cd ai-prompt-optimizer pip install -r requirements.txt ``` Create a `.env` file in the root of the project and add your API keys: ``` AIMLAPI_API_KEY= TAVILY_API_KEY= ``` --- ## ▶️ Usage ```bash streamlit run app.py ``` 1. Select the **LLM Model** you want to calculate the cost for. 2. Choose your **Optimization Model**: * **spaCy + Lemminflect:** For local, rule-based optimization. * **GPT-5 (LangGraph Agent):** For autonomous, search-enhanced optimization. * **GPT-5 (LangChain):** For simpler LLM-based optimization. 3. If using a GPT-5 optimizer, you can optionally provide an **AIMLAPI API Key** and for the Agent, a **Tavily API Key**. 4. Paste your prompt into the **Original Prompt** box. 5. If using the local optimizer, adjust the **Optimization Level** slider. 6. Click **Optimize**. 7. View token & cost savings instantly. --- ## 📊 Optimization Logic The optimizer applies one of three methods: 1. **Local Optimizer (spaCy + Lemminflect):** * **Regex-based text rules** (filler removal, phrase shortening, structure simplification) * **Linguistic lemmatization** (optional aggressive optimization) 2. **LLM-based Optimizer (GPT-5, LangChain):** * Sends the prompt to the `openai/gpt-5-chat-latest` model via the `aimlapi.com` endpoint for optimization. * Uses one of **8 Specialized Personas** to guide the optimization. 3. **Agent-based Optimizer (GPT-5, LangGraph):** * Uses a **ReAct Agent** powered by LangGraph and GPT-5. * The agent can autonomously decide to use the **Tavily Search API** to enhance prompts with real-time information. * This provides a more intelligent and context-aware optimization. ### Advanced Tokenization - **Model-Specific Encoding:** Uses `o200k_base` for GPT-5 and latest models - **Fallback Support:** Automatically falls back to `cl100k_base` for compatibility - **Accurate Counting:** Provides precise token estimates for cost calculations --- ## 📂 Project Structure ``` . ├── app.py # Streamlit UI ├── engine.py # Local optimization logic ├── llm_optimizer.py # LLM-based optimization logic ├── requirements.txt # Python dependencies ├── pyproject.toml # Project configuration ├── .env # API key storage ├── assets/ │ ├── main_ui.png │ └── detailed_results.png └── README.md ``` --- ## 💡 Example Prompts for Testing ### 🔍 **Research-Based Prompts** (Test Tavily Integration) | Persona | Complexity | Prompt | | --- | --- | --- | | **Default** | Research | "Research and analyze the latest developments in quantum computing breakthroughs announced in 2024. Provide a comprehensive summary of the top 3 most significant advancements, including their potential impact on cybersecurity and data encryption methods." | | **Software Engineer** | Research | "Investigate the current state of WebAssembly adoption in enterprise applications as of 2024. Compare performance benchmarks against traditional JavaScript execution and provide code examples demonstrating WASM integration with modern web frameworks like React or Vue.js." | | **Financial Analyst** | Research | "Analyze the recent Federal Reserve interest rate decisions in 2024 and their impact on tech stock valuations. Research specific examples from major companies like NVIDIA, Apple, and Microsoft, including their quarterly earnings responses to monetary policy changes." | | **Medical Professional** | Research | "Research the latest clinical trial results for GLP-1 receptor agonists in treating obesity published in 2024. Summarize efficacy data, side effect profiles, and compare outcomes across different patient populations, citing peer-reviewed medical literature." | | **Legal Advisor** | Research | "Investigate recent changes to EU AI Act regulations implemented in 2024 and their implications for US companies operating in European markets. Provide specific compliance requirements and recent enforcement actions or precedents." | ### 📝 **Standard Optimization Prompts** | Persona | Quality | Prompt | | --- | --- | --- | | **Default** | Bad | "give me code" | | | Medium | "Can you please very carefully and thoroughly provide me with the complete python code for a flask application that works properly" | | | Good | "Generate a Python script for a simple Flask application with a single endpoint that returns 'Hello, World!'. Include comments to explain each part of the code." | | **UI/UX Designer** | Bad | "make it look better" | | | Medium | "Please help me improve the overall user interface and user experience design of my website to make it more attractive and usable" | | | Good | "Analyze the user flow for our e-commerce checkout process and identify three areas for improvement to reduce cart abandonment. Provide wireframes for the proposed changes." | | **Software Engineer** | Bad | "my code is broken" | | | Medium | "I'm getting a null pointer exception in my java code and I really need help fixing this issue as soon as possible" | | | Good | "I'm encountering a `NullPointerException` in my Java application on line 42 of the `UserAuthentication.java` file. The error occurs when trying to access the `username` property of a `User` object. Provide a code snippet to safely handle this null case." | | **Marketing Copywriter** | Bad | "sell my product" | | | Medium | "Please write a really compelling and engaging product description for my brand new mobile application" | | | Good | "Craft a compelling product description for a new mobile app that helps users learn a new language. Highlight the key features, benefits, and target audience. Use persuasive language to encourage downloads." | | **Creative Writer** | Bad | "write a story" | | | Medium | "Write a very interesting and captivating story about a dragon that goes on adventures" | | | Good | "Write a short story about a young dragon who is afraid of heights and must overcome their fear to save their kingdom from a looming threat. The story should be suitable for young adult readers." | | **Technical Writer** | Bad | "write docs" | | | Medium | "Please create comprehensive documentation for my software API that explains everything users need to know" | | | Good | "Create API documentation for a REST endpoint that handles user authentication. Include request/response examples, error codes, and rate limiting information." | | **Medical Professional** | Bad | "what's wrong with me" | | | Medium | "Please explain the symptoms and potential treatments for common cardiovascular conditions that affect adults" | | | Good | "Explain the diagnostic criteria for atrial fibrillation, including ECG findings, patient symptoms, and evidence-based treatment protocols according to current cardiology guidelines." | | **Financial Analyst** | Bad | "is this stock good" | | | Medium | "Please analyze the financial performance of technology companies and provide investment recommendations" | | | Good | "Perform a DCF analysis for a SaaS company with $50M ARR, 25% growth rate, and 15% churn. Include sensitivity analysis for key assumptions and comparable company multiples." | ### 🎯 **Extreme Optimization Test Cases** | Type | Prompt | | --- | --- | | **Verbose/Wordy** | "I would really, really appreciate it very much if you could possibly help me out by providing me with some extremely detailed and comprehensive information about the various different methods and techniques that are currently being used in the field of machine learning, particularly those that are considered to be the most effective and efficient approaches." | | **Redundant** | "Please analyze and examine the data carefully and thoroughly, making sure to look at all the important details and significant information that might be relevant or useful for understanding the complete picture and overall situation." | | **Formal/Stiff** | "It would be most beneficial if you could provide a comprehensive analysis regarding the implementation of artificial intelligence solutions within the context of modern business environments, taking into consideration the various factors that contribute to successful deployment." | --- ## 📜 License MIT License — feel free to use and modify. --- ## 🤝 Contributing to Team Promptcraft ![Main UI](assets/02.png) --- ## ⭐ Show Your Support If you find this useful, **star this repo** on GitHub and share it with your network!