Skip to main content

RequestBuilder

Struct RequestBuilder 

Source
pub struct RequestBuilder { /* private fields */ }
Expand description

A way to add messages with finer control given.

This includes control over:

  • Logits processors
  • Constraints
  • Logprobs
  • Tools
  • Sampling
  • Enable thinking for models that support the configuration

Implementations§

Source§

impl RequestBuilder

Source

pub fn new() -> Self

Examples found in repository?
examples/gemma2/main.rs (line 15)
7async fn main() -> Result<()> {
8    let model = TextModelBuilder::new("google/gemma-2-9b-it")
9        .with_isq(IsqType::Q4K)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    let request = RequestBuilder::new().add_message(
16        TextMessageRole::User,
17        "Please write a mathematical equation where a few numbers are added.",
18    );
19
20    let response = model.send_chat_request(request).await?;
21
22    println!("{}", response.choices[0].message.content.as_ref().unwrap());
23
24    Ok(())
25}
More examples
Hide additional examples
examples/grammar/main.rs (line 16)
7async fn main() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q4K)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    // Bullet list regex
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::Regex(
18            "(- [^\n]*\n)+(- [^\n]*)(\n\n)?".to_string(),
19        ))
20        .add_message(TextMessageRole::User, "Please write a few jokes.");
21
22    let response = model.send_chat_request(request).await?;
23
24    println!("{}", response.choices[0].message.content.as_ref().unwrap());
25
26    Ok(())
27}
examples/custom_logits_processor/main.rs (line 35)
23async fn main() -> Result<()> {
24    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
25        .with_isq(IsqType::Q4K)
26        .with_logging()
27        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
28        .build()
29        .await?;
30
31    let mut rng = rand::rng();
32    let random_value: f64 = rng.random_range(0.0..=1.0);
33    let threshold: f64 = rng.random_range(0.0..=0.5);
34
35    let request = RequestBuilder::new()
36        .add_logits_processor(Arc::new(move |logits: &Tensor, _context: &[u32]| {
37            logits * random_value
38        }))
39        .add_logits_processor(Arc::new(ThresholdLogitsProcessor { threshold }))
40        .add_message(
41            TextMessageRole::User,
42            "Please write a mathematical equation where a few numbers are added.",
43        );
44
45    let response = model.send_chat_request(request).await?;
46
47    println!("{}", response.choices[0].message.content.as_ref().unwrap());
48
49    Ok(())
50}
examples/json_schema/main.rs (line 16)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
10        .with_isq(IsqType::Q4K)
11        .with_logging()
12        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
13        .build()
14        .await?;
15
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::JsonSchema(json!(
18            {
19                "type": "object",
20                "properties": {
21                    "street": {"type": "string"},
22                    "city": {"type": "string"},
23                    "state": {"type": "string", "pattern": "^[A-Z]{2}$"},
24                    "zip": {"type": "integer", "minimum": 10000, "maximum": 99999},
25                },
26                "required": ["street", "city", "state", "zip"],
27                "additionalProperties": false,
28            }
29        )))
30        .set_sampler_max_len(100)
31        .add_message(TextMessageRole::User, "A sample address please.");
32
33    let response = model.send_chat_request(request).await?;
34
35    println!("{}", response.choices[0].message.content.as_ref().unwrap());
36
37    Ok(())
38}
examples/llguidance/main.rs (line 32)
9async fn main() -> Result<()> {
10    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
11        .with_isq(IsqType::Q4K)
12        .with_logging()
13        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
14        .build()
15        .await?;
16
17    let top =
18        GrammarWithLexer::from_lark(r#"start: "Reasoning: " /.+/ "\nJSON: " @myobj"#.to_string());
19    let schema = GrammarWithLexer {
20        name: Some("myobj".to_string()),
21        json_schema: Some(json!({
22            "type": "object",
23            "properties": {
24                "answer": {"type": "string", "enum": ["Yes", "No"]},
25            },
26            "required": ["answer"],
27            "additionalProperties": false,
28        })),
29        ..Default::default()
30    };
31
32    let request = RequestBuilder::new()
33        .set_constraint(mistralrs::Constraint::Llguidance(LlguidanceGrammar {
34            grammars: vec![top, schema],
35            max_tokens: None,
36        }))
37        .set_sampler_max_len(100)
38        .add_message(
39            TextMessageRole::User,
40            "If all dogs are mammals, and all mammals are animals, are dogs animals?",
41        );
42
43    let response = model.send_chat_request(request).await?;
44
45    println!("{}", response.choices[0].message.content.as_ref().unwrap());
46
47    Ok(())
48}
examples/async/main.rs (line 34)
7async fn run() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q8_0)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    let messages = TextMessages::new()
16        .add_message(
17            TextMessageRole::System,
18            "You are an AI agent with a specialty in programming.",
19        )
20        .add_message(
21            TextMessageRole::User,
22            "Hello! How are you? Please write generic binary search function in Rust.",
23        );
24
25    let response = model.send_chat_request(messages).await?;
26
27    println!("{}", response.choices[0].message.content.as_ref().unwrap());
28    dbg!(
29        response.usage.avg_prompt_tok_per_sec,
30        response.usage.avg_compl_tok_per_sec
31    );
32
33    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
34    let request = RequestBuilder::new().return_logprobs(true).add_message(
35        TextMessageRole::User,
36        "Please write a mathematical equation where a few numbers are added.",
37    );
38
39    let response = model.send_chat_request(request).await?;
40
41    println!(
42        "Logprobs: {:?}",
43        &response.choices[0]
44            .logprobs
45            .as_ref()
46            .unwrap()
47            .content
48            .as_ref()
49            .unwrap()[0..3]
50    );
51    Ok(())
52}
Source

pub fn with_web_search_options( self, web_search_options: WebSearchOptions, ) -> Self

Examples found in repository?
examples/web_search/main.rs (line 21)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("NousResearch/Hermes-3-Llama-3.1-8B")
10        .with_isq(IsqType::Q4K)
11        .with_logging()
12        .with_search(SearchEmbeddingModel::default())
13        .build()
14        .await?;
15
16    let messages = TextMessages::new().add_message(
17        TextMessageRole::User,
18        "What is the weather forecast for Boston?",
19    );
20    let messages =
21        RequestBuilder::from(messages).with_web_search_options(WebSearchOptions::default());
22
23    let response = model.send_chat_request(messages).await?;
24
25    println!("What is the weather forecast for Boston?\n\n");
26    println!("{}", response.choices[0].message.content.as_ref().unwrap());
27    dbg!(
28        response.usage.avg_prompt_tok_per_sec,
29        response.usage.avg_compl_tok_per_sec
30    );
31
32    Ok(())
33}
More examples
Hide additional examples
examples/custom_search/main.rs (line 66)
42async fn main() -> Result<()> {
43    // Build the model enabling web-search support.  We supply a custom search callback that is
44    // used **instead** of the default remote web search when we set `web_search_options` later
45    // in the request.
46
47    // The EmbeddingGemma reranker is **not** required even when using a custom callback – it is
48    // used inside the retrieval pipeline to cluster / rank the results that our callback returns.
49    let model = TextModelBuilder::new("NousResearch/Hermes-3-Llama-3.1-8B")
50        .with_isq(IsqType::Q4K)
51        .with_logging()
52        .with_search_callback(Arc::new(|params: &mistralrs::SearchFunctionParameters| {
53            // In a real application there could be network or database calls here – but for the
54            // sake of demonstration we simply perform a local filesystem search.
55            local_search(&params.query)
56        }))
57        .build()
58        .await?;
59
60    let messages =
61        TextMessages::new().add_message(TextMessageRole::User, "Where is Cargo.toml in this repo?");
62
63    // Enable searching for this request.  Because we provided a custom search callback above, the
64    // model will call **our** function instead of performing an online web search.
65    let messages =
66        RequestBuilder::from(messages).with_web_search_options(WebSearchOptions::default());
67
68    let response = model.send_chat_request(messages).await?;
69
70    println!("{}", response.choices[0].message.content.as_ref().unwrap());
71
72    Ok(())
73}
Source

pub fn add_message(self, role: TextMessageRole, text: impl ToString) -> Self

Add a message to the request.

For messages with tool calls, use Self::add_message_with_tool_call. For messages with tool outputs, use Self::add_tool_message.

Examples found in repository?
examples/gemma2/main.rs (lines 15-18)
7async fn main() -> Result<()> {
8    let model = TextModelBuilder::new("google/gemma-2-9b-it")
9        .with_isq(IsqType::Q4K)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    let request = RequestBuilder::new().add_message(
16        TextMessageRole::User,
17        "Please write a mathematical equation where a few numbers are added.",
18    );
19
20    let response = model.send_chat_request(request).await?;
21
22    println!("{}", response.choices[0].message.content.as_ref().unwrap());
23
24    Ok(())
25}
More examples
Hide additional examples
examples/grammar/main.rs (line 20)
7async fn main() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q4K)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    // Bullet list regex
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::Regex(
18            "(- [^\n]*\n)+(- [^\n]*)(\n\n)?".to_string(),
19        ))
20        .add_message(TextMessageRole::User, "Please write a few jokes.");
21
22    let response = model.send_chat_request(request).await?;
23
24    println!("{}", response.choices[0].message.content.as_ref().unwrap());
25
26    Ok(())
27}
examples/custom_logits_processor/main.rs (lines 40-43)
23async fn main() -> Result<()> {
24    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
25        .with_isq(IsqType::Q4K)
26        .with_logging()
27        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
28        .build()
29        .await?;
30
31    let mut rng = rand::rng();
32    let random_value: f64 = rng.random_range(0.0..=1.0);
33    let threshold: f64 = rng.random_range(0.0..=0.5);
34
35    let request = RequestBuilder::new()
36        .add_logits_processor(Arc::new(move |logits: &Tensor, _context: &[u32]| {
37            logits * random_value
38        }))
39        .add_logits_processor(Arc::new(ThresholdLogitsProcessor { threshold }))
40        .add_message(
41            TextMessageRole::User,
42            "Please write a mathematical equation where a few numbers are added.",
43        );
44
45    let response = model.send_chat_request(request).await?;
46
47    println!("{}", response.choices[0].message.content.as_ref().unwrap());
48
49    Ok(())
50}
examples/json_schema/main.rs (line 31)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
10        .with_isq(IsqType::Q4K)
11        .with_logging()
12        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
13        .build()
14        .await?;
15
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::JsonSchema(json!(
18            {
19                "type": "object",
20                "properties": {
21                    "street": {"type": "string"},
22                    "city": {"type": "string"},
23                    "state": {"type": "string", "pattern": "^[A-Z]{2}$"},
24                    "zip": {"type": "integer", "minimum": 10000, "maximum": 99999},
25                },
26                "required": ["street", "city", "state", "zip"],
27                "additionalProperties": false,
28            }
29        )))
30        .set_sampler_max_len(100)
31        .add_message(TextMessageRole::User, "A sample address please.");
32
33    let response = model.send_chat_request(request).await?;
34
35    println!("{}", response.choices[0].message.content.as_ref().unwrap());
36
37    Ok(())
38}
examples/llguidance/main.rs (lines 38-41)
9async fn main() -> Result<()> {
10    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
11        .with_isq(IsqType::Q4K)
12        .with_logging()
13        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
14        .build()
15        .await?;
16
17    let top =
18        GrammarWithLexer::from_lark(r#"start: "Reasoning: " /.+/ "\nJSON: " @myobj"#.to_string());
19    let schema = GrammarWithLexer {
20        name: Some("myobj".to_string()),
21        json_schema: Some(json!({
22            "type": "object",
23            "properties": {
24                "answer": {"type": "string", "enum": ["Yes", "No"]},
25            },
26            "required": ["answer"],
27            "additionalProperties": false,
28        })),
29        ..Default::default()
30    };
31
32    let request = RequestBuilder::new()
33        .set_constraint(mistralrs::Constraint::Llguidance(LlguidanceGrammar {
34            grammars: vec![top, schema],
35            max_tokens: None,
36        }))
37        .set_sampler_max_len(100)
38        .add_message(
39            TextMessageRole::User,
40            "If all dogs are mammals, and all mammals are animals, are dogs animals?",
41        );
42
43    let response = model.send_chat_request(request).await?;
44
45    println!("{}", response.choices[0].message.content.as_ref().unwrap());
46
47    Ok(())
48}
examples/async/main.rs (lines 34-37)
7async fn run() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q8_0)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    let messages = TextMessages::new()
16        .add_message(
17            TextMessageRole::System,
18            "You are an AI agent with a specialty in programming.",
19        )
20        .add_message(
21            TextMessageRole::User,
22            "Hello! How are you? Please write generic binary search function in Rust.",
23        );
24
25    let response = model.send_chat_request(messages).await?;
26
27    println!("{}", response.choices[0].message.content.as_ref().unwrap());
28    dbg!(
29        response.usage.avg_prompt_tok_per_sec,
30        response.usage.avg_compl_tok_per_sec
31    );
32
33    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
34    let request = RequestBuilder::new().return_logprobs(true).add_message(
35        TextMessageRole::User,
36        "Please write a mathematical equation where a few numbers are added.",
37    );
38
39    let response = model.send_chat_request(request).await?;
40
41    println!(
42        "Logprobs: {:?}",
43        &response.choices[0]
44            .logprobs
45            .as_ref()
46            .unwrap()
47            .content
48            .as_ref()
49            .unwrap()[0..3]
50    );
51    Ok(())
52}
Source

pub fn add_tool_message( self, tool_content: impl ToString, tool_id: impl ToString, ) -> Self

Add a message with the output of a tool call.

Examples found in repository?
examples/tools/main.rs (line 74)
20async fn main() -> Result<()> {
21    let model = TextModelBuilder::new("meta-llama/Meta-Llama-3.1-8B-Instruct")
22        .with_logging()
23        .with_isq(IsqType::Q8_0)
24        .build()
25        .await?;
26
27    let parameters: HashMap<String, Value> = serde_json::from_value(json!({
28        "type": "object",
29        "properties": {
30            "place": {
31                "type": "string",
32                "description": "The place to get the weather for.",
33            },
34        },
35        "required": ["place"],
36    }))?;
37
38    let tools = vec![Tool {
39        tp: ToolType::Function,
40        function: Function {
41            description: Some("Get the weather for a certain city.".to_string()),
42            name: "get_weather".to_string(),
43            parameters: Some(parameters),
44        },
45    }];
46
47    // We will keep all the messages here
48    let mut messages = RequestBuilder::new()
49        .add_message(TextMessageRole::User, "What is the weather in Boston?")
50        .set_tools(tools)
51        .set_tool_choice(ToolChoice::Auto);
52
53    let response = model.send_chat_request(messages.clone()).await?;
54
55    let message = &response.choices[0].message;
56
57    if let Some(tool_calls) = &message.tool_calls {
58        let called = &tool_calls[0];
59        if called.function.name == "get_weather" {
60            let input: GetWeatherInput = serde_json::from_str(&called.function.arguments)?;
61            println!("Called tool `get_weather` with arguments {input:?}");
62
63            let result = get_weather(input);
64            println!("Output of tool call: {result}");
65
66            // Add tool call message from assistant so it knows what it called
67            // Then, add message from the tool
68            messages = messages
69                .add_message_with_tool_call(
70                    TextMessageRole::Assistant,
71                    String::new(),
72                    vec![called.clone()],
73                )
74                .add_tool_message(result, called.id.clone())
75                .set_tool_choice(ToolChoice::None);
76
77            let response = model.send_chat_request(messages.clone()).await?;
78
79            let message = &response.choices[0].message;
80            println!("Output of model: {:?}", message.content);
81        }
82    }
83
84    Ok(())
85}
Source

pub fn add_message_with_tool_call( self, role: TextMessageRole, text: impl ToString, tool_calls: Vec<ToolCallResponse>, ) -> Self

Examples found in repository?
examples/tools/main.rs (lines 69-73)
20async fn main() -> Result<()> {
21    let model = TextModelBuilder::new("meta-llama/Meta-Llama-3.1-8B-Instruct")
22        .with_logging()
23        .with_isq(IsqType::Q8_0)
24        .build()
25        .await?;
26
27    let parameters: HashMap<String, Value> = serde_json::from_value(json!({
28        "type": "object",
29        "properties": {
30            "place": {
31                "type": "string",
32                "description": "The place to get the weather for.",
33            },
34        },
35        "required": ["place"],
36    }))?;
37
38    let tools = vec![Tool {
39        tp: ToolType::Function,
40        function: Function {
41            description: Some("Get the weather for a certain city.".to_string()),
42            name: "get_weather".to_string(),
43            parameters: Some(parameters),
44        },
45    }];
46
47    // We will keep all the messages here
48    let mut messages = RequestBuilder::new()
49        .add_message(TextMessageRole::User, "What is the weather in Boston?")
50        .set_tools(tools)
51        .set_tool_choice(ToolChoice::Auto);
52
53    let response = model.send_chat_request(messages.clone()).await?;
54
55    let message = &response.choices[0].message;
56
57    if let Some(tool_calls) = &message.tool_calls {
58        let called = &tool_calls[0];
59        if called.function.name == "get_weather" {
60            let input: GetWeatherInput = serde_json::from_str(&called.function.arguments)?;
61            println!("Called tool `get_weather` with arguments {input:?}");
62
63            let result = get_weather(input);
64            println!("Output of tool call: {result}");
65
66            // Add tool call message from assistant so it knows what it called
67            // Then, add message from the tool
68            messages = messages
69                .add_message_with_tool_call(
70                    TextMessageRole::Assistant,
71                    String::new(),
72                    vec![called.clone()],
73                )
74                .add_tool_message(result, called.id.clone())
75                .set_tool_choice(ToolChoice::None);
76
77            let response = model.send_chat_request(messages.clone()).await?;
78
79            let message = &response.choices[0].message;
80            println!("Output of model: {:?}", message.content);
81        }
82    }
83
84    Ok(())
85}
Source

pub fn add_image_message( self, role: TextMessageRole, text: impl ToString, images: Vec<DynamicImage>, model: &Model, ) -> Result<Self>

Source

pub fn add_audio_message( self, role: TextMessageRole, text: impl ToString, audios: Vec<AudioInput>, model: &Model, ) -> Result<Self>

Source

pub fn add_multimodal_message( self, role: TextMessageRole, text: impl ToString, images: Vec<DynamicImage>, audios: Vec<AudioInput>, model: &Model, ) -> Result<Self>

By convention, all images are added before all audios.

Source

pub fn add_logits_processor( self, processor: Arc<dyn CustomLogitsProcessor>, ) -> Self

Examples found in repository?
examples/custom_logits_processor/main.rs (lines 36-38)
23async fn main() -> Result<()> {
24    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
25        .with_isq(IsqType::Q4K)
26        .with_logging()
27        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
28        .build()
29        .await?;
30
31    let mut rng = rand::rng();
32    let random_value: f64 = rng.random_range(0.0..=1.0);
33    let threshold: f64 = rng.random_range(0.0..=0.5);
34
35    let request = RequestBuilder::new()
36        .add_logits_processor(Arc::new(move |logits: &Tensor, _context: &[u32]| {
37            logits * random_value
38        }))
39        .add_logits_processor(Arc::new(ThresholdLogitsProcessor { threshold }))
40        .add_message(
41            TextMessageRole::User,
42            "Please write a mathematical equation where a few numbers are added.",
43        );
44
45    let response = model.send_chat_request(request).await?;
46
47    println!("{}", response.choices[0].message.content.as_ref().unwrap());
48
49    Ok(())
50}
Source

pub fn set_adapters(self, adapters: Vec<String>) -> Self

Source

pub fn set_tools(self, tools: Vec<Tool>) -> Self

The default tool choice is auto.

Examples found in repository?
examples/custom_tool_call/main.rs (line 68)
34async fn main() -> Result<()> {
35    // Build the model and register the *tool callback*.
36    let model = TextModelBuilder::new("NousResearch/Hermes-3-Llama-3.1-8B")
37        .with_isq(IsqType::Q4K)
38        .with_logging()
39        .with_tool_callback(
40            "local_search",
41            Arc::new(|f: &CalledFunction| {
42                let args: serde_json::Value = serde_json::from_str(&f.arguments)?;
43                let query = args["query"].as_str().unwrap_or("");
44                Ok(serde_json::to_string(&local_search(query)?)?)
45            }),
46        )
47        .build()
48        .await?;
49
50    // Define the JSON schema for the tool the model can call.
51    let parameters = std::collections::HashMap::from([(
52        "query".to_string(),
53        serde_json::json!({"type": "string", "description": "Query"}),
54    )]);
55    let tool = Tool {
56        tp: ToolType::Function,
57        function: mistralrs::Function {
58            description: Some("Local filesystem search".to_string()),
59            name: "local_search".to_string(),
60            parameters: Some(parameters),
61        },
62    };
63
64    // Ask the user question and allow the model to call the tool automatically.
65    let messages =
66        TextMessages::new().add_message(TextMessageRole::User, "Where is Cargo.toml in this repo?");
67    let messages = RequestBuilder::from(messages)
68        .set_tools(vec![tool])
69        .set_tool_choice(ToolChoice::Auto);
70
71    let response = model.send_chat_request(messages).await?;
72    println!("{}", response.choices[0].message.content.as_ref().unwrap());
73    Ok(())
74}
More examples
Hide additional examples
examples/tools/main.rs (line 50)
20async fn main() -> Result<()> {
21    let model = TextModelBuilder::new("meta-llama/Meta-Llama-3.1-8B-Instruct")
22        .with_logging()
23        .with_isq(IsqType::Q8_0)
24        .build()
25        .await?;
26
27    let parameters: HashMap<String, Value> = serde_json::from_value(json!({
28        "type": "object",
29        "properties": {
30            "place": {
31                "type": "string",
32                "description": "The place to get the weather for.",
33            },
34        },
35        "required": ["place"],
36    }))?;
37
38    let tools = vec![Tool {
39        tp: ToolType::Function,
40        function: Function {
41            description: Some("Get the weather for a certain city.".to_string()),
42            name: "get_weather".to_string(),
43            parameters: Some(parameters),
44        },
45    }];
46
47    // We will keep all the messages here
48    let mut messages = RequestBuilder::new()
49        .add_message(TextMessageRole::User, "What is the weather in Boston?")
50        .set_tools(tools)
51        .set_tool_choice(ToolChoice::Auto);
52
53    let response = model.send_chat_request(messages.clone()).await?;
54
55    let message = &response.choices[0].message;
56
57    if let Some(tool_calls) = &message.tool_calls {
58        let called = &tool_calls[0];
59        if called.function.name == "get_weather" {
60            let input: GetWeatherInput = serde_json::from_str(&called.function.arguments)?;
61            println!("Called tool `get_weather` with arguments {input:?}");
62
63            let result = get_weather(input);
64            println!("Output of tool call: {result}");
65
66            // Add tool call message from assistant so it knows what it called
67            // Then, add message from the tool
68            messages = messages
69                .add_message_with_tool_call(
70                    TextMessageRole::Assistant,
71                    String::new(),
72                    vec![called.clone()],
73                )
74                .add_tool_message(result, called.id.clone())
75                .set_tool_choice(ToolChoice::None);
76
77            let response = model.send_chat_request(messages.clone()).await?;
78
79            let message = &response.choices[0].message;
80            println!("Output of model: {:?}", message.content);
81        }
82    }
83
84    Ok(())
85}
Source

pub fn set_tool_choice(self, tool_choice: ToolChoice) -> Self

Examples found in repository?
examples/custom_tool_call/main.rs (line 69)
34async fn main() -> Result<()> {
35    // Build the model and register the *tool callback*.
36    let model = TextModelBuilder::new("NousResearch/Hermes-3-Llama-3.1-8B")
37        .with_isq(IsqType::Q4K)
38        .with_logging()
39        .with_tool_callback(
40            "local_search",
41            Arc::new(|f: &CalledFunction| {
42                let args: serde_json::Value = serde_json::from_str(&f.arguments)?;
43                let query = args["query"].as_str().unwrap_or("");
44                Ok(serde_json::to_string(&local_search(query)?)?)
45            }),
46        )
47        .build()
48        .await?;
49
50    // Define the JSON schema for the tool the model can call.
51    let parameters = std::collections::HashMap::from([(
52        "query".to_string(),
53        serde_json::json!({"type": "string", "description": "Query"}),
54    )]);
55    let tool = Tool {
56        tp: ToolType::Function,
57        function: mistralrs::Function {
58            description: Some("Local filesystem search".to_string()),
59            name: "local_search".to_string(),
60            parameters: Some(parameters),
61        },
62    };
63
64    // Ask the user question and allow the model to call the tool automatically.
65    let messages =
66        TextMessages::new().add_message(TextMessageRole::User, "Where is Cargo.toml in this repo?");
67    let messages = RequestBuilder::from(messages)
68        .set_tools(vec![tool])
69        .set_tool_choice(ToolChoice::Auto);
70
71    let response = model.send_chat_request(messages).await?;
72    println!("{}", response.choices[0].message.content.as_ref().unwrap());
73    Ok(())
74}
More examples
Hide additional examples
examples/tools/main.rs (line 51)
20async fn main() -> Result<()> {
21    let model = TextModelBuilder::new("meta-llama/Meta-Llama-3.1-8B-Instruct")
22        .with_logging()
23        .with_isq(IsqType::Q8_0)
24        .build()
25        .await?;
26
27    let parameters: HashMap<String, Value> = serde_json::from_value(json!({
28        "type": "object",
29        "properties": {
30            "place": {
31                "type": "string",
32                "description": "The place to get the weather for.",
33            },
34        },
35        "required": ["place"],
36    }))?;
37
38    let tools = vec![Tool {
39        tp: ToolType::Function,
40        function: Function {
41            description: Some("Get the weather for a certain city.".to_string()),
42            name: "get_weather".to_string(),
43            parameters: Some(parameters),
44        },
45    }];
46
47    // We will keep all the messages here
48    let mut messages = RequestBuilder::new()
49        .add_message(TextMessageRole::User, "What is the weather in Boston?")
50        .set_tools(tools)
51        .set_tool_choice(ToolChoice::Auto);
52
53    let response = model.send_chat_request(messages.clone()).await?;
54
55    let message = &response.choices[0].message;
56
57    if let Some(tool_calls) = &message.tool_calls {
58        let called = &tool_calls[0];
59        if called.function.name == "get_weather" {
60            let input: GetWeatherInput = serde_json::from_str(&called.function.arguments)?;
61            println!("Called tool `get_weather` with arguments {input:?}");
62
63            let result = get_weather(input);
64            println!("Output of tool call: {result}");
65
66            // Add tool call message from assistant so it knows what it called
67            // Then, add message from the tool
68            messages = messages
69                .add_message_with_tool_call(
70                    TextMessageRole::Assistant,
71                    String::new(),
72                    vec![called.clone()],
73                )
74                .add_tool_message(result, called.id.clone())
75                .set_tool_choice(ToolChoice::None);
76
77            let response = model.send_chat_request(messages.clone()).await?;
78
79            let message = &response.choices[0].message;
80            println!("Output of model: {:?}", message.content);
81        }
82    }
83
84    Ok(())
85}
Source

pub fn return_logprobs(self, return_logprobs: bool) -> Self

Examples found in repository?
examples/async/main.rs (line 34)
7async fn run() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q8_0)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    let messages = TextMessages::new()
16        .add_message(
17            TextMessageRole::System,
18            "You are an AI agent with a specialty in programming.",
19        )
20        .add_message(
21            TextMessageRole::User,
22            "Hello! How are you? Please write generic binary search function in Rust.",
23        );
24
25    let response = model.send_chat_request(messages).await?;
26
27    println!("{}", response.choices[0].message.content.as_ref().unwrap());
28    dbg!(
29        response.usage.avg_prompt_tok_per_sec,
30        response.usage.avg_compl_tok_per_sec
31    );
32
33    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
34    let request = RequestBuilder::new().return_logprobs(true).add_message(
35        TextMessageRole::User,
36        "Please write a mathematical equation where a few numbers are added.",
37    );
38
39    let response = model.send_chat_request(request).await?;
40
41    println!(
42        "Logprobs: {:?}",
43        &response.choices[0]
44            .logprobs
45            .as_ref()
46            .unwrap()
47            .content
48            .as_ref()
49            .unwrap()[0..3]
50    );
51    Ok(())
52}
More examples
Hide additional examples
examples/simple/main.rs (line 35)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
10        .with_isq(IsqType::Q8_0)
11        .with_logging()
12        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
13        .build()
14        .await?;
15
16    let messages = TextMessages::new()
17        .add_message(
18            TextMessageRole::System,
19            "You are an AI agent with a specialty in programming.",
20        )
21        .add_message(
22            TextMessageRole::User,
23            "Hello! How are you? Please write generic binary search function in Rust.",
24        );
25
26    let response = model.send_chat_request(messages).await?;
27
28    println!("{}", response.choices[0].message.content.as_ref().unwrap());
29    dbg!(
30        response.usage.avg_prompt_tok_per_sec,
31        response.usage.avg_compl_tok_per_sec
32    );
33
34    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
35    let request = RequestBuilder::new().return_logprobs(true).add_message(
36        TextMessageRole::User,
37        "Please write a mathematical equation where a few numbers are added.",
38    );
39
40    let response = model.send_chat_request(request).await?;
41
42    println!(
43        "Logprobs: {:?}",
44        &response.choices[0]
45            .logprobs
46            .as_ref()
47            .unwrap()
48            .content
49            .as_ref()
50            .unwrap()[0..3]
51    );
52
53    Ok(())
54}
examples/uqff/main.rs (line 37)
7async fn main() -> Result<()> {
8    let model = UqffTextModelBuilder::new(
9        "EricB/Phi-3.5-mini-instruct-UQFF",
10        vec!["phi3.5-mini-instruct-q8_0.uqff".into()],
11    )
12    .into_inner()
13    .with_logging()
14    .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
15    .build()
16    .await?;
17
18    let messages = TextMessages::new()
19        .add_message(
20            TextMessageRole::System,
21            "You are an AI agent with a specialty in programming.",
22        )
23        .add_message(
24            TextMessageRole::User,
25            "Hello! How are you? Please write generic binary search function in Rust.",
26        );
27
28    let response = model.send_chat_request(messages).await?;
29
30    println!("{}", response.choices[0].message.content.as_ref().unwrap());
31    dbg!(
32        response.usage.avg_prompt_tok_per_sec,
33        response.usage.avg_compl_tok_per_sec
34    );
35
36    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
37    let request = RequestBuilder::new().return_logprobs(true).add_message(
38        TextMessageRole::User,
39        "Please write a mathematical equation where a few numbers are added.",
40    );
41
42    let response = model.send_chat_request(request).await?;
43
44    println!(
45        "Logprobs: {:?}",
46        &response.choices[0]
47            .logprobs
48            .as_ref()
49            .unwrap()
50            .content
51            .as_ref()
52            .unwrap()[0..3]
53    );
54
55    Ok(())
56}
examples/gguf_locally/main.rs (line 35)
7async fn main() -> Result<()> {
8    // We do not use any files from remote servers here, and instead load the
9    // chat template from the specified file, and the tokenizer and model from a
10    // local GGUF file at the path specified.
11    let model = GgufModelBuilder::new(
12        "gguf_models/mistral_v0.1/",
13        vec!["mistral-7b-instruct-v0.1.Q4_K_M.gguf"],
14    )
15    .with_chat_template("chat_templates/mistral.json")
16    .with_logging()
17    .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
18    .build()
19    .await?;
20
21    let messages = TextMessages::new().add_message(
22        TextMessageRole::User,
23        "Hello! How are you? Please write generic binary search function in Rust.",
24    );
25
26    let response = model.send_chat_request(messages).await?;
27
28    println!("{}", response.choices[0].message.content.as_ref().unwrap());
29    dbg!(
30        response.usage.avg_prompt_tok_per_sec,
31        response.usage.avg_compl_tok_per_sec
32    );
33
34    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
35    let request = RequestBuilder::new().return_logprobs(true).add_message(
36        TextMessageRole::User,
37        "Please write a mathematical equation where a few numbers are added.",
38    );
39
40    let response = model.send_chat_request(request).await?;
41
42    println!(
43        "Logprobs: {:?}",
44        &response.choices[0]
45            .logprobs
46            .as_ref()
47            .unwrap()
48            .content
49            .as_ref()
50            .unwrap()[0..3]
51    );
52
53    Ok(())
54}
examples/speculative/main.rs (line 37)
8async fn main() -> Result<()> {
9    let target = TextModelBuilder::new("meta-llama/Llama-3.1-8B-Instruct").with_logging();
10    let draft = TextModelBuilder::new("meta-llama/Llama-3.2-1B-Instruct")
11        .with_logging()
12        .with_isq(IsqType::Q8_0);
13    let spec_cfg = SpeculativeConfig { gamma: 16 };
14    let model = TextSpeculativeBuilder::new(target, draft, spec_cfg)?
15        .build()
16        .await?;
17
18    let messages = TextMessages::new()
19        .add_message(
20            TextMessageRole::System,
21            "You are an AI agent with a specialty in programming.",
22        )
23        .add_message(
24            TextMessageRole::User,
25            "Hello! How are you? Please write generic binary search function in Rust.",
26        );
27
28    let response = model.send_chat_request(messages).await?;
29
30    println!("{}", response.choices[0].message.content.as_ref().unwrap());
31    dbg!(
32        response.usage.avg_prompt_tok_per_sec,
33        response.usage.avg_compl_tok_per_sec
34    );
35
36    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
37    let request = RequestBuilder::new().return_logprobs(true).add_message(
38        TextMessageRole::User,
39        "Please write a mathematical equation where a few numbers are added.",
40    );
41
42    let response = model.send_chat_request(request).await?;
43
44    println!(
45        "Logprobs: {:?}",
46        &response.choices[0]
47            .logprobs
48            .as_ref()
49            .unwrap()
50            .content
51            .as_ref()
52            .unwrap()[0..3]
53    );
54
55    Ok(())
56}
examples/text_auto_device_map/main.rs (line 40)
8async fn main() -> Result<()> {
9    let auto_map_params = AutoDeviceMapParams::Text {
10        max_seq_len: 4096,
11        max_batch_size: 2,
12    };
13    let model = TextModelBuilder::new("meta-llama/Llama-3.3-70B-Instruct")
14        .with_isq(IsqType::Q8_0)
15        .with_logging()
16        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
17        .with_device_mapping(DeviceMapSetting::Auto(auto_map_params))
18        .build()
19        .await?;
20
21    let messages = TextMessages::new()
22        .add_message(
23            TextMessageRole::System,
24            "You are an AI agent with a specialty in programming.",
25        )
26        .add_message(
27            TextMessageRole::User,
28            "Hello! How are you? Please write generic binary search function in Rust.",
29        );
30
31    let response = model.send_chat_request(messages).await?;
32
33    println!("{}", response.choices[0].message.content.as_ref().unwrap());
34    dbg!(
35        response.usage.avg_prompt_tok_per_sec,
36        response.usage.avg_compl_tok_per_sec
37    );
38
39    // Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability.
40    let request = RequestBuilder::new().return_logprobs(true).add_message(
41        TextMessageRole::User,
42        "Please write a mathematical equation where a few numbers are added.",
43    );
44
45    let response = model.send_chat_request(request).await?;
46
47    println!(
48        "Logprobs: {:?}",
49        &response.choices[0]
50            .logprobs
51            .as_ref()
52            .unwrap()
53            .content
54            .as_ref()
55            .unwrap()[0..3]
56    );
57
58    Ok(())
59}
Source

pub fn set_constraint(self, constraint: Constraint) -> Self

Examples found in repository?
examples/grammar/main.rs (lines 17-19)
7async fn main() -> Result<()> {
8    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
9        .with_isq(IsqType::Q4K)
10        .with_logging()
11        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
12        .build()
13        .await?;
14
15    // Bullet list regex
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::Regex(
18            "(- [^\n]*\n)+(- [^\n]*)(\n\n)?".to_string(),
19        ))
20        .add_message(TextMessageRole::User, "Please write a few jokes.");
21
22    let response = model.send_chat_request(request).await?;
23
24    println!("{}", response.choices[0].message.content.as_ref().unwrap());
25
26    Ok(())
27}
More examples
Hide additional examples
examples/json_schema/main.rs (lines 17-29)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
10        .with_isq(IsqType::Q4K)
11        .with_logging()
12        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
13        .build()
14        .await?;
15
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::JsonSchema(json!(
18            {
19                "type": "object",
20                "properties": {
21                    "street": {"type": "string"},
22                    "city": {"type": "string"},
23                    "state": {"type": "string", "pattern": "^[A-Z]{2}$"},
24                    "zip": {"type": "integer", "minimum": 10000, "maximum": 99999},
25                },
26                "required": ["street", "city", "state", "zip"],
27                "additionalProperties": false,
28            }
29        )))
30        .set_sampler_max_len(100)
31        .add_message(TextMessageRole::User, "A sample address please.");
32
33    let response = model.send_chat_request(request).await?;
34
35    println!("{}", response.choices[0].message.content.as_ref().unwrap());
36
37    Ok(())
38}
examples/llguidance/main.rs (lines 33-36)
9async fn main() -> Result<()> {
10    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
11        .with_isq(IsqType::Q4K)
12        .with_logging()
13        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
14        .build()
15        .await?;
16
17    let top =
18        GrammarWithLexer::from_lark(r#"start: "Reasoning: " /.+/ "\nJSON: " @myobj"#.to_string());
19    let schema = GrammarWithLexer {
20        name: Some("myobj".to_string()),
21        json_schema: Some(json!({
22            "type": "object",
23            "properties": {
24                "answer": {"type": "string", "enum": ["Yes", "No"]},
25            },
26            "required": ["answer"],
27            "additionalProperties": false,
28        })),
29        ..Default::default()
30    };
31
32    let request = RequestBuilder::new()
33        .set_constraint(mistralrs::Constraint::Llguidance(LlguidanceGrammar {
34            grammars: vec![top, schema],
35            max_tokens: None,
36        }))
37        .set_sampler_max_len(100)
38        .add_message(
39            TextMessageRole::User,
40            "If all dogs are mammals, and all mammals are animals, are dogs animals?",
41        );
42
43    let response = model.send_chat_request(request).await?;
44
45    println!("{}", response.choices[0].message.content.as_ref().unwrap());
46
47    Ok(())
48}
Source

pub fn set_sampling(self, params: SamplingParams) -> Self

Set the sampling parameters as given.

Source

pub fn set_deterministic_sampler(self) -> Self

Set the sampling parameters for deterministic generation. This sets up the parameters so that there is:

  • No temperature, topk, topp, minp
  • No penalties, stop tokens, or logit bias
  • No maximum length
Source

pub fn set_sampler_temperature(self, temperature: f64) -> Self

Source

pub fn set_sampler_topk(self, topk: usize) -> Self

Source

pub fn set_sampler_topp(self, topp: f64) -> Self

Source

pub fn set_sampler_minp(self, minp: f64) -> Self

Source

pub fn set_sampler_topn_logprobs(self, top_n_logprobs: usize) -> Self

Source

pub fn set_sampler_frequency_penalty(self, frequency_penalty: f32) -> Self

Source

pub fn set_sampler_presence_penalty(self, presence_penalty: f32) -> Self

Source

pub fn set_sampler_stop_toks(self, stop_toks: StopTokens) -> Self

Source

pub fn set_sampler_max_len(self, max_len: usize) -> Self

Examples found in repository?
examples/json_schema/main.rs (line 30)
8async fn main() -> Result<()> {
9    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
10        .with_isq(IsqType::Q4K)
11        .with_logging()
12        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
13        .build()
14        .await?;
15
16    let request = RequestBuilder::new()
17        .set_constraint(mistralrs::Constraint::JsonSchema(json!(
18            {
19                "type": "object",
20                "properties": {
21                    "street": {"type": "string"},
22                    "city": {"type": "string"},
23                    "state": {"type": "string", "pattern": "^[A-Z]{2}$"},
24                    "zip": {"type": "integer", "minimum": 10000, "maximum": 99999},
25                },
26                "required": ["street", "city", "state", "zip"],
27                "additionalProperties": false,
28            }
29        )))
30        .set_sampler_max_len(100)
31        .add_message(TextMessageRole::User, "A sample address please.");
32
33    let response = model.send_chat_request(request).await?;
34
35    println!("{}", response.choices[0].message.content.as_ref().unwrap());
36
37    Ok(())
38}
More examples
Hide additional examples
examples/llguidance/main.rs (line 37)
9async fn main() -> Result<()> {
10    let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct")
11        .with_isq(IsqType::Q4K)
12        .with_logging()
13        .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
14        .build()
15        .await?;
16
17    let top =
18        GrammarWithLexer::from_lark(r#"start: "Reasoning: " /.+/ "\nJSON: " @myobj"#.to_string());
19    let schema = GrammarWithLexer {
20        name: Some("myobj".to_string()),
21        json_schema: Some(json!({
22            "type": "object",
23            "properties": {
24                "answer": {"type": "string", "enum": ["Yes", "No"]},
25            },
26            "required": ["answer"],
27            "additionalProperties": false,
28        })),
29        ..Default::default()
30    };
31
32    let request = RequestBuilder::new()
33        .set_constraint(mistralrs::Constraint::Llguidance(LlguidanceGrammar {
34            grammars: vec![top, schema],
35            max_tokens: None,
36        }))
37        .set_sampler_max_len(100)
38        .add_message(
39            TextMessageRole::User,
40            "If all dogs are mammals, and all mammals are animals, are dogs animals?",
41        );
42
43    let response = model.send_chat_request(request).await?;
44
45    println!("{}", response.choices[0].message.content.as_ref().unwrap());
46
47    Ok(())
48}
examples/llama_vision_multiturn/main.rs (line 17)
7async fn main() -> Result<()> {
8    let model = VisionModelBuilder::new(MODEL_ID)
9        .with_logging()
10        .with_isq(mistralrs::IsqType::Q8_0)
11        .build()
12        .await?;
13
14    let mut messages = VisionMessages::new().add_message(TextMessageRole::User, "Hello!");
15
16    let resp = model
17        .send_chat_request(RequestBuilder::from(messages.clone()).set_sampler_max_len(100))
18        .await?
19        .choices[0]
20        .message
21        .content
22        .clone()
23        .unwrap();
24    println!("\n\n{resp}");
25    messages = messages.add_message(TextMessageRole::Assistant, resp);
26
27    let bytes = match reqwest::blocking::get(
28        // "https://s3.amazonaws.com/cdn.tulips.com/images/large/Timeless-Tulip.jpg",
29        "https://niche-museums.imgix.net/pioneer-history.jpeg",
30    ) {
31        Ok(http_resp) => http_resp.bytes()?.to_vec(),
32        Err(e) => anyhow::bail!(e),
33    };
34    let image = image::load_from_memory(&bytes)?;
35
36    messages = messages.add_image_message(
37        TextMessageRole::User,
38        "What is depicted here? Please describe the scene in detail.",
39        vec![image],
40        &model,
41    )?;
42    let resp = model
43        .send_chat_request(RequestBuilder::from(messages.clone()).set_sampler_max_len(100))
44        .await?
45        .choices[0]
46        .message
47        .content
48        .clone()
49        .unwrap();
50    println!("\n\n{resp}");
51    messages = messages.add_message(TextMessageRole::Assistant, resp);
52
53    let bytes = match reqwest::blocking::get(
54            "https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg"
55        ) {
56            Ok(http_resp) => http_resp.bytes()?.to_vec(),
57            Err(e) => anyhow::bail!(e),
58        };
59    let image = image::load_from_memory(&bytes)?;
60
61    messages =
62        messages.add_image_message(TextMessageRole::User, "What is this?", vec![image], &model)?;
63    let resp = model
64        .send_chat_request(RequestBuilder::from(messages.clone()).set_sampler_max_len(100))
65        .await?
66        .choices[0]
67        .message
68        .content
69        .clone()
70        .unwrap();
71    println!("\n\n{resp}");
72    messages = messages.add_message(TextMessageRole::Assistant, resp);
73
74    let bytes =
75        match reqwest::blocking::get("https://cdn.britannica.com/79/4679-050-BC127236/Titanic.jpg")
76        {
77            Ok(http_resp) => http_resp.bytes()?.to_vec(),
78            Err(e) => anyhow::bail!(e),
79        };
80    let image = image::load_from_memory(&bytes)?;
81
82    messages =
83        messages.add_image_message(TextMessageRole::User, "What is this?", vec![image], &model)?;
84    let resp = model
85        .send_chat_request(RequestBuilder::from(messages.clone()).set_sampler_max_len(100))
86        .await?
87        .choices[0]
88        .message
89        .content
90        .clone()
91        .unwrap();
92    println!("\n\nModel response*: {resp}");
93    messages = messages.add_message(TextMessageRole::Assistant, resp);
94
95    println!("Final chat history: {messages:?}");
96
97    Ok(())
98}
Source

pub fn set_sampler_logits_bias(self, logits_bias: HashMap<u32, f32>) -> Self

Source

pub fn set_sampler_n_choices(self, n_choices: usize) -> Self

Source

pub fn set_sampler_dry_params(self, dry_params: DrySamplingParams) -> Self

Source

pub fn enable_thinking(self, enable_thinking: bool) -> Self

Source

pub fn with_truncate_sequence(self, truncate_sequence: bool) -> Self

Truncate prompts that exceed the model’s maximum context length.

Trait Implementations§

Source§

impl Clone for RequestBuilder

Source§

fn clone(&self) -> RequestBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for RequestBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<TextMessages> for RequestBuilder

Source§

fn from(value: TextMessages) -> Self

Converts to this type from the input type.
Source§

impl From<VisionMessages> for RequestBuilder

Source§

fn from(value: VisionMessages) -> Self

Converts to this type from the input type.
Source§

impl RequestLike for RequestBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

Gets the type name of self
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: AsAny + ?Sized,

Source§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
Source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryClone for T
where T: Clone,

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,