Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required method
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        input_json: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<String, ToolError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

LLMから呼び出し可能なツールを定義するトレイト

ツールはLLMが外部リソースにアクセスしたり、 計算を実行したりするために使用します。 セッション中の状態を保持できます。

§実装方法

通常は#[tool_registry]マクロを使用して自動実装します:

#[tool_registry]
impl MyApp {
    #[tool]
    async fn search(&self, query: String) -> String {
        format!("Results for: {}", query)
    }
}

// 登録
worker.register_tool(app.search_definition())?;

§手動実装

use llm_worker::tool::{Tool, ToolError, ToolMeta, ToolDefinition};
use std::sync::Arc;

struct MyTool { counter: std::sync::atomic::AtomicUsize }

#[async_trait::async_trait]
impl Tool for MyTool {
    async fn execute(&self, input: &str) -> Result<String, ToolError> {
        self.counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        Ok("result".to_string())
    }
}

let def: ToolDefinition = Arc::new(|| {
    (
        ToolMeta::new("my_tool")
            .description("My custom tool")
            .input_schema(serde_json::json!({"type": "object"})),
        Arc::new(MyTool { counter: Default::default() }) as Arc<dyn Tool>,
    )
});

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, input_json: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

ツールを実行する

§Arguments
  • input_json - LLMが生成したJSON形式の引数
§Returns

実行結果の文字列。この内容がLLMに返されます。

Implementors§