Secretary
Secretary is a Rust library that transforms natural language into structured data using large language models (LLMs). With its powerful derive macro system, you can extract structured information from unstructured text with minimal boilerplate code.
Features
- ๐ Unified Task Trait: Single trait combining data extraction, schema definition, and system prompt generation with
#[derive(Task)] - ๐ Schema-Based Extraction: Define your data structure using Rust structs with field-level instructions
- ๐ Context-Aware Conversations: Maintain conversation state for multi-turn interactions
- ๐ Declarative Field Instructions: Use
#[task(instruction = "...")]attributes to guide extraction - โก Async Support: Built-in async/await support for concurrent processing
- ๐ Extensible LLM Support: Currently supports OpenAI API with more providers planned
- ๐ก๏ธ Type Safety: Leverage Rust's type system for reliable data extraction
- ๐งน Simplified API: Consolidated traits reduce boilerplate and complexity
Quick Start
Basic Example
use Task;
use OpenAILLM;
use GenerateJSON;
use ;
// Define your data structure with extraction instructions
How It Works
- Define Your Schema: Create a Rust struct with
#[derive(Task)]and field-level instructions - Add Required Fields: Include
contextandadditional_instructionsfields (marked with#[serde(skip)]) - Annotate Fields: Use
#[task(instruction = "...")]to guide the LLM on how to extract each field - Automatic Implementation: The derive macro implements all necessary traits (data model, system prompt generation, context management)
- Create Task Instance: Initialize with
YourStruct::new(additional_instructions) - Process Text: Send natural language input to an LLM through the Secretary API
- Get Structured Data: Receive JSON that can be parsed back into your struct
Field Instructions
The #[task(instruction = "...")] attribute tells the LLM how to extract each field:
Advanced Features
Async Processing
Secretary provides full async support for concurrent processing:
use AsyncGenerateJSON;
use tokio;
async
Context-Aware Conversations
Maintain conversation state for multi-turn interactions:
use Role;
System Prompt Generation
The derive macro automatically generates comprehensive system prompts:
let task = new;
let prompt = task.get_system_prompt;
println!;
// Output includes:
// - JSON structure specification
// - Field-specific instructions
// - Additional instructions
// - Formatting guidelines
Examples
The examples/ directory contains practical demonstrations:
Basic Usage
derive_example.rs- Basic person information extractionasync_example.rs- Async product information extraction with comprehensive testing
Run examples with:
# Basic example (no API key required for demo)
# Async example (no API key required for demo)
# To test with real API (uncomment API calls in examples):
Environment Setup
For production use with OpenAI:
In your code:
let api_key = var
.expect;
let llm = new?;
API Reference
Core Traits
Task- Main trait for data extraction, schema definition, and system prompt generation (auto-implemented by derive macro)GenerateJSON- Synchronous LLM interactionAsyncGenerateJSON- Asynchronous LLM interaction
LLM Providers
OpenAILLM- OpenAI API integration
Message Management
MessageList- Conversation context managementMessage- Individual conversation messagesRole- Message roles (User, Assistant, System)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
- Clone the repository
- Install Rust (latest stable)
- Run tests:
cargo test - Run examples:
cargo run --example async_example
License
This project is licensed under the MIT License - see the LICENSE file for details.