lellm-derive 0.3.0

Derive macros for LeLLM — ToolSchema, ParallelSafety
Documentation
  • Coverage
  • 25%
    1 out of 4 items documented0 out of 3 items with examples
  • Size
  • Source code size: 32.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 370.03 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • lellm-ai/lellm
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • penghcn

lellm-derive — 派生宏与属性宏。

三级 API

Level 1: #[tool] 函数宏(推荐,95% 用户)

use lellm_agent::ToolResult;
use lellm_derive::tool;

#[tool(name = "search", description = "搜索互联网信息")]
async fn search(query: String, limit: Option<u32>) -> ToolResult {
    // 实现逻辑
    Ok(format!("搜索结果: {}", query))
}

// 无依赖 — 直接调用生成的工厂函数:
builder.tool(search_tool());

// 有依赖 — 使用 _with 后缀工厂函数:
let client = SearchClient::new();
builder.tool(search_tool_with({
    let client = client.clone();
    move |args| async move {
        client.search(&args.query, args.limit).await
    }
}));

Level 2: #[derive(Tool)] struct 宏(高级用户)

use lellm_agent::ToolResult;
use lellm_derive::Tool;

#[derive(Tool, JsonSchema)]
#[tool(name = "search", description = "搜索互联网信息")]
struct SearchArgs {
    /// 搜索关键词
    query: String,
    /// 返回数量
    limit: Option<u32>,
}

// 注册:
let reg = SearchArgs::safe(|args| async move {
    Ok(format!("搜索结果: {}", args.query))
});

Level 3: ToolRegistration::safe()(框架开发者)

use lellm_agent::{ToolDefinition, ToolRegistration};

let reg = ToolRegistration::safe(
    ToolDefinition {
        name: "search".to_string(),
        description: "搜索".to_string(),
        parameters: serde_json::json!({
            "type": "object",
            "properties": { "query": { "type": "string" } }
        }),
    },
    |args| async { Ok(args["query"].as_str().unwrap().to_string()) }
);