Skip to main content

lmstudio_schema/
lmstudio-schema.rs

1use anylm::{Chunk, Completions, Schema, Tool, prelude::*};
2
3#[tokio::main]
4async fn main() -> Result<()> {
5    /// The weather tool data
6    #[allow(dead_code)]
7    #[derive(Debug, serde::Deserialize)]
8    struct LocationData {
9        location: String,
10    }
11
12    // send request:
13    let mut response = Completions::lmstudio("", "mistralai/ministral-3-3b")
14        .user_message(vec!["What's the weather like in London?".into()])
15        .tool(Tool::new(
16            "weather",
17            "Search weather by location",
18            Schema::object("Location data")
19                .required_property("location", Schema::string("The location")),
20        ))
21        .send()
22        .await?;
23
24    // read response stream:
25    let mut tool_calls = vec![];
26    while let Some(chunk) = response.next().await {
27        match chunk? {
28            Chunk::Text(text) => {
29                eprint!("{text}");
30            }
31            Chunk::Tool(name, json_str) => {
32                tool_calls.push((name, json_str));
33            }
34        }
35    }
36    println!();
37
38    // handle tool calls:
39    for (name, json_str) in tool_calls {
40        match name.as_ref() {
41            "weather" => {
42                let location: LocationData = serde_json::from_str(&json_str)?;
43                println!("{location:#?}");
44            }
45            _ => {}
46        }
47    }
48
49    Ok(())
50}