{"id":36969165,"url":"https://github.com/leonardosul/mixture-llm","last_synced_at":"2026-02-26T10:50:08.915Z","repository":{"id":329044363,"uuid":"1117331905","full_name":"leonardosul/mixture-llm","owner":"leonardosul","description":"Combine multiple LLMs for better outputs.","archived":false,"fork":false,"pushed_at":"2025-12-18T04:25:22.000Z","size":49,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-20T15:28:11.160Z","etag":null,"topics":["llms","moa","python"],"latest_commit_sha":null,"homepage":"https://leonardosul.github.io/mixture-llm/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leonardosul.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-16T06:57:15.000Z","updated_at":"2025-12-18T22:50:29.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/leonardosul/mixture-llm","commit_stats":null,"previous_names":["leonardosul/mixture-llm"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/leonardosul/mixture-llm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardosul%2Fmixture-llm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardosul%2Fmixture-llm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardosul%2Fmixture-llm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardosul%2Fmixture-llm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leonardosul","download_url":"https://codeload.github.com/leonardosul/mixture-llm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leonardosul%2Fmixture-llm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28399513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["llms","moa","python"],"created_at":"2026-01-13T21:03:35.287Z","updated_at":"2026-01-13T21:03:35.945Z","avatar_url":"https://github.com/leonardosul.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mixture-llm\n\n[![CI](https://github.com/leonardosul/mixture-llm/actions/workflows/ci.yaml/badge.svg)](https://github.com/leonardosul/mixture-llm/actions/workflows/ci.yaml)\n[![PyPI](https://img.shields.io/pypi/v/mixture-llm)](https://pypi.org/project/mixture-llm/)\n[![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://leonardosul.github.io/mixture-llm)\n\nCombine LLMs to beat the best single LLM.\n\nThe Mixture-of-Agents architecture achieved **65.1% on AlpacaEval 2.0** using only open-source models—surpassing GPT-4o's 57.5%. This library gives you the building blocks to construct these pipelines.\n\n## Install\n\n```bash\npip install mixture-llm\n```\n\n## Quick start\n\n```python\nfrom mixture_llm import Propose, Aggregate, run\n\npipeline = [\n    Propose([\"gpt-5-nano-2025-08-07\", \"claude-sonnet-4-5\", \"llama-3.3-70b\"]),\n    Aggregate(\"gpt-5-nano-2025-08-07\"),\n]\n\nresult, history = await run(pipeline, \"What is quantum computing?\", my_client)\n```\n\n## Paper-accurate pipelines\n\n### Together MoA (65.1% AlpacaEval)\n\nThe benchmark-winning configuration from [Wang et al. (2024)](https://arxiv.org/abs/2406.04692): 3 layers, 6 diverse proposers, Qwen aggregator.\n\n```python\nPROPOSERS = [\n    \"wizardlm-2-8x22b\",\n    \"qwen1.5-110b-chat\",\n    \"qwen1.5-72b-chat\",\n    \"llama-3-70b-instruct\",\n    \"mixtral-8x22b-instruct\",\n    \"dbrx-instruct\",\n]\n\ntogether_moa = [\n    Propose(PROPOSERS, temp=0.7, max_tokens=512),\n    Synthesize(PROPOSERS, temp=0.7, max_tokens=512),\n    Synthesize(PROPOSERS, temp=0.7, max_tokens=512),\n    Aggregate(\"qwen1.5-110b-chat\"),\n]\n```\n\n### MoA-Lite (59.3% AlpacaEval)\n\nCost-optimized 2-layer variant—still beats GPT-4o.\n\n```python\nmoa_lite = [\n    Propose(PROPOSERS, temp=0.7, max_tokens=512),\n    Synthesize(PROPOSERS, temp=0.7, max_tokens=512),\n    Aggregate(\"qwen1.5-72b-chat\"),\n]\n```\n\n### Self-MoA (+6.6% over standard MoA)\n\n[Li et al. (2025)](https://arxiv.org/abs/2502.00674) showed that sampling one top model multiple times can outperform diverse model mixtures.\n\n```python\n# Same model, multiple samples via temperature\nself_moa = [\n    Propose([\"gpt-5-nano-2025-08-07\"] * 6, temp=0.7),\n    Aggregate(\"gpt-5-nano-2025-08-07\"),\n]\n```\n\n### With robustness (shuffle + dropout)\n\nPrevents positional bias and improves diversity.\n\n```python\nrobust_moa = [\n    Propose([\"gpt-5-nano-2025-08-07\", \"claude-sonnet-4-5\", \"llama-70b\", \"gemini-2.5-flash\"]),\n    Shuffle(),\n    Dropout(0.2),\n    Aggregate(\"gpt-5-nano-2025-08-07\"),\n]\n```\n\n## Steps\n\n**LLM steps** — call models:\n- `Propose(agents)` — generate initial responses in parallel\n- `Synthesize(agents)` — each agent synthesizes all previous outputs\n- `Aggregate(agent)` — single model combines everything into final output\n- `Refine(agents)` — improve each response individually\n- `Rank(agent, n)` — select top n responses by quality\n- `Vote(agent)` — pick consensus answer\n\n**Transform steps** — manipulate responses:\n- `Shuffle()` — randomize order (prevents position bias)\n- `Dropout(rate)` — randomly drop responses (improves robustness)\n- `Sample(n)` — random subset\n- `Take(n)` — first n responses\n- `Filter(fn)` — keep responses matching predicate\n- `Map(fn)` — transform each response\n\n## Configuration\n\nEvery LLM step accepts `temp` and `max_tokens`:\n\n```python\nPropose([\"gpt-5-nano-2025-08-07\", \"claude-sonnet-4-5\"], temp=0.9, max_tokens=4096)\n```\n\nOverride the synthesis prompt:\n\n```python\nAggregate(\"gpt-5-nano-2025-08-07\", prompt=\"Pick the single best response and return it verbatim.\")\n```\n\n## Client examples\n\nYour client is an async function with this signature:\n\n```python\nasync def client(model, messages, temp, max_tokens) -\u003e tuple[str, int, int]:\n    # Returns (response_text, input_tokens, output_tokens)\n```\n\n### OpenAI SDK (OpenAI + Anthropic models)\n\n```python\nfrom openai import AsyncOpenAI\n\nopenai_client = AsyncOpenAI()\nanthropic_client = AsyncOpenAI(\n    base_url=\"https://api.anthropic.com/v1/\",\n    api_key=os.environ[\"ANTHROPIC_API_KEY\"],\n)\n\nasync def multi_provider_client(model, messages, temp, max_tokens):\n    client = anthropic_client if model.startswith(\"claude\") else openai_client\n    # GPT-5: max_completion_tokens, no temperature, minimal reasoning\n    is_gpt5 = model.startswith(\"gpt-5\")\n    params = {\"model\": model, \"messages\": messages}\n    params.update({\"max_completion_tokens\": max_tokens, \"reasoning_effort\": \"minimal\"} if is_gpt5 else {\"max_tokens\": max_tokens, \"temperature\": temp})\n    resp = await client.chat.completions.create(**params)\n    return resp.choices[0].message.content, resp.usage.prompt_tokens, resp.usage.completion_tokens\n\n# Mix providers in one pipeline\npipeline = [\n    Propose([\"gpt-5-nano-2025-08-07\", \"claude-sonnet-4-5\", \"gpt-5-nano-2025-08-07\"]),\n    Aggregate(\"claude-sonnet-4-5\"),\n]\n```\n\n### OpenRouter (access all models via one API)\n\n```python\nfrom openai import AsyncOpenAI\n\nclient = AsyncOpenAI(\n    base_url=\"https://openrouter.ai/api/v1\",\n    api_key=os.environ[\"OPENROUTER_API_KEY\"],\n)\n\nasync def openrouter_client(model, messages, temp, max_tokens):\n    resp = await client.chat.completions.create(\n        model=model, messages=messages, temperature=temp, max_tokens=max_tokens\n    )\n    return resp.choices[0].message.content, resp.usage.prompt_tokens, resp.usage.completion_tokens\n\n# Together MoA models via OpenRouter\nPROPOSERS = [\n    \"qwen/qwen-2.5-72b-instruct\",\n    \"meta-llama/llama-3.3-70b-instruct\",\n    \"mistralai/mixtral-8x22b-instruct\",\n]\n\ntogether_moa_openrouter = [\n    Propose(PROPOSERS, temp=0.7, max_tokens=512),\n    Synthesize(PROPOSERS, temp=0.7, max_tokens=512),\n    Aggregate(\"qwen/qwen-2.5-72b-instruct\"),\n]\n```\n\n### Groq via LiteLLM (free tier)\n\nGroq offers free access to several models. Great for experimentation.\n\n```python\nfrom litellm import acompletion\n\nasync def groq_client(model, messages, temp, max_tokens):\n    resp = await acompletion(\n        model=f\"groq/{model}\", messages=messages, temperature=temp, max_tokens=max_tokens\n    )\n    return resp.choices[0].message.content, resp.usage.prompt_tokens, resp.usage.completion_tokens\n\n# Free Groq models (check console.groq.com/docs/rate-limits for current list)\nGROQ_FREE = [\n    \"llama-3.3-70b-versatile\",\n    \"llama-3.1-8b-instant\",\n    \"qwen/qwen3-32b\",\n    \"meta-llama/llama-4-scout-17b-16e-instruct\",\n]\n\nfree_moa = [\n    Propose(GROQ_FREE, temp=0.7, max_tokens=512),\n    Aggregate(\"llama-3.3-70b-versatile\"),\n]\n\n# Self-MoA with Groq (single model, multiple samples)\nfree_self_moa = [\n    Propose([\"llama-3.3-70b-versatile\"] * 4, temp=0.7),\n    Aggregate(\"llama-3.3-70b-versatile\"),\n]\n```\n\n## Examples\n\nThe [`examples/`](examples/) directory contains tested, runnable scripts for different providers. See [`examples/EXAMPLES.md`](examples/EXAMPLES.md) for detailed documentation.\n\n| Example | Provider | What You'll Learn |\n|---------|----------|-------------------|\n| [`openai_basic.py`](examples/openai_basic.py) | OpenAI | Basic MoA pattern (Propose → Aggregate), client setup, token tracking |\n| [`openai_self_moa.py`](examples/openai_self_moa.py) | OpenAI | Self-MoA technique—one model sampled 6 times beats diverse mixtures |\n| [`multi_provider.py`](examples/multi_provider.py) | OpenAI + Anthropic | Provider routing, Shuffle step to prevent position bias |\n| [`openrouter_moa.py`](examples/openrouter_moa.py) | OpenRouter | 3-layer MoA (Propose → Synthesize → Aggregate), paper configuration |\n| [`groq_free.py`](examples/groq_free.py) | Groq | Free experimentation, LiteLLM integration, Dropout for robustness |\n| [`with_history.py`](examples/with_history.py) | Groq | Pipeline debugging, Rank step, execution history inspection |\n\n```bash\n# Install and run\npip install -e \".[examples]\"\nexport OPENAI_API_KEY=sk-...\npython examples/openai_basic.py\n\n# Or try free with Groq\nexport GROQ_API_KEY=gsk_...\npython examples/groq_free.py\n```\n\n## Key findings from the research\n\n- **Aggregator quality matters 2x more than proposer quality** — invest in your final model\n- **3 layers is the sweet spot** — diminishing returns beyond this\n- **Diversity vs quality tradeoff** — Self-MoA shows a single great model can beat diverse mediocre ones\n- **6 proposers optimal** — gains diminish after this point\n\n## References\n\n- Wang et al. \"Mixture-of-Agents Enhances Large Language Model Capabilities\" (2024) — [arXiv:2406.04692](https://arxiv.org/abs/2406.04692)\n- Li et al. \"Rethinking Mixture-of-Agents: Is Mixing Different Large Language Models Beneficial?\" (2025) — [arXiv:2502.00674](https://arxiv.org/abs/2502.00674)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardosul%2Fmixture-llm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonardosul%2Fmixture-llm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonardosul%2Fmixture-llm/lists"}