2024-03-09 11:28:50 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
use std::{any::Any, fmt::Debug};
|
2024-03-09 11:28:50 +01:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Definitions
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
pub struct ToolCall {
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub id: Option<String>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub r#type: Option<String>,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub function: ToolCallFunction,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
pub struct ToolCallFunction {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub arguments: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct Tool {
|
|
|
|
|
pub r#type: ToolType,
|
|
|
|
|
pub function: ToolFunction,
|
|
|
|
|
}
|
|
|
|
|
impl Tool {
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
/// Create a tool with a JSON Schema parameters object.
|
2024-03-09 11:28:50 +01:00
|
|
|
pub fn new(
|
|
|
|
|
function_name: String,
|
|
|
|
|
function_description: String,
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
parameters: serde_json::Value,
|
2024-03-09 11:28:50 +01:00
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
r#type: ToolType::Function,
|
|
|
|
|
function: ToolFunction {
|
|
|
|
|
name: function_name,
|
|
|
|
|
description: function_description,
|
|
|
|
|
parameters,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Request
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct ToolFunction {
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
pub name: String,
|
|
|
|
|
pub description: String,
|
|
|
|
|
pub parameters: serde_json::Value,
|
2024-03-09 11:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
pub enum ToolType {
|
|
|
|
|
#[serde(rename = "function")]
|
|
|
|
|
Function,
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 15:53:25 +02:00
|
|
|
/// An enum representing how functions should be called.
|
2024-03-09 11:28:50 +01:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
pub enum ToolChoice {
|
2024-06-07 15:53:25 +02:00
|
|
|
/// The model is forced to call a function.
|
2024-03-09 11:28:50 +01:00
|
|
|
#[serde(rename = "any")]
|
|
|
|
|
Any,
|
2024-06-07 15:53:25 +02:00
|
|
|
/// The model can choose to either generate a message or call a function.
|
2024-03-09 11:28:50 +01:00
|
|
|
#[serde(rename = "auto")]
|
|
|
|
|
Auto,
|
2024-06-07 15:53:25 +02:00
|
|
|
/// The model won't call a function and will generate a message instead.
|
2024-03-09 11:28:50 +01:00
|
|
|
#[serde(rename = "none")]
|
|
|
|
|
None,
|
refactor!: modernize core types and client for latest Mistral API
BREAKING CHANGE: Model is now a string-based struct with constructor
methods instead of a closed enum. EmbedModel is removed — use
Model::mistral_embed() instead. Tool parameters now accept
serde_json::Value (JSON Schema) instead of limited enum types.
- Replace Model enum with flexible Model(String) supporting all
current models: Large 3, Small 4, Medium 3.1, Magistral, Codestral,
Devstral, Pixtral, Voxtral, Ministral, and arbitrary strings
- Remove EmbedModel enum (consolidated into Model)
- Chat: add frequency_penalty, presence_penalty, stop, n, min_tokens,
parallel_tool_calls, reasoning_effort, json_schema response format
- Embeddings: add output_dimension and output_dtype fields
- Tools: accept raw JSON Schema, add tool call IDs and Required choice
- Stream delta content is now Option<String> for tool call chunks
- Add Length, ModelLength, Error finish reason variants
- DRY HTTP transport with shared response handlers
- Add DELETE method support and model get/delete endpoints
- Make model_list fields more lenient with Option/default for API compat
2026-03-20 17:54:29 +00:00
|
|
|
/// The model must call at least one tool.
|
|
|
|
|
#[serde(rename = "required")]
|
|
|
|
|
Required,
|
2024-03-09 11:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Custom
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
2024-07-24 19:16:11 +01:00
|
|
|
pub trait Function: Send {
|
2024-03-09 11:28:50 +01:00
|
|
|
async fn execute(&self, arguments: String) -> Box<dyn Any + Send>;
|
|
|
|
|
}
|
2024-07-24 19:08:25 +01:00
|
|
|
|
|
|
|
|
impl Debug for dyn Function {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "Function()")
|
|
|
|
|
}
|
|
|
|
|
}
|