DSRs (DSPy Rust)

repository·main·Indexed 43 days ago

https://github.com/krypticmouse/dsrs

A high-performance rewrite of the DSPy framework in Rust for building type-safe and efficient applications powered by Language Models. DSRs version 0.7.3 provides modular components including Signatures, Modules, and Optimizers such as COPRO, MIPROv2, and GEPA to refine prompts and instructions.

Tokens
187.5K
Snippets
390
Records
727
Agent score
55%

What's inside DSRs

  1. Overview of dspy_rs::typesys

    main
    The dspy_rs::typesys module provides an in-house type system designed to replace the vendored BAML stack (including bamltype, baml_types, internal_baml_jinja, and jsonish). It manages type definitions, schema rendering for prompts, tolerant coercion of LLM outputs, and runtime constraint evaluation.
  2. Overview of DSRs

    main

    DSRs is a Rust library for building LM-powered applications using typed contracts instead of raw prompts. It allows developers to treat LM pipelines as data that is printable, diffable, replayable, and optimizable.

    Key features include:

    • Typed Contracts: Define inputs, outputs, and instructions as Rust structs. The library handles prompt rendering and response parsing into these types.
    • Programs as Data: Using the #[module] macro, code is projected into a .dsrs artifact that can be served over HTTP or embedded in builds.
    • Recorded Runs: Every execution is captured as a trace, enabling deterministic testing via strict replay or targeted re-runs via until-divergence replay.
    • Optimizers: Includes COPRO, MIPROv2, GEPA, SIMBA, and BootstrapFewShot to tune instructions and few-shot demos against metrics.
    • Tools & Agents: Supports models calling typed Rust tools or writing sandboxed JavaScript programs in 'Code Mode'.
  3. The .dsrs file format overview

    main

    A .dsrs file is the canonical text representation of a program. It consists of declarations followed by exactly one main block. The program's identity (hash) is derived from this text, excluding the lineage block.

    General Syntax Rules:

    • // starts a comment.
    • Whitespace is insignificant (except inside js``` ``` code fences).
    • Strings must be JSON strings.
    • Reserved Words: You cannot use any of the following as names: dsrs program caps model sig class enum tool lineage main in out predict cot agent hole seq fork join route retry refine loop else js demos string int float bool map true false null while carry.

    File Skeleton:

    dsrs 1
    program qa
    
    caps { net:search }
    
    model fast = "openai:gpt-4o-mini" { temperature 0.2 }
    
    sig Main {
      in  question: string
      out answer: string
    }
    
    main: Main = seq {
      ...
      out { answer = ... }
    }
  4. Use dsrs-tools for sandboxed tool execution

    main
    The dsrs-tools crate provides sandboxed execution environments for tools used within DSRs. It primarily leverages QuickJsExecutor to run JavaScript-based tools in a secure, isolated manner. This allows for safe execution of code-mode tools and custom logic without compromising the host system.
  5. Understand the `dspy_rs::ir` module structure

    main

    The dspy_rs::ir (Intermediate Representation) module is the core of the system, providing the building blocks for defining, constructing, and executing programs. It is organized into several specialized sub-modules:

    • graph: The core IR graph containing entity IDs, the Interner, Node enums, and the Program structure.
    • sig: Value-level signatures (SignatureDef) defining inputs and outputs.
    • builder: The Rust frontend used to programmatically construct a Program (equivalent to what the .dsrs text parser produces).
    • interp: The IR interpreter used for async evaluation of a loaded Program.
    • text: Support for the .dsrs text format.
    • edit: The graph-edit calculus used for structural mutations of the IR.
    • params: Management of named, addressable parameter slots.
    • validate: Load-time validation logic to ensure program integrity.
    • bridge: Mechanisms to bridge fx/ModuleState and Overlay contexts.
    • sig: Value-level signatures (SignatureDef) defining inputs and outputs.
    • step: Metadata for individual execution steps.
    • module_build: Library support for the module "linker".
  6. Explore the DSRs public API surface

    main

    The DSRs API is distributed across several specialized crates. For full signatures, method details, and trait implementations, refer to the official documentation on docs.rs for each specific crate:

    • dspy-rs: The primary crate containing core signatures, predictors, modules, Intermediate Representation (IR), traces, and optimizers.
    • dsrs-tools: Provides sandboxed tool execution and Code Mode capabilities.
    • dsrs-macros: Contains the procedural macros used for the derive and attribute syntax.
    • dsrs-syntax: Defines the shared .dsrs lexer and structural grammar used by all frontends.
  7. What is a DSPy Signature?

    main

    A Signature is a typed contract between a module and a Language Model (LM). It defines named input fields, named output fields, and instructions. This declarative approach allows the framework to handle prompt construction, output parsing, and type validation automatically.

    Key Concept: A Signature is a class, not an instance. When you define or create a signature, you are working with a new type (a dynamically-created Pydantic BaseModel subclass). Operations like prepend, with_instructions, or delete do not mutate the existing signature; instead, they return a new Signature class.

  8. What is Code Mode and how does it work?

    main

    Code Mode is a high-efficiency alternative to standard JSON tool calling. Instead of the model making multiple round trips to the host for each tool invocation, it uses a single meta-tool called run_js.

    In Code Mode, the model sees your tools as a JavaScript API. It writes a single script that calls these tools as global functions, composes their results, and returns a final value. This reduces latency and token usage by replacing multiple model turns with a single execution step.

    Key Benefits:

    • Reduced Latency: One model round trip instead of $N$ trips for $N$ tools.
    • Reduced Token Usage: Intermediate tool results stay within the sandbox and don't necessarily need to flow through the model's context.
    • Composition: The model can easily pipe the output of one tool into the input of another using standard JavaScript logic.
  9. What is DSPy and how does it work?

    main

    DSPy is a framework for programming with language models where you declare what you want using typed signatures, rather than manually constructing prompts. The framework manages prompt construction, output parsing, and automatic optimization of prompts and few-shot examples.

    At its core, DSPy is built on the Predict primitive. A Predict instance takes a typed signature (defining input and output fields), uses an Adapter to format it into a prompt, calls a Language Model (LM), and parses the response back into typed outputs. Higher-level modules like ChainOfThought or ReAct are simply orchestrations of multiple Predict instances.

  10. What is GEPA (Genetic-Pareto)?

    main

    GEPA is a reflective prompt optimizer that evolves instructions using textual feedback provided by your metrics. Unlike optimizers that rely solely on scalar scores, GEPA uses detailed explanations of failures to drive an LLM-based reflection process. It maintains a Pareto frontier of candidates, meaning it preserves a diverse set of instructions that excel on different subsets of examples, preventing premature convergence to a single mediocre solution.

    Key features include:

    • Rich Textual Feedback: Uses detailed explanations (e.g., why a classification failed) instead of just numbers.
    • Pareto-based Selection: Keeps candidates with complementary strengths.
    • LLM-driven Reflection: Uses a prompt_model to propose targeted instruction rewrites based on feedback and execution traces.
    • Inference-time Search: Capable of optimizing at test time.
  11. Overview of the DSRs ecosystem components

    main

    The DSRs system is completed by several key mechanisms that manage security, observability, and optimization:

    • Capabilities: These gate what a program is allowed to do. The program declares its requirements in the .dsrs artifact, and the host must grant them. Loading fails if there is a mismatch.
    • Traces: These record every execution. They allow for Strict Replay (reproducing a run with zero live calls for deterministic testing) and Until-divergence Replay (re-running only the steps affected by a specific change).
    • Evaluation & Optimizers: These create a feedback loop. An Evaluation metric scores outputs, and Optimizers use those scores to search for better instruction sets and demonstration (few-shot) candidates.