Skip to main content

spec_for

Function spec_for 

pub fn spec_for<A>(
    name: impl Into<String>,
    description: impl Into<String>,
) -> ToolSpec
where A: JsonSchema,
Expand description

The [ToolSpec] of a tool whose arguments are A: its parameters are the JSON Schema of A.

A must serialize as an object (a struct; a unit struct with braces for a tool without parameters). The schema is draft 2020-12 with subschemas inlined where possible, no $schema and no title (a title is noise for the model). Doc comments on A and on its fields become descriptions, and Option<T> fields are not required: that is what #[derive(JsonSchema)] does. A recursive type refers back with $ref.

use adam_llm_agent::spec_for;
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct Args {
    /// What you need to know
    question: String,
    /// Give up after this many seconds
    timeout: Option<u32>,
}

let spec = spec_for::<Args>("ask_user", "Ask the user a question.");
assert_eq!(spec.name, "ask_user");
assert_eq!(spec.parameters["required"], serde_json::json!(["question"]));
assert_eq!(
    spec.parameters["properties"]["question"]["description"],
    "What you need to know"
);
assert!(spec.parameters.get("title").is_none());