Skip to content

Restrict tool choice to an allowed subset

Restrict tool choice to an allowed subset.

Run with: cargo run --release --example allowed_tools -p mistralrs

//! Restrict tool choice to an allowed subset.
//!
//! Run with: `cargo run --release --example allowed_tools -p mistralrs`
use std::collections::HashMap;
use anyhow::Result;
use mistralrs::{
AllowedToolChoice, AllowedToolsMode, AllowedToolsToolChoice, AllowedToolsToolChoiceType,
Function, IsqBits, ModelBuilder, RequestBuilder, TextMessageRole, Tool, ToolChoice, ToolType,
};
use serde_json::{json, Value};
#[tokio::main]
async fn main() -> Result<()> {
let model = ModelBuilder::new("Qwen/Qwen3-4B")
.with_logging()
.with_auto_isq(IsqBits::Eight)
.build()
.await?;
let parameters: HashMap<String, Value> = serde_json::from_value(json!({
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to use for the tool call.",
},
},
"required": ["city"],
}))?;
let tools = vec![
Tool {
tp: ToolType::Function,
function: Function {
description: Some("Get the current weather for a city.".to_string()),
name: "get_weather".to_string(),
parameters: Some(parameters.clone()),
strict: Some(true),
},
},
Tool {
tp: ToolType::Function,
function: Function {
description: Some("Book a flight to a city.".to_string()),
name: "book_flight".to_string(),
parameters: Some(parameters),
strict: Some(true),
},
},
];
let response = model
.send_chat_request(
RequestBuilder::new()
.add_message(
TextMessageRole::User,
"Use a tool to help me plan for Tokyo. Do not answer directly.",
)
.set_tools(tools)
.set_tool_choice(ToolChoice::AllowedTools(AllowedToolsToolChoice {
tp: AllowedToolsToolChoiceType::AllowedTools,
mode: AllowedToolsMode::Required,
tools: vec![AllowedToolChoice::Function {
name: "get_weather".to_string(),
}],
})),
)
.await?;
if let Some(tool_calls) = &response.choices[0].message.tool_calls {
let called = &tool_calls[0].function;
println!("Model called `{}` with {}", called.name, called.arguments);
}
Ok(())
}

Source: mistralrs/examples/advanced/allowed_tools/main.rs