https://github.com/guidance-ai/guidance Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Customer Stories + White papers, Ebooks, Webinars + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} guidance-ai / guidance Public * Notifications * Fork 847 * Star 13.2k A guidance language for controlling large language models. License MIT license 13.2k stars 847 forks Activity Star Notifications * Code * Issues 124 * Pull requests 34 * Discussions * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Discussions * Actions * Projects * Security * Insights guidance-ai/guidance This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 9 branches 20 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/g] Use Git or checkout with SVN using the web URL. [gh repo clone guidan] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @Harsha-Nori Harsha-Nori Updating URLs and merging MAINTAINERS update. ... 23d0ba1 Aug 12, 2023 Updating URLs and merging MAINTAINERS update. 23d0ba1 Git stats * 605 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github Fix branch name June 14, 2023 17:45 client/src Fix bugs, add tests April 1, 2023 07:15 docs Updating URLs to match new project organization. August 11, 2023 18:20 guidance Merge pull request #336 from emrekiciman/main August 9, 2023 12:42 notebooks Updating URLs to match new project organization. August 11, 2023 18:20 tests Merge branch 'main' into main June 20, 2023 16:48 .gitignore adding support for functions June 15, 2023 17:56 CONTRIBUTING.md Putting Guidance under Minimal Viable Governance (MVG) structure. August 11, 2023 13:26 GOVERNANCE.md Putting Guidance under Minimal Viable Governance (MVG) structure. August 11, 2023 13:26 LICENSE.md Putting Guidance under Minimal Viable Governance (MVG) structure. August 11, 2023 13:26 MAINTAINERS.md Update MAINTAINERS.md August 11, 2023 13:42 README.md Updating URLs to match new project organization. August 11, 2023 18:20 SECURITY.md Microsoft mandatory file June 1, 2023 18:23 setup.py Fix #266 by adding pyparsing version June 20, 2023 16:28 View code [ ] Install Live streaming (notebook) Chat dialog (notebook) Guidance acceleration (notebook) Token healing (notebook) Rich output structure example (notebook) Guaranteeing valid syntax JSON example (notebook) Role-based chat model example (notebook) Agents (notebook) GPT4 + Bing API reference Template syntax Generation Basic generation Selecting Sequences of generate/select Hidden generation Generate with n>1 Calling functions Pausing execution with await Notebook functions Chat (see also this notebook) Agents with geneach Using tools README.md [6874747073] guidance Where there is no guidance, a model fails, but in an abundance of instructions there is safety. - GPT 11:14 Guidance enables you to control modern language models more effectively and efficiently than traditional prompting or chaining. Guidance programs allow you to interleave generation, prompting, and logical control into a single continuous flow matching how the language model actually processes the text. Simple output structures like Chain of Thought and its many variants (e.g., ART, Auto-CoT, etc.) have been shown to improve LLM performance. The advent of more powerful LLMs like GPT-4 allows for even richer structure, and guidance makes that structure easier and cheaper. Features: * [*] Simple, intuitive syntax, based on Handlebars templating. * [*] Rich output structure with multiple generations, selections, conditionals, tool use, etc. * [*] Playground-like streaming in Jupyter/VSCode Notebooks. * [*] Smart seed-based generation caching. * [*] Support for role-based chat models (e.g., ChatGPT). * [*] Easy integration with Hugging Face models, including guidance acceleration for speedups over standard prompting, token healing to optimize prompt boundaries, and regex pattern guides to enforce formats. Install pip install guidance Live streaming (notebook) Speed up your prompt development cycle by streaming complex templates and generations live in your notebook. At first glance, Guidance feels like a templating language, and just like standard Handlebars templates, you can do variable interpolation (e.g., {{proverb}}) and logical control. But unlike standard templating languages, guidance programs have a well defined linear execution order that directly corresponds to the token order as processed by the language model. This means that at any point during execution the language model can be used to generate text (using the {{gen}} command) or make logical control flow decisions. This interleaving of generation and prompting allows for precise output structure that produces clear and parsable results. import guidance # set the default language model used to execute guidance programs guidance.llm = guidance.llms.OpenAI("text-davinci-003") # define a guidance program that adapts a proverb program = guidance("""Tweak this proverb to apply to model instructions instead. {{proverb}} - {{book}} {{chapter}}:{{verse}} UPDATED Where there is no guidance{{gen 'rewrite' stop="\\n-"}} - GPT {{#select 'chapter'}}9{{or}}10{{or}}11{{/select}}:{{gen 'verse'}}""") # execute the program on a specific proverb executed_program = program( proverb="Where there is no guidance, a people falls,\nbut in an abundance of counselors there is safety.", book="Proverbs", chapter=11, verse=14 ) [proverb_animation] After a program is executed, all the generated variables are now easily accessible: executed_program["rewrite"] ', a model fails,\nbut in an abundance of instructions there is safety.' Chat dialog (notebook) Guidance supports API-based chat models like GPT-4, as well as open chat models like Vicuna through a unified API based on role tags (e.g., {{#system}}...{{/system}}). This allows interactive dialog development that combines rich templating and logical control with modern chat models. # connect to a chat model like GPT-4 or Vicuna gpt4 = guidance.llms.OpenAI("gpt-4") # vicuna = guidance.llms.transformers.Vicuna("your_path/vicuna_13B", device_map="auto") experts = guidance(''' {{#system~}} You are a helpful and terse assistant. {{~/system}} {{#user~}} I want a response to the following question: {{query}} Name 3 world-class experts (past or present) who would be great at answering this? Don't answer the question yet. {{~/user}} {{#assistant~}} {{gen 'expert_names' temperature=0 max_tokens=300}} {{~/assistant}} {{#user~}} Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer. {{~/user}} {{#assistant~}} {{gen 'answer' temperature=0 max_tokens=500}} {{~/assistant}} ''', llm=gpt4) experts(query='How can I be more productive?') [chat_animation] Guidance acceleration (notebook) When multiple generation or LLM-directed control flow statements are used in a single Guidance program then we can significantly improve inference performance by optimally reusing the Key/Value caches as we progress through the prompt. This means Guidance only asks the LLM to generate the green text below, not the entire program. This cuts this prompt's runtime in half vs. a standard generation approach. # we use LLaMA here, but any GPT-style model will do llama = guidance.llms.Transformers("your_path/llama-7b", device=0) # we can pre-define valid option sets valid_weapons = ["sword", "axe", "mace", "spear", "bow", "crossbow"] # define the prompt character_maker = guidance("""The following is a character profile for an RPG game in JSON format. ```json { "id": "{{id}}", "description": "{{description}}", "name": "{{gen 'name'}}", "age": {{gen 'age' pattern='[0-9]+' stop=','}}, "armor": "{{#select 'armor'}}leather{{or}}chainmail{{or}}plate{{/select}}", "weapon": "{{select 'weapon' options=valid_weapons}}", "class": "{{gen 'class'}}", "mantra": "{{gen 'mantra' temperature=0.7}}", "strength": {{gen 'strength' pattern='[0-9]+' stop=','}}, "items": [{{#geneach 'items' num_iterations=5 join=', '}}"{{gen 'this' temperature=0.7}}"{{/geneach}}] }```""") # generate a character character_maker( id="e1f491f7-7ab8-4dac-8c20-c92b5e7d883d", description="A quick and nimble fighter.", valid_weapons=valid_weapons, llm=llama ) [json_animation] The prompt above typically takes just over 2.5 seconds to complete on a A6000 GPU when using LLaMA 7B. If we were to run the same prompt adapted to be a single generation call (the standard practice today) it takes about 5 seconds to complete (4 of which is token generation and 1 of which is prompt processing). This means Guidance acceleration delivers a 2x speedup over the standard approach for this prompt. In practice the exact speed-up factor depends on the format of your specific prompt and the size of your model (larger models benefit more). Acceleration is also only supported for Transformers LLMs at the moment. See the notebook for more details. Token healing (notebook) The standard greedy tokenizations used by most language models introduce a subtle and powerful bias that can have all kinds of unintended consequences for your prompts. Using a process we call "token healing" guidance automatically removes these surprising biases, freeing you to focus on designing the prompts you want without worrying about tokenization artifacts. Consider the following example, where we are trying to generate an HTTP URL string: # we use StableLM as an open example, but these issues impact all models to varying degrees guidance.llm = guidance.llms.Transformers("stabilityai/stablelm-base-alpha-3b", device=0) # we turn token healing off so that guidance acts like a normal prompting library program = guidance('''The link is