|
| 1 | +# rustia-llm |
| 2 | + |
| 3 | +`rustia-llm` provides a typed adapter from Rust `rustia` models to `aisdk::core::tools::Tool`. |
| 4 | + |
| 5 | +The adapter enforces rustia's three-layer harness for tool input: |
| 6 | + |
| 7 | +1. Lenient JSON parsing |
| 8 | +2. Parse-time type coercion |
| 9 | +3. Validation feedback |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```rust |
| 14 | +use schemars::JsonSchema; |
| 15 | +use serde::{Deserialize, Serialize}; |
| 16 | +use rustia::LLMData; |
| 17 | +use rustia_llm::{LlmToolSpec, tool}; |
| 18 | + |
| 19 | +#[derive(Debug, Serialize, Deserialize, JsonSchema, LLMData)] |
| 20 | +struct WeatherInput { |
| 21 | + city: String, |
| 22 | + days: u8, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Serialize, Deserialize)] |
| 26 | +struct WeatherOutput { |
| 27 | + summary: String, |
| 28 | +} |
| 29 | + |
| 30 | +fn main() { |
| 31 | + let weather_tool = tool::<WeatherInput, WeatherOutput, _, std::convert::Infallible>( |
| 32 | + LlmToolSpec::new( |
| 33 | + "get_weather", |
| 34 | + "Get a short weather summary for a city and forecast length", |
| 35 | + ), |
| 36 | + |input| { |
| 37 | + Ok(WeatherOutput { |
| 38 | + summary: format!("{} forecast for {} day(s)", input.city, input.days), |
| 39 | + }) |
| 40 | + }, |
| 41 | + ) |
| 42 | + .expect("tool should build"); |
| 43 | + |
| 44 | + let output = weather_tool |
| 45 | + .execute |
| 46 | + .call(serde_json::json!({ "city": "seoul", "days": "2" })) |
| 47 | + .expect("tool execution should succeed"); |
| 48 | + |
| 49 | + println!("{output}"); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## API Key Setup |
| 54 | + |
| 55 | +`rustia-llm` only builds typed `Tool` adapters. API key configuration is handled by |
| 56 | +the `aisdk` provider you use (for example, OpenAI). |
| 57 | + |
| 58 | +If you use OpenAI, enable the provider feature in your app crate: |
| 59 | + |
| 60 | +```bash |
| 61 | +cargo add aisdk --features openai |
| 62 | +``` |
| 63 | + |
| 64 | +### Option A: Environment Variable (recommended) |
| 65 | + |
| 66 | +```bash |
| 67 | +export OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>" |
| 68 | +``` |
| 69 | + |
| 70 | +Then use your tool with `aisdk`: |
| 71 | + |
| 72 | +```rust |
| 73 | +use aisdk::{core::LanguageModelRequest, providers::OpenAI}; |
| 74 | + |
| 75 | +let response = LanguageModelRequest::builder() |
| 76 | + .model(OpenAI::gpt_5()) |
| 77 | + .prompt("Use the provided tool to answer the request.") |
| 78 | + .with_tool(weather_tool) |
| 79 | + .build() |
| 80 | + .generate_text() |
| 81 | + .await?; |
| 82 | +``` |
| 83 | + |
| 84 | +### Option B: Explicit API Key in Provider Builder |
| 85 | + |
| 86 | +```rust |
| 87 | +use aisdk::providers::OpenAI; |
| 88 | + |
| 89 | +let mut openai = OpenAI::gpt_5(); |
| 90 | +openai.settings.api_key = "<YOUR_OPENAI_API_KEY>".to_owned(); |
| 91 | +``` |
| 92 | + |
| 93 | +Use `openai` as the model in `LanguageModelRequest::builder().model(openai)`. |
| 94 | + |
| 95 | +## API Surface |
| 96 | + |
| 97 | +- `LlmToolInput`: `rustia::LLMData + schemars::JsonSchema + Send + Sync + 'static` |
| 98 | +- `LlmToolOutput`: `serde::Serialize + Send + Sync + 'static` |
| 99 | +- `LlmToolSpec` |
| 100 | +- `tool<I, O, F, E>(spec, handler) -> Result<aisdk::core::tools::Tool, LlmToolBuildError>` |
| 101 | + |
| 102 | +## Error Model |
| 103 | + |
| 104 | +- `LlmToolBuildError` |
| 105 | +- `LlmToolInputError` |
| 106 | +- `LlmToolExecutionError` |
| 107 | + |
| 108 | +Input parsing and validation failures return deterministic feedback text in the tool error payload, including rustia validation paths (`$input...`) and expected constraints. |
| 109 | + |
| 110 | +## Local Validation |
| 111 | + |
| 112 | +Run from repository root: |
| 113 | + |
| 114 | +```bash |
| 115 | +cargo test -p rustia-llm |
| 116 | +cargo test --workspace --all-targets |
| 117 | +``` |
| 118 | + |
| 119 | +## Documentation Links |
| 120 | + |
| 121 | +- Project index: [`docs/project-rustia.md`](../../docs/project-rustia.md) |
| 122 | +- rustia-llm contract: [`docs/crates-rustia-llm-foundation.md`](../../docs/crates-rustia-llm-foundation.md) |
| 123 | +- rustia core contract: [`docs/crates-rustia-core-foundation.md`](../../docs/crates-rustia-core-foundation.md) |
0 commit comments