guidance

Constrain AI-generated text to structured formats using regex and grammars.

Updated Oct 23, 2024
One-click install
npx skills add https://github.com/lenadlm/docker --skill guidance-lenadlm
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: guidance
Source: https://github.com/lenadlm/docker/tree/main/hermes-skills/skills/mlops/inference/guidance
Command: npx skills add https://github.com/lenadlm/docker --skill guidance-lenadlm

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires guidance, transformers, llama_cpp, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve?

This Skill allows users to control the output of AI-generated text using regex, grammars, and structured output formats, ensuring accuracy and consistency.

Core Features & Use Cases

  • Control LLM Output: Use regex or grammars to enforce structured formats, such as valid JSON/XML/code generation.
  • Reduced Latency: Achieve faster generation with streamlined prompting and constrained outputs.
  • Use Cases: Suitable for tasks like automated code generation, data extraction, and structured output processing.

Quick Start

To use the guidance skill, load a model and specify the constraints. For example:

from guidance import models, gen

lm = models.OpenAI("gpt-4")
result = lm + "The capital of France is " + gen("capital", max_tokens=5)
print(result["capital"])  # "Paris"

Frequently Asked Questions about guidance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I constrain LLM output to a specific format like JSON or XML?

You can constrain LLM output to specific formats like JSON or XML by applying regex patterns and grammars during text generation. This enforces structured outputs, ensuring the model produces syntactically valid results for automated code generation and data extraction.

What is constrained generation and how does it reduce AI latency?

Constrained generation restricts AI models to specific grammars or regex patterns, streamlining prompting and limiting output scope. This structured approach reduces latency by preventing invalid generations and minimizing repeated parsing or retries.

Can I use regex to control AI text generation with OpenAI or Anthropic models?

Yes, you can use regex to control AI text generation with compatible backends like OpenAI, Anthropic, or llama.cpp. By loading a model and specifying constraints, you enforce formatting rules directly during the generation process.

Do I need the guidance library to generate structured output from LLMs?

Yes, you need the guidance library along with dependencies like transformers or llama_cpp to generate structured output. It provides the framework to define grammars and regex constraints that control model behavior and enforce formats.

What is the best way to ensure valid syntax for automated code generation?

The best way to ensure valid syntax for automated code generation is using constrained generation with grammars. By defining strict structural rules, you prevent syntax errors and ensure the AI-generated code adheres to required programming language specifications.

Why does structured output processing fail without grammars or regex constraints?

Structured output processing often fails without grammars or regex constraints because standard LLMs can deviate from required formats. Applying constraints forces the model to adhere to defined structures, preventing parsing errors during document processing.