2024-03-09 11:28:50 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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 {
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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 {
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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,
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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 {
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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,
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +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()")
|
|
|
|
|
}
|
|
|
|
|
}
|