use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(super) struct OllamaMessage {
pub(super) role: String,
#[serde(default)]
pub(super) content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) thinking: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) images: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) tool_calls: Option<Vec<OllamaToolCall>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) tool_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) tool_call_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub(super) struct OllamaToolCall {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) id: Option<String>,
pub(super) function: OllamaToolCallFunction,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub(super) struct OllamaToolCallFunction {
#[serde(default)]
pub(super) index: usize,
pub(super) name: String,
pub(super) arguments: serde_json::Value,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaChatRequest {
pub(super) model: String,
#[serde(default)]
pub(super) messages: Vec<OllamaMessage>,
#[serde(default = "bool_true")]
pub(super) stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) options: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) think: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) tools: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) format: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) keep_alive: Option<serde_json::Value>,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaGenerateRequest {
pub(super) model: String,
#[serde(default)]
pub(super) prompt: String,
#[serde(default = "bool_true")]
pub(super) stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) options: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) keep_alive: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) think: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) format: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) system: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) images: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) suffix: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) template: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub(super) raw: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) context: Option<serde_json::Value>,
}
pub(super) fn format_to_response_format(
format: &Option<serde_json::Value>,
) -> Option<serde_json::Value> {
match format {
Some(serde_json::Value::String(s)) if s == "json" => {
Some(serde_json::json!({ "type": "json_object" }))
}
Some(schema @ serde_json::Value::Object(_)) => Some(serde_json::json!({
"type": "json_schema",
"json_schema": { "name": "response", "schema": schema, "strict": true }
})),
_ => None,
}
}
pub(super) fn think_to_chat_template_kwargs(
think: &Option<serde_json::Value>,
) -> Option<serde_json::Value> {
match think {
Some(serde_json::Value::Bool(b)) => Some(serde_json::json!({ "enable_thinking": b })),
Some(serde_json::Value::String(level))
if crate::chat_template::EFFORT_LEVELS.contains(&level.trim()) =>
{
Some(serde_json::json!({
"enable_thinking": true,
"reasoning_effort": level.trim(),
}))
}
_ => None,
}
}
pub(super) fn resolve_think(
think: &Option<serde_json::Value>,
thinking: Option<&crate::chat_template::Thinking>,
) -> Option<serde_json::Value> {
match (think, thinking) {
(Some(serde_json::Value::String(level)), Some(thinking))
if !thinking.values.contains(&serde_json::json!(level.trim())) =>
{
Some(thinking.default.clone())
}
_ => think.clone(),
}
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaChatChunk {
pub(super) model: String,
pub(super) created_at: String,
pub(super) message: OllamaMessage,
pub(super) done: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) done_reason: Option<String>,
#[serde(flatten)]
pub(super) metrics: OllamaMetrics,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaGenerateChunk {
pub(super) model: String,
pub(super) created_at: String,
pub(super) response: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) thinking: Option<String>,
pub(super) done: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) done_reason: Option<String>,
#[serde(flatten)]
pub(super) metrics: OllamaMetrics,
}
#[derive(Debug, Serialize, Default, Clone, PartialEq)]
pub(super) struct OllamaMetrics {
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) total_duration: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) load_duration: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) prompt_eval_count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) prompt_eval_cached_count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) prompt_eval_cache_write_count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) prompt_eval_duration: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) eval_count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) eval_duration: Option<u64>,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaTagsResponse {
pub(super) models: Vec<OllamaModelInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaModelInfo {
pub(super) name: String,
pub(super) model: String,
pub(super) size: u64,
pub(super) digest: String,
pub(super) modified_at: String,
pub(super) details: OllamaModelDetails,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaModelDetails {
#[serde(default)]
pub(super) parent_model: String,
pub(super) format: String,
pub(super) family: String,
#[serde(default)]
pub(super) families: Vec<String>,
pub(super) parameter_size: String,
pub(super) quantization_level: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaPsResponse {
pub(super) models: Vec<OllamaRunningModelInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct OllamaRunningModelInfo {
pub(super) name: String,
pub(super) model: String,
pub(super) expires_at: Option<String>,
pub(super) digest: String,
pub(super) size: u64,
pub(super) size_vram: u64,
pub(super) pid: Option<u32>,
pub(super) port: u16,
pub(super) processor: String,
pub(super) context_length: Option<u64>,
pub(super) started_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) node: Option<String>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaShowRequest {
pub(super) model: String,
pub(super) name: Option<String>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaShowResponse {
pub(super) model_info: serde_json::Value,
pub(super) details: OllamaModelDetails,
pub(super) capabilities: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) template: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) thinking: Option<crate::chat_template::Thinking>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaDeleteRequest {
pub(super) model: String,
pub(super) name: Option<String>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaCopyRequest {
pub(super) source: String,
pub(super) destination: String,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaCreateRequest {
#[serde(default)]
pub(super) model: String,
#[serde(default)]
pub(super) name: String,
#[serde(default)]
pub(super) from: Option<String>,
#[serde(default)]
pub(super) files: Option<HashMap<String, String>>,
#[serde(default = "bool_true")]
pub(super) stream: bool,
#[serde(flatten)]
pub(super) unsupported: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaEmbedRequest {
pub(super) model: String,
#[serde(default)]
pub(super) input: serde_json::Value,
#[serde(default)]
pub(super) truncate: Option<bool>,
#[serde(default)]
pub(super) dimensions: Option<usize>,
#[serde(default)]
pub(super) keep_alive: Option<serde_json::Value>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaEmbedResponse {
pub(super) model: String,
pub(super) embeddings: Vec<Vec<f32>>,
pub(super) total_duration: u64,
pub(super) load_duration: u64,
pub(super) prompt_eval_count: u64,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaEmbeddingsRequest {
pub(super) model: String,
#[serde(default)]
pub(super) prompt: String,
#[serde(default)]
pub(super) keep_alive: Option<serde_json::Value>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaEmbeddingsResponse {
pub(super) embedding: Vec<f64>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OAIEmbeddingsResponse {
pub(super) data: Vec<OAIEmbeddingsDatum>,
#[serde(default)]
pub(super) usage: OAIEmbeddingsUsage,
}
#[derive(Debug, Deserialize)]
pub(super) struct OAIEmbeddingsDatum {
#[serde(default)]
pub(super) index: usize,
pub(super) embedding: Vec<f32>,
}
#[derive(Debug, Default, Deserialize)]
pub(super) struct OAIEmbeddingsUsage {
#[serde(default)]
pub(super) prompt_tokens: u64,
}
#[derive(Debug, Serialize, PartialEq, Default)]
pub(super) struct OAIMessage {
pub(super) role: String,
pub(super) content: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_calls: Option<Vec<OAIToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_call_id: Option<String>,
}
impl OAIMessage {
pub(super) fn text(role: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: role.into(),
content: serde_json::Value::String(content.into()),
..Default::default()
}
}
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(super) struct OAIToolCall {
pub(super) id: String,
#[serde(rename = "type")]
pub(super) type_: &'static str,
pub(super) function: OAIToolCallFunction,
}
#[derive(Debug, Clone, Serialize, PartialEq)]
pub(super) struct OAIToolCallFunction {
pub(super) name: String,
pub(super) arguments: String,
}
pub(super) fn ollama_message_to_oai(m: &OllamaMessage) -> OAIMessage {
let content = match &m.images {
Some(images) if !images.is_empty() => {
let mut parts = Vec::with_capacity(images.len() + 1);
if !m.content.is_empty() {
parts.push(serde_json::json!({ "type": "text", "text": m.content }));
}
for image in images {
if is_wav_base64(image) {
parts.push(serde_json::json!({
"type": "input_audio",
"input_audio": { "data": image, "format": "wav" }
}));
} else {
parts.push(serde_json::json!({
"type": "image_url",
"image_url": { "url": image_data_uri(image) }
}));
}
}
serde_json::Value::Array(parts)
}
_ => serde_json::Value::String(m.content.clone()),
};
let tool_calls = m.tool_calls.as_ref().map(|calls| {
calls
.iter()
.enumerate()
.map(|(i, c)| OAIToolCall {
id: c
.id
.clone()
.filter(|id| !id.is_empty())
.unwrap_or_else(|| format!("call_{}_{i}", gen_id())),
type_: "function",
function: OAIToolCallFunction {
name: c.function.name.clone(),
arguments: c.function.arguments.to_string(),
},
})
.collect()
});
OAIMessage {
role: m.role.clone(),
content,
tool_calls,
name: m.tool_name.clone(),
tool_call_id: m.tool_call_id.clone(),
}
}
pub(super) fn image_data_uri(base64_bytes: &str) -> String {
if base64_bytes.starts_with("data:") {
base64_bytes.to_string()
} else {
format!("data:image/png;base64,{base64_bytes}")
}
}
pub(super) fn is_wav_base64(base64_bytes: &str) -> bool {
use base64::Engine as _;
let Some(head) = base64_bytes.get(..16) else {
return false;
};
base64::engine::general_purpose::STANDARD
.decode(head)
.map(|b| b.starts_with(b"RIFF") && b[8..].starts_with(b"WAVE"))
.unwrap_or(false)
}
#[derive(Debug, Serialize, Default)]
pub(super) struct OAIChatRequest {
pub(super) model: String,
pub(super) messages: Vec<OAIMessage>,
pub(super) stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) top_p: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) max_tokens: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) repeat_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) chat_template_kwargs: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tools: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_choice: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) response_format: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) seed: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) stop: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) presence_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) frequency_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) top_k: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) min_p: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) stream_options: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) reasoning_budget_tokens: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) reasoning_budget_message: Option<&'static str>,
}
pub(super) const DEFAULT_REPEAT_PENALTY: f32 = 1.0;
#[derive(Debug, Deserialize)]
pub(super) struct OAIChunk {
#[serde(default)]
pub(super) choices: Vec<OAIChunkChoice>,
#[serde(default)]
pub(super) usage: Option<OAIUsage>,
#[serde(default)]
pub(super) timings: Option<LlamaTimings>,
}
#[derive(Debug, Deserialize, Default, Clone, Copy)]
pub(super) struct OAIUsage {
#[serde(default)]
pub(super) prompt_tokens: u64,
#[serde(default)]
pub(super) completion_tokens: u64,
#[serde(default)]
pub(super) total_tokens: u64,
#[serde(default)]
pub(super) prompt_tokens_details: OAIPromptTokensDetails,
#[serde(default)]
pub(super) completion_tokens_details: OAICompletionTokensDetails,
}
#[derive(Debug, Deserialize, Default, Clone, Copy)]
pub(super) struct OAICompletionTokensDetails {
#[serde(default)]
pub(super) reasoning_tokens: Option<u64>,
}
#[derive(Debug, Deserialize, Default, Clone, Copy)]
pub(super) struct OAIPromptTokensDetails {
#[serde(default)]
pub(super) cached_tokens: Option<u64>,
#[serde(default)]
pub(super) cache_write_tokens: Option<u64>,
}
#[derive(Debug, Deserialize, Default, Clone, Copy)]
pub(super) struct LlamaTimings {
#[serde(default)]
pub(super) prompt_n: u64,
#[serde(default)]
pub(super) prompt_ms: f64,
#[serde(default)]
pub(super) cache_n: Option<u64>,
#[serde(default)]
pub(super) predicted_n: u64,
#[serde(default)]
pub(super) predicted_ms: f64,
}
#[derive(Debug, Deserialize)]
pub(super) struct OAIChunkChoice {
pub(super) delta: OAIChunkDelta,
pub(super) finish_reason: Option<String>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OAIChunkDelta {
pub(super) content: Option<String>,
pub(super) reasoning_content: Option<String>,
pub(super) reasoning: Option<String>,
pub(super) thinking: Option<String>,
#[serde(default)]
pub(super) tool_calls: Option<Vec<OAIToolCallDelta>>,
}
#[derive(Debug, Deserialize, Default)]
pub(super) struct OAIToolCallDelta {
#[serde(default)]
pub(super) index: Option<usize>,
#[serde(default)]
pub(super) id: Option<String>,
#[serde(default)]
pub(super) function: Option<OAIToolCallFunctionDelta>,
}
#[derive(Debug, Deserialize, Default)]
pub(super) struct OAIToolCallFunctionDelta {
#[serde(default)]
pub(super) name: Option<String>,
#[serde(default)]
pub(super) arguments: Option<String>,
}
#[derive(Default, Clone)]
pub(super) struct ToolCallAccumulator {
pub(super) id: String,
pub(super) name: String,
pub(super) arguments: String,
}
pub(super) fn oai_chunk_tool_call_deltas(payload: &str) -> Vec<OAIToolCallDelta> {
if payload == "[DONE]" {
return Vec::new();
}
serde_json::from_str::<OAIChunk>(payload)
.ok()
.and_then(|c| c.choices.into_iter().next())
.and_then(|c| c.delta.tool_calls)
.unwrap_or_default()
}
pub(super) fn accumulate_tool_call_deltas(
payload: &str,
acc: &std::cell::RefCell<std::collections::BTreeMap<usize, ToolCallAccumulator>>,
) {
let deltas = oai_chunk_tool_call_deltas(payload);
if deltas.is_empty() {
return;
}
let mut acc = acc.borrow_mut();
for (position, delta) in deltas.into_iter().enumerate() {
let entry = acc.entry(delta.index.unwrap_or(position)).or_default();
if let Some(id) = delta.id.filter(|id| !id.is_empty()) {
if entry.id.is_empty() {
entry.id = id;
}
}
if let Some(f) = delta.function {
if let Some(name) = f.name {
entry.name.push_str(&name);
}
if let Some(args) = f.arguments {
entry.arguments.push_str(&args);
}
}
}
}
pub(super) fn finalize_tool_calls(
acc: &std::collections::BTreeMap<usize, ToolCallAccumulator>,
) -> Option<Vec<OllamaToolCall>> {
if acc.is_empty() {
return None;
}
Some(
acc.iter()
.map(|(index, c)| OllamaToolCall {
id: Some(if c.id.is_empty() {
format!("call_{}_{index}", gen_id())
} else {
c.id.clone()
}),
function: OllamaToolCallFunction {
index: *index,
name: c.name.clone(),
arguments: serde_json::from_str(&c.arguments)
.unwrap_or_else(|_| serde_json::json!({})),
},
})
.collect(),
)
}
pub(super) fn bool_true() -> bool {
true
}
pub(super) fn gen_id() -> String {
use std::time::SystemTime;
let secs = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
format!("{secs:032x}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_to_response_format_maps_json_string_and_schema_object() {
assert_eq!(
format_to_response_format(&Some(serde_json::json!("json"))),
Some(serde_json::json!({ "type": "json_object" }))
);
let schema = serde_json::json!({
"type": "object",
"properties": { "answer": { "type": "string" } }
});
assert_eq!(
format_to_response_format(&Some(schema.clone())),
Some(serde_json::json!({
"type": "json_schema",
"json_schema": { "name": "response", "schema": schema, "strict": true }
}))
);
}
#[test]
fn format_to_response_format_is_a_no_op_when_absent_or_unrecognized() {
assert_eq!(format_to_response_format(&None), None);
assert_eq!(
format_to_response_format(&Some(serde_json::json!(true))),
None
);
assert_eq!(
format_to_response_format(&Some(serde_json::json!("text"))),
None
);
}
#[test]
fn ollama_message_to_oai_plain_text_has_string_content_and_no_extras() {
let m = OllamaMessage {
role: "user".into(),
content: "hi".into(),
..Default::default()
};
let oai = ollama_message_to_oai(&m);
assert_eq!(oai.role, "user");
assert_eq!(oai.content, serde_json::json!("hi"));
assert_eq!(oai.tool_calls, None);
assert_eq!(oai.name, None);
}
#[test]
fn ollama_message_to_oai_with_images_builds_a_content_parts_array() {
let m = OllamaMessage {
role: "user".into(),
content: "what is this?".into(),
images: Some(vec!["Zm9v".into()]),
..Default::default()
};
let oai = ollama_message_to_oai(&m);
assert_eq!(
oai.content,
serde_json::json!([
{ "type": "text", "text": "what is this?" },
{ "type": "image_url", "image_url": { "url": "data:image/png;base64,Zm9v" } }
])
);
}
#[test]
fn ollama_message_to_oai_with_only_an_image_omits_the_empty_text_part() {
let m = OllamaMessage {
role: "user".into(),
images: Some(vec!["Zm9v".into()]),
..Default::default()
};
let oai = ollama_message_to_oai(&m);
assert_eq!(
oai.content,
serde_json::json!([
{ "type": "image_url", "image_url": { "url": "data:image/png;base64,Zm9v" } }
])
);
}
#[test]
fn ollama_message_to_oai_carries_tool_calls_and_re_encodes_arguments_as_a_string() {
let m = OllamaMessage {
role: "assistant".into(),
tool_calls: Some(vec![OllamaToolCall {
id: None,
function: OllamaToolCallFunction {
index: 0,
name: "get_weather".into(),
arguments: serde_json::json!({ "city": "nyc" }),
},
}]),
..Default::default()
};
let oai = ollama_message_to_oai(&m);
let calls = oai.tool_calls.expect("tool_calls must be carried over");
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].function.name, "get_weather");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&calls[0].function.arguments).unwrap(),
serde_json::json!({ "city": "nyc" })
);
}
#[test]
fn ollama_message_to_oai_maps_tool_name_to_name_on_a_tool_result_message() {
let m = OllamaMessage {
role: "tool".into(),
content: "72F and sunny".into(),
tool_name: Some("get_weather".into()),
tool_call_id: Some("call_abc".into()),
..Default::default()
};
let oai = ollama_message_to_oai(&m);
assert_eq!(oai.name.as_deref(), Some("get_weather"));
assert_eq!(oai.tool_call_id.as_deref(), Some("call_abc"));
}
#[test]
fn ollama_message_to_oai_keeps_a_tool_calls_own_id() {
let m = OllamaMessage {
role: "assistant".into(),
tool_calls: Some(vec![
OllamaToolCall {
id: Some("call_given".into()),
..Default::default()
},
OllamaToolCall::default(),
OllamaToolCall {
id: Some(String::new()),
..Default::default()
},
]),
..Default::default()
};
let calls = ollama_message_to_oai(&m).tool_calls.unwrap();
assert_eq!(calls[0].id, "call_given");
assert!(calls[1].id.starts_with("call_"), "{}", calls[1].id);
assert!(calls[2].id.starts_with("call_"), "{}", calls[2].id);
}
#[test]
fn generate_system_and_images_become_messages() {
let user = OllamaMessage {
role: "user".into(),
content: "what is this".into(),
images: Some(vec!["AAAA".into()]),
..Default::default()
};
let oai = ollama_message_to_oai(&user);
assert_eq!(oai.content[0]["text"], "what is this");
assert_eq!(oai.content[1]["type"], "image_url");
}
#[test]
fn image_data_uri_wraps_bare_base64_and_passes_through_existing_data_uris() {
assert_eq!(image_data_uri("Zm9v"), "data:image/png;base64,Zm9v");
assert_eq!(
image_data_uri("data:image/jpeg;base64,Zm9v"),
"data:image/jpeg;base64,Zm9v"
);
}
#[test]
fn tool_call_accumulator_assembles_fragmented_streaming_deltas() {
let acc = std::cell::RefCell::new(std::collections::BTreeMap::new());
accumulate_tool_call_deltas(
r#"{"choices":[{"delta":{"tool_calls":[
{"index":0,"id":"call_1","type":"function","function":{"name":"get_weather","arguments":"{\"city\":"}}
]},"finish_reason":null}]}"#,
&acc,
);
accumulate_tool_call_deltas(
r#"{"choices":[{"delta":{"tool_calls":[
{"index":0,"function":{"arguments":"\"nyc\"}"}}
]},"finish_reason":null}]}"#,
&acc,
);
let calls = finalize_tool_calls(&acc.borrow()).expect("must assemble one tool call");
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].function.name, "get_weather");
assert_eq!(
calls[0].function.arguments,
serde_json::json!({ "city": "nyc" })
);
}
#[test]
fn finalize_tool_calls_is_none_when_no_tool_calls_were_made() {
assert_eq!(
finalize_tool_calls(&std::collections::BTreeMap::new()),
None
);
}
#[test]
fn finalize_tool_calls_falls_back_to_an_empty_object_on_unparseable_arguments() {
let mut acc = std::collections::BTreeMap::new();
acc.insert(
0,
ToolCallAccumulator {
name: "f".into(),
arguments: "not json".into(),
..Default::default()
},
);
let calls = finalize_tool_calls(&acc).unwrap();
assert_eq!(calls[0].function.arguments, serde_json::json!({}));
}
#[test]
fn oai_chunk_tool_call_deltas_is_empty_for_done_sentinel_and_ordinary_content() {
assert!(oai_chunk_tool_call_deltas("[DONE]").is_empty());
assert!(oai_chunk_tool_call_deltas(
r#"{"choices":[{"delta":{"content":"hi"},"finish_reason":null}]}"#
)
.is_empty());
}
#[test]
fn resolve_think_replaces_only_a_level_the_model_does_not_take() {
use serde_json::json;
let thinking = crate::chat_template::Thinking {
values: vec![json!(false), json!("low"), json!("medium")],
default: json!("medium"),
};
let resolve = |think: serde_json::Value, thinking| resolve_think(&Some(think), thinking);
assert_eq!(
resolve(json!("xhigh"), Some(&thinking)),
Some(json!("medium"))
);
assert_eq!(resolve(json!("low"), Some(&thinking)), Some(json!("low")));
assert_eq!(
resolve(json!(" low "), Some(&thinking)),
Some(json!(" low "))
);
assert_eq!(resolve(json!(true), Some(&thinking)), Some(json!(true)));
assert_eq!(resolve(json!("xhigh"), None), Some(json!("xhigh")));
assert_eq!(resolve_think(&None, Some(&thinking)), None);
}
#[test]
fn think_to_chat_template_kwargs_maps_booleans_and_reasoning_levels() {
assert_eq!(
think_to_chat_template_kwargs(&Some(serde_json::json!(true))),
Some(serde_json::json!({ "enable_thinking": true }))
);
assert_eq!(
think_to_chat_template_kwargs(&Some(serde_json::json!(false))),
Some(serde_json::json!({ "enable_thinking": false }))
);
for level in crate::chat_template::EFFORT_LEVELS {
assert_eq!(
think_to_chat_template_kwargs(&Some(serde_json::json!(level))),
Some(serde_json::json!({
"enable_thinking": true,
"reasoning_effort": level,
})),
"string level {level:?}"
);
}
for not_a_level in ["", " ", "verbose", "LOW", "none"] {
assert_eq!(
think_to_chat_template_kwargs(&Some(serde_json::json!(not_a_level))),
None,
"string {not_a_level:?}"
);
}
assert_eq!(think_to_chat_template_kwargs(&None), None);
assert_eq!(
think_to_chat_template_kwargs(&Some(serde_json::Value::Null)),
None
);
}
}