{"data":{"slug":"srush-minichain","name":"MiniChain","tagline":"A tiny library for coding with large language models","github_url":"https://github.com/srush/MiniChain","owner":"srush","repo":"MiniChain","owner_avatar_url":"https://avatars.githubusercontent.com/u/35882?v=4","primary_language":"Python","stars":1232,"forks":74,"topics":[],"archived":false,"github_pushed_at":"2024-07-10T14:59:50+00:00","maintenance_label":"Dormant","stars_delta_30d":0,"url":"https://www.graphcanon.com/tools/srush-minichain","markdown_url":"https://www.graphcanon.com/tools/srush-minichain.md","api_url":"https://www.graphcanon.com/api/graphcanon/tools/srush-minichain","graph_url":"https://www.graphcanon.com/api/graphcanon/graph?tool=srush-minichain","description":"A tiny library for coding with large language models.","homepage_url":"https://srush-minichain.hf.space/","license":"MIT","open_issues":12,"watchers":13,"ai_summary":"MiniChain is a Python-based, lightweight framework intended to facilitate the creation of applications using large language models through annotated function calls and Jinja-based prompt templating.","readme_excerpt":"<img src=\"https://user-images.githubusercontent.com/35882/227030644-f70e55e8-68a3-48d3-afa3-54c4de8fc210.png\" width=\"100%\">\n\nA tiny library for coding with **large** language models. Check out the [MiniChain Zoo](https://srush-minichain.hf.space/) to get a sense of how it works.\n\n## Coding\n\n* Code ([math_demo.py](https://github.com/srush/MiniChain/blob/main/examples/math_demo.py)): Annotate Python functions that call language models.\n\n```python\n@prompt(OpenAI(), template_file=\"math.pmpt.tpl\")\ndef math_prompt(model, question):\n    \"Prompt to call GPT with a Jinja template\"\n    return model(dict(question=question))\n\n@prompt(Python(), template=\"import math\\n{{code}}\")\ndef python(model, code):\n    \"Prompt to call Python interpreter\"\n    code = \"\\n\".join(code.strip().split(\"\\n\")[1:-1])\n    return model(dict(code=code))\n\ndef math_demo(question):\n    \"Chain them together\"\n    return python(math_prompt(question))\n```\n\n* Chains ([Space](https://srush-minichain.hf.space/)): MiniChain builds a graph (think like PyTorch) of all the calls you make for debugging and error handling.\n<img src=\"https://user-images.githubusercontent.com/35882/226965531-78df7927-988d-45a7-9faa-077359876730.png\" width=\"50%\">\n\n\n```python\nshow(math_demo,\n     examples=[\"What is the sum of the powers of 3 (3^i) that are smaller than 100?\",\n               \"What is the sum of the 10 first positive integers?\"],\n     subprompts=[math_prompt, python],\n     out_type=\"markdown\").queue().launch()\n```\n\n\n* Template ([math.pmpt.tpl](https://github.com/srush/MiniChain/blob/main/examples/math.pmpt.tpl)): Prompts are separated from code.\n\n```\n...\nQuestion:\nA robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?\nCode:\n2 + 2/2\n\nQuestion:\n{{question}}\nCode:\n```\n\n* Installation\n\n```bash\npip install minichain\nexport OPENAI_API_KEY=\"sk-***\"\n```\n\n## Examples\n\nThis library allows us to implement several popular approaches in a few lines of code.\n\n* [Retrieval-Augmented QA](https://srush.github.io/MiniChain/examples/qa/)\n* [Chat with memory](https://srush.github.io/MiniChain/examples/chatgpt/)\n* [Information Extraction](https://srush.github.io/MiniChain/examples/ner/)\n* [Interleaved Code (PAL)](https://srush.github.io/MiniChain/examples/pal/) - [(Gao et al 2022)](https://arxiv.org/pdf/2211.10435.pdf)\n* [Search Augmentation (Self-Ask)](https://srush.github.io/MiniChain/examples/selfask/) - [(Press et al 2022)](https://ofir.io/self-ask.pdf)\n* [Chain-of-Thought](https://srush.github.io/MiniChain/examples/bash/) - [(Wei et al 2022)](https://arxiv.org/abs/2201.11903)\n\nIt supports the current backends.\n\n* OpenAI (Completions / Embeddings)\n* Hugging Face 🤗\n* Google Search\n* Python\n* Manifest-ML (AI21, Cohere, Together)\n* Bash\n\n## Why Mini-Chain?\n\nThere are several very popular libraries for prompt chaining,\nnotably: [LangChain](https://langchain.readthedocs.io/en/latest/),\n[Promptify](https://github.com/promptslab/Promptify), and\n[GPTIndex](https://gpt-index.readthedocs.io/en/latest/reference/prompts.html).\nThese library are useful, but they are extremely large and\ncomplex. MiniChain aims to implement the core prompt chaining\nfunctionality in a tiny digestable library.\n\n\n## Tutorial\n\nMini-chain is based on annotating functions as prompts.\n\n\n\n\n```python\n@prompt(OpenAI())\ndef color_prompt(model, input):\n    return model(f\"Answer 'Yes' if this is a color, {input}. Answer:\")\n```\n\nPrompt functions act like python functions, except they are lazy to access the result you need to call `run()`.\n\n```python\nif color_prompt(\"blue\").run() == \"Yes\":\n    print(\"It's a color\")\n```\nAlternatively you can chain prompts together. Prompts are lazy, so if you want to manipulate them you need to add `@transform()` to your function. For example:\n\n```python\n@transform()\ndef said_yes(input):\n    return input == \"Yes\"\n```\n\n\n\n```python\n@prompt(OpenAI())\ndef adjective_prompt(model, input):\n    return model(f\"Give an adjective to describe {input}. Answer:\")\n```\n\n\n```python\nad","github_created_at":"2023-02-10T16:07:11+00:00","created_at":"2026-07-11T10:53:35.3465+00:00","updated_at":"2026-08-15T18:00:44.338861+00:00","categories":[{"slug":"llm-frameworks","name":"LLM Frameworks","url":"https://www.graphcanon.com/categories/llm-frameworks","markdown_url":"https://www.graphcanon.com/categories/llm-frameworks.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/llm-frameworks"}],"tags":[{"slug":"function-annotation","name":"function annotation"},{"slug":"model-chains","name":"model chains"},{"slug":"prompt-templating","name":"prompt templating"},{"slug":"python","name":"python"}],"trust":{"provenance":{"is_fork":false,"github_id":600114716,"owner_type":"User","methodology":"github_public_v1","parent_repo":null,"near_duplicate_slugs":[]},"computed_at":"2026-08-15T18:00:43.596Z","maintenance":{"label":"Dormant","score":18,"methodology":"github_public_v1","releases_90d":0,"days_since_push":766,"last_release_at":"2023-04-18T19:58:46Z","stars_delta_30d":0,"open_issues_delta_30d":0},"security_summary":{"status":"no_lockfile","scanner":null,"low_count":0,"high_count":0,"last_scan_at":"2026-07-11T10:53:37.247Z","medium_count":0,"scan_profile":"none","critical_count":0}},"capability_facts":{"scan":{"source":"repo_scan","observed_at":"2026-08-15T18:00:44.036Z"},"languages":{"value":["python"],"source":"github.language","observed_at":"2026-08-15T18:00:44.036Z"},"license_spdx":{"value":"MIT","source":"github.license","observed_at":"2026-08-15T18:00:44.036Z"}},"decision_facts":{"hosting":null,"pricing":null,"requirements":null,"constraints":null,"when_to_use":["When integrating lightweight prompt chaining functionality without the complexity of larger libraries","If you need easy to understand graphs for debugging chains similar to PyTorch","For quick implementation of popular AI approaches with a small number of lines of code like retrieval-augmented QA and chat applications with memory"],"when_not_to_use":["When seeking comprehensive features that only large, complex libraries offer, such as extensive example implementations or integrated support systems","If you require more advanced features not present in MiniChain for specialized AI applications"],"source":"enrich:decision_facts","observed_at":"2026-07-14T21:34:55.941Z"},"constraint_facets":null,"decision_summary":[{"label":"Adopt for","value":"MiniChain is a lightweight Python framework for using large language models through annotated function calls and Jinja-based prompt templating."}]}}