Skip to main content

Provider-agnostic prompt optimization pipeline powered by LiteLLM.

Project description

Prompt Optimizer

A provider-agnostic prompt optimization pipeline powered by LiteLLM and DeepEval.

Give it an initial prompt, a set of tasks, and an evaluation rubric — it will generate responses, score them with an LLM judge, and automatically rewrite the prompt to fix recurring weaknesses across multiple optimization loops.

Features

  • Provider-agnostic — works with any LiteLLM-supported model (Gemini, Groq, OpenAI, Anthropic, etc.) for both generation and judging, just by changing a model string.
  • Automated feedback loop — generates responses, scores them, and rewrites the prompt to target only recurring weaknesses (not one-off mistakes).
  • Regression-safe — always keeps the best-scoring prompt version found so far, even if a later candidate performs worse.
  • Blended scoring — combines a custom rubric (GEval) with a generic relevance check (AnswerRelevancyMetric).
  • Deterministic section checks — optionally verifies required sections are present and in order, independent of the LLM judge's leniency.
  • Full telemetry — tracks token usage, timing, and per-task score deltas across every loop.

Installation

pip install prompt_optimizer

Setup

  1. Create a .env file in your project root with your model names and API key(s):
    GENERATION_MODEL_NAME=gemini/gemini-2.0-flash
    EVAL_MODEL_NAME=gemini/gemini-2.0-flash
    GEMINI_API_KEY=your-api-key-here
Any [LiteLLM-supported model string](https://docs.litellm.ai/docs/providers) works here (e.g. `gpt-4o`, `claude-3-5-sonnet-20241022`, `groq/llama-3.3-70b-versatile`), as long as the matching API key env var is also set.
  1. Create a config.json describing your prompt, tasks, and evaluation rubric:
    {
      "max_loops": 3,
      "initial_prompt": "You are a helpful assistant. Answer: {user_query}",
      "optimizer_prompt": "You are a Prompt Optimization Expert...",
      "evaluation_steps": [
        "Check that the response directly answers the question."
      ],
      "tasks": [
        { "id": 1, "query": "What is the capital of France?" }
      ]
    }

Usage

prompt-optimizer

Optional flags:

prompt-optimizer --config config.json --env .env --output results.json
Flag Description
-c, --config Path to config JSON (default: config.json)
-e, --env Path to .env file (default: .env)
-o, --output Save results as JSON (generated responses are terminal-only and never saved to disk)

How it works

  1. Generate — the current prompt is run against every task using the generation model.
  2. Evaluate — each response is scored by an LLM judge using your custom rubric, blended with a generic relevance check, plus an optional deterministic required-section check.
  3. Compare — the new average score is compared against the best version seen so far. Regressions are kept on record but never become the new baseline.
  4. Optimize — if the score is below threshold, an optimizer model analyzes recurring weaknesses and proposes new prompt instructions, which are merged into a bounded instruction set.
  5. Repeat until the score threshold is hit or max_loops is reached.

The final result includes the best-scoring prompt version, its score, and full telemetry (token usage, timing, prompt version history).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litellm_prompt_optimizer-0.5.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litellm_prompt_optimizer-0.5.0-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file litellm_prompt_optimizer-0.5.0.tar.gz.

File metadata

  • Download URL: litellm_prompt_optimizer-0.5.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for litellm_prompt_optimizer-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c3329033c3148ade75775bf9dc03e8f8562d34ed340c06aec28a049229ddb936
MD5 9d68ed8615fe120e7dc95b034fb2ba9f
BLAKE2b-256 a161d015bb93831c3b2180bd8a6150ce2beca252d92de8130f23485bedc8d6ae

See more details on using hashes here.

File details

Details for the file litellm_prompt_optimizer-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litellm_prompt_optimizer-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e7e1a387abaabd041688966ea2859a866c62029815ea90aeab8501199c039dd
MD5 761389b7086f3297ae6d04ec973c1ab7
BLAKE2b-256 e7559e17b024066c081fa4d2ccfecf212a7819712fbd915dce51a2c37ee2d937

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page