gglib_core/ports/tool_support.rs
1//! Tool support detection port.
2//!
3//! This port defines the interface for detecting whether models support
4//! tool/function calling capabilities based on their metadata.
5
6/// The source/provider of the model being analyzed.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum ModelSource {
9 /// Local GGUF file.
10 LocalGguf,
11 /// `HuggingFace` model.
12 HuggingFace,
13}
14
15/// Input for tool support detection.
16///
17/// Contains metadata from various sources (chat templates, tags, model names)
18/// that can be analyzed to determine tool calling support.
19#[derive(Debug, Clone)]
20pub struct ToolSupportDetectionInput<'a> {
21 /// Model identifier (file path or HF model ID).
22 pub model_id: &'a str,
23 /// Chat template text (e.g., from `tokenizer.chat_template`).
24 pub chat_template: Option<&'a str>,
25 /// Model tags (e.g., from `HuggingFace`).
26 pub tags: &'a [String],
27 /// Source of the model.
28 pub source: ModelSource,
29}
30
31/// Tool calling format detected.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum ToolFormat {
34 /// Hermes-style tool calling.
35 Hermes,
36 /// Llama 3.x style.
37 Llama3,
38 /// Mistral style.
39 Mistral,
40 /// `OpenAI` tools format.
41 OpenAiTools,
42 /// Qwen XML-in-text dialect (`<tool_call>{json}</tool_call>` markup
43 /// embedded in the text channel rather than the `OpenAI` `tool_calls` array).
44 QwenXml,
45 /// Generic/unknown format.
46 Generic,
47}
48
49impl ToolFormat {
50 /// Convert to string representation.
51 #[must_use]
52 pub const fn as_str(&self) -> &'static str {
53 match self {
54 Self::Hermes => "hermes",
55 Self::Llama3 => "llama3",
56 Self::Mistral => "mistral",
57 Self::OpenAiTools => "openai-tools",
58 Self::QwenXml => "qwen-xml",
59 Self::Generic => "generic",
60 }
61 }
62}
63
64impl std::fmt::Display for ToolFormat {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 f.write_str(self.as_str())
67 }
68}
69
70/// Result of tool support detection.
71#[derive(Debug, Clone)]
72pub struct ToolSupportDetection {
73 /// Whether the model likely supports tool/function calling.
74 pub supports_tool_calling: bool,
75 /// Confidence level (0.0 = unknown/no support, 1.0 = certain support).
76 pub confidence: f32,
77 /// Detected tool calling format, if identified.
78 pub detected_format: Option<ToolFormat>,
79}
80
81/// Port for detecting tool/function calling support in models.
82///
83/// Implementations analyze model metadata (chat templates, tags, names)
84/// to determine if a model supports tool calling capabilities.
85pub trait ToolSupportDetectorPort: Send + Sync {
86 /// Detect tool support based on model metadata.
87 fn detect(&self, input: ToolSupportDetectionInput<'_>) -> ToolSupportDetection;
88}