### Minimal C/C++ Example for llguidance Source: https://github.com/guidance-ai/llguidance/blob/main/README.md Shows a basic integration of the llguidance library in C/C++. This example requires the llguidance C/C++ headers to be correctly included. ```c #include "llguidance.h" int main() { llguidance_context_t* context = llguidance_context_new(); llguidance_state_t* state = llguidance_state_new(); // Example: Generate a token const char* token = llguidance_generate_token(context, state, "Hello "); printf("%s\n", token); llguidance_context_free(context); llguidance_state_free(state); return 0; } ``` -------------------------------- ### Minimal Rust Example for llguidance Source: https://github.com/guidance-ai/llguidance/blob/main/README.md Demonstrates the basic usage of the llguidance library in Rust. Ensure the llguidance library is correctly set up and imported. ```rust use llguidance::*; fn main() { let mut context = Context::new(); let mut state = State::new(); // Example: Generate a token let token = generate_token(&mut context, &mut state, "Hello "); println!("{}", token); } ``` -------------------------------- ### Run project tests Source: https://github.com/guidance-ai/llguidance/blob/main/CONTRIBUTING.md Commands to execute Rust tests or install and test the Python bindings. ```bash # Run Rust tests (debug) cargo nextest run # Run Rust tests (release) cargo nextest run --release # Run Python tests (after installing) pip install -v -e . pytest ``` -------------------------------- ### Injecting Unified Tool Call Instructions Source: https://github.com/guidance-ai/llguidance/blob/main/docs/toolcalls.md Appends system prompt instructions to guide models towards a standardized tool call format. Ensure 'funcs' is a JSON-serializable object representing available functions. ```python system_prompt += r""" In addition to plain text responses, you can choose to call one or more of the provided functions. Use the following rule to decide when to call a function: * if the response can be generated from your internal knowledge (e.g., as in the case of queries like "What is the capital of Poland?"), do so * if you need external information that can be obtained by calling one or more of the provided functions, generate a function calls If you decide to call functions: * prefix function calls with functools marker (no closing marker required) * all function calls should be generated in a single JSON list formatted as functools[{"name": [function name], "arguments": [function arguments as JSON]}, ...] * follow the provided JSON schema. Do not hallucinate arguments or values. Do not blindly copy values from the provided samples * respect the argument type formatting. E.g., if the type is number and format is float, write value 7 as 7.0 * make sure you pick the right functions that match the user intent """ system_prompt += json.dumps(funcs, indent=2) ``` -------------------------------- ### Lexeme Options Example Source: https://github.com/guidance-ai/llguidance/blob/main/docs/syntax.md Demonstrates lexeme options for terminals, including stop conditions, maximum tokens, and temperature. Use this to control generation within specific regex or named rules. ```lark mygen[stop="\n", max_tokens=10, temperature=0.7]: /.*/ ``` -------------------------------- ### Build sample_parser Binary Source: https://github.com/guidance-ai/llguidance/blob/main/sample_parser/README.md Builds the `sample_parser` binary, with release mode recommended for production use with real tokenizers. ```bash cargo build --release -p sample_parser ``` -------------------------------- ### Lazy Grammar Example Source: https://github.com/guidance-ai/llguidance/blob/main/plan.md This example demonstrates a lazy grammar structure using `gen()` and `select()`. It shows how literals and regexes are combined to form grammar rules, with implicit or explicit stop conditions. ```python "Name: " + gen(regex=r'[A-Z][a-z]+', stop='\n')) + "\n" + select([ "Married? " + gen(regex=r'Yes|No'), "Age: " + gen(regex=r'\d+', stop='\n') ]) + "\n" ``` -------------------------------- ### Grammar Rule Construction Source: https://github.com/guidance-ai/llguidance/blob/main/plan.md This shows how the previously defined lexemes are combined to form the complete grammar rule for the lazy parsing example. ```python L0 + L1 + L2 + select([L3 + L4, L5 + L6]) + L2 ``` -------------------------------- ### CMake Project Configuration for C FFI Tests Source: https://github.com/guidance-ai/llguidance/blob/main/c_ffi_tests/CMakeLists.txt Initializes the project, sets C++20 standards, and locates the required Boost unit test framework. ```cmake cmake_minimum_required(VERSION 3.16) project(c_ffi_tests CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Find Boost.Test find_package(Boost REQUIRED COMPONENTS unit_test_framework) ``` -------------------------------- ### Inline JSON Schema Definition in Lark Source: https://github.com/guidance-ai/llguidance/blob/main/docs/syntax.md Defines a JSON function calling schema for Meta Llama 3.1. Strings starting with '{' will be forced to follow this schema. ```lark start: TEXT | fun_call TEXT: /[^{](.| )*/ fun_call: %json { "type": "object", "properties": { "name": { "const": "get_weather" }, "parameters": { "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] } }, "required": ["name", "parameters"] } ``` -------------------------------- ### Lexeme Definitions for Lazy Grammar Source: https://github.com/guidance-ai/llguidance/blob/main/plan.md These are the regular expressions identified as lexemes (terminals) from the preceding lazy grammar example. They define the basic building blocks for parsing. ```python L0 = r"Name: " L1 = r"[A-Z][a-z]+\n" L2 = r"\n" L3 = r"Married\? " # (note quote on '?') L4 = r"Yes|No" L5 = r"Age: " L6 = r"\d+\n" ``` -------------------------------- ### Lint the codebase Source: https://github.com/guidance-ai/llguidance/blob/main/CONTRIBUTING.md Commands to verify formatting, run clippy, and check crate features. ```bash # Check formatting cargo fmt --check # Run clippy cargo clippy --workspace --all-targets --all-features -- -D warnings # Check that the crate builds without default features cargo check --no-default-features -p llguidance ``` -------------------------------- ### Validate Input with JSON Schema Source: https://github.com/guidance-ai/llguidance/blob/main/sample_parser/README.md Validates a known-good JSON input against a JSON Schema using the `minimal` binary. This is a good starting point for understanding the core API. ```bash cargo run --bin minimal -- data/blog.schema.json data/blog.sample.json ``` -------------------------------- ### CLI Options for Test Suite Runner Source: https://github.com/guidance-ai/llguidance/blob/main/json_schema_test_suite/README.md Overview of command-line arguments and options available for the JSON Schema Test Suite Runner, including specifying the test suite directory, baseline file, drafts, and update behavior. ```text Arguments: [SUITE_DIR] Path to JSON-Schema-Test-Suite checkout (auto-cloned if omitted) Options: --expected Baseline file for ratchet comparison --draft Draft(s) to run (repeatable). Without this: runs all drafts in the baseline, or draft2020-12 if no baseline --update Overwrite the baseline with current results ```