{"name":"LMQL: A Language for Constraint-Guided and Efficient LLM Programming","description":"LMQL is an innovative programming language that extends Python, designed for efficient and constraint-guided programming of Large Language Models (LLMs). It allows developers to interweave traditional programming logic with native LLM calls, offering advanced control over model behavior through features like constraints, rich control flow, and optimized runtimes. This makes it easier to build complex LLM-powered applications with greater precision and efficiency.","github":"https://github.com/eth-sri/lmql","url":"https://osrepos.com/repo/eth-sri-lmql","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/eth-sri-lmql","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/eth-sri-lmql.md","json":"https://osrepos.com/repo/eth-sri-lmql.json","topics":["Python","LLM Programming","Language Models","ChatGPT","Hugging Face","AI","Constraint Programming"],"keywords":["Python","LLM Programming","Language Models","ChatGPT","Hugging Face","AI","Constraint Programming"],"stars":null,"summary":"LMQL is an innovative programming language that extends Python, designed for efficient and constraint-guided programming of Large Language Models (LLMs). It allows developers to interweave traditional programming logic with native LLM calls, offering advanced control over model behavior through features like constraints, rich control flow, and optimized runtimes. This makes it easier to build complex LLM-powered applications with greater precision and efficiency.","content":"## Introduction\n\nLMQL is an innovative programming language designed for Large Language Models (LLMs), functioning as a superset of Python. It offers a novel approach to interweaving traditional programming logic with native LLM calls directly within your code. Unlike conventional templating languages, LMQL integrates LLM interaction at the program code level, enabling developers to build sophisticated LLM-powered applications with enhanced control and efficiency. It emphasizes constraint-guided programming, allowing precise control over model outputs.\n\n## Installation\n\nGetting started with LMQL is straightforward. Ensure you have Python 3.10 installed, then run the following command:\n\nbash\npip install lmql\n\n\nFor users who wish to run models on a local GPU, it is recommended to install LMQL in an environment with a GPU-enabled PyTorch (version >= 1.11) and then install with:\n\nbash\npip install lmql[hf]\n\n\nYou can also launch the LMQL playground IDE after installation by running `lmql playground` to explore examples in a browser-based environment.\n\n## Examples\n\nLMQL programs seamlessly blend Python syntax with LLM queries. Top-level strings are interpreted as query strings, where template variables like `[GREETINGS]` are automatically completed by the model. The `where` keyword allows you to specify constraints on the generated text, providing fine-grained control over LLM behavior.\n\nConsider this example from the LMQL documentation:\n\npython\n\"Greet LMQL:[GREETINGS]\\n\" where stops_at(GREETINGS, \".\") and not \"\\n\" in GREETINGS\n\nif \"Hi there\" in GREETINGS:\n    \"Can you reformulate your greeting in the speech of \\\n     victorian-era English: [VIC_GREETINGS]\\n\" where stops_at(VIC_GREETINGS, \".\")\n\n\"Analyse what part of this response makes it typically victorian:\\n\"\n\nfor i in range(4):\n    \"-[THOUGHT]\\n\" where stops_at(THOUGHT, \".\")\n\n\"To summarize:[SUMMARY]\"\n\n\nThis snippet demonstrates how LMQL allows you to combine algorithmic logic (like `if` statements and `for` loops) with LLM prompts, guiding the model's reasoning process and constraining its outputs effectively.\n\n## Why Use LMQL?\n\nLMQL is engineered to make working with language models more efficient and powerful. Its key advantages include:\n\n*   **Python Syntax Integration**: Write queries using familiar Python syntax, fully integrated with your existing Python environment.\n*   **Rich Control-Flow**: Leverage full Python support for powerful control flow and logic within your prompting.\n*   **Advanced Decoding**: Benefit from sophisticated decoding techniques such as beam search and `best_k`.\n*   **Powerful Constraints**: Apply precise constraints to model output using logit masking, controlling token length, character-level patterns, data types, and stopping phrases.\n*   **Optimized Runtime**: LMQL employs speculative execution, constraint short-circuiting, and tree-based caching for faster inference and efficient token usage.\n*   **Multi-Model Support**: Seamlessly integrate with OpenAI API, Azure OpenAI, and Hugging Face Transformers models.\n*   **Extensive Applications**: Implement advanced applications like schema-safe JSON decoding, algorithmic prompting, and interactive chat interfaces.\n\n## Links\n\n*   [Documentation](https://lmql.ai/docs \"LMQL Documentation\"){target=\"_blank\"}\n*   [Playground IDE](https://lmql.ai/playground \"LMQL Playground IDE\"){target=\"_blank\"}\n*   [GitHub Repository](https://github.com/eth-sri/lmql \"LMQL GitHub Repository\"){target=\"_blank\"}\n*   [Example Showcase](https://lmql.ai \"LMQL Example Showcase\"){target=\"_blank\"}","metrics":{"detailViews":11,"githubClicks":16},"dates":{"published":null,"modified":"2026-01-17T12:01:02.000Z"}}