llm_xml_caster: Robust XML Caster for LLM Structured Output in Rust
llm_xml_caster is a powerful and reliable Rust library designed to simplify the process of extracting structured data from Large Language Models (LLMs) via XML. By leveraging Rust's type system and procedural macros, it enables developers to define data structures once and automatically generate precise XML schemas and prompts for LLMs, ensuring high-fidelity data casting.
Features
- Schema Generation: Automatically generates detailed XML schemas (including descriptions) from Rust structs and enums for precise LLM prompting.
- Type Safety: Integrates seamlessly with
serdefor safe and efficient deserialization. - Rich Type Support: Supports basic types, nested structs, enums,
Vec<T>,Option<T>, and more. - LLM Integration: Provides utilities like
generate_asandgenerate_as_with_retriesfor direct interaction (requires setting up thegenaidependency).
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0" # Current version, check crates.io for the latest
= { = "1.0", = ["derive"] }
Usage Example: Defining and Using Prompts
The core functionality is provided by the #[llm_prompt] procedural macro and the LlmPrompt trait.
1. Define Your Structure
Use #[llm_prompt] on your struct, and use #[prompt("...")] on fields to provide descriptions that guide the LLM's output.
use ;
use Deserialize;
2. Generate the XML Schema for LLM Prompting
The LlmPrompt trait is automatically implemented, allowing you to retrieve the schema required by the LLM:
let schema = get_prompt_schema;
// The schema output will be a detailed XML structure with descriptions:
// <SimpleStruct>
// <name> <!-- The name of the person --> </name>
// <age> <!-- The age of the person --> </age>
// <is_student> <!-- Whether the person is a student (true/false, yes/no) --> </is_student>
// </SimpleStruct>
// Also get the root element name:
let root_name = root_name; // "SimpleStruct"
3. Deserialize the LLM's XML Output
Use quick-xml (or similar deserializers) with the output generated by the LLM:
use from_str;
let xml_output = r#"
<SimpleStruct>
<name><![CDATA[John Doe]]></name>
<age>30</age>
<is_student>yes</is_student>
</SimpleStruct>
"#;
let decoded: SimpleStruct = from_str.unwrap;
assert_eq!;
assert_eq!; // Custom casting logic handles 'yes'
Advanced Types (Enums and Collections)
The macro handles complex types automatically:
Nested Structs
// Requires SimpleStruct to also implement LlmPrompt
Collections (Vectors and Options)
Enums (Sum Types)
Contributing
We welcome contributions! Please check the issues and pull requests on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.