Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Wikipedia

WikipediaTool searches and retrieves article summaries from Wikipedia using the MediaWiki API. No API key is required.

Setup

[dependencies]
synaptic = { version = "0.4", features = ["tools"] }

Basic usage

use synaptic::tools::WikipediaTool;
use synaptic::core::Tool;
use serde_json::json;

let tool = WikipediaTool::new();

let result = tool.call(json!({ "query": "Large language model" })).await?;
println!("{}", serde_json::to_string_pretty(&result)?);

Configuration

// Default: English Wikipedia, up to 3 results
let tool = WikipediaTool::new();

// Custom language and result count
let tool = WikipediaTool::builder()
    .language("de")          // German Wikipedia
    .max_results(5)
    .build();

Configuration reference

FieldTypeDefaultDescription
languageString"en"Wikipedia language code (e.g. "en", "zh", "de")
max_resultsusize3Maximum number of articles to return

Tool parameters

ParameterTypeRequiredDescription
querystringyesSearch query to find Wikipedia articles

Response format

The tool returns a JSON array of article summaries:

{
  "query": "large language model",
  "results": [
    {
      "title": "Large language model",
      "url": "https://en.wikipedia.org/wiki/Large_language_model",
      "summary": "A large language model (LLM) is a type of machine learning model...",
      "extract": "A large language model (LLM) is a type of machine learning model designed to understand and generate human language..."
    }
  ]
}
FieldDescription
titleArticle title
urlFull Wikipedia URL
summaryShort description (1–2 sentences)
extractLonger text extract from the article

Use with an agent

use synaptic::tools::WikipediaTool;
use synaptic::core::Tool;
use synaptic::models::OpenAiChatModel;
use synaptic::graph::create_react_agent;
use std::sync::Arc;

let model = Arc::new(OpenAiChatModel::from_env()?);
let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(WikipediaTool::new())];

let agent = create_react_agent(model, tools);

Combining DuckDuckGo and Wikipedia

For richer research agents, combine both tools:

use synaptic::tools::{DuckDuckGoTool, WikipediaTool};
use synaptic::core::Tool;
use std::sync::Arc;

let tools: Vec<Arc<dyn Tool>> = vec![
    Arc::new(DuckDuckGoTool::new()),
    Arc::new(WikipediaTool::new()),
];

Error handling

use synaptic::core::SynapticError;

match tool.call(json!({ "query": "Rust programming" })).await {
    Ok(result) => {
        for article in result["results"].as_array().unwrap_or(&vec![]) {
            println!("{}: {}", article["title"], article["summary"]);
        }
    }
    Err(SynapticError::Tool(msg)) => eprintln!("Wikipedia error: {msg}"),
    Err(e) => return Err(e.into()),
}