fix: make llm provider async and add tool toggle

This commit is contained in:
Dave
2025-12-24 17:32:46 +00:00
parent d9cd16601b
commit b241c47fd9
8 changed files with 149 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
use crate::llm::types::{
CompletionResponse, FunctionCall, Message, ModelProvider, Role, ToolCall, ToolDefinition,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
@@ -76,14 +77,15 @@ struct OllamaResponseFunctionCall {
arguments: Value, // Ollama returns Object, we convert to String for internal storage
}
#[async_trait]
impl ModelProvider for OllamaProvider {
fn chat(
async fn chat(
&self,
model: &str,
messages: &[Message],
tools: &[ToolDefinition],
) -> Result<CompletionResponse, String> {
let client = reqwest::blocking::Client::new();
let client = reqwest::Client::new();
let url = format!("{}/api/chat", self.base_url.trim_end_matches('/'));
// Convert domain Messages to Ollama Messages (handling String -> Object args mismatch)
@@ -129,16 +131,18 @@ impl ModelProvider for OllamaProvider {
.post(&url)
.json(&request_body)
.send()
.await
.map_err(|e| format!("Request failed: {}", e))?;
if !res.status().is_success() {
let status = res.status();
let text = res.text().unwrap_or_default();
let text = res.text().await.unwrap_or_default();
return Err(format!("Ollama API error {}: {}", status, text));
}
let response_body: OllamaResponse = res
.json()
.await
.map_err(|e| format!("Failed to parse response: {}", e))?;
// Convert Response back to Domain types

View File

@@ -1,3 +1,4 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
@@ -62,8 +63,9 @@ pub struct CompletionResponse {
}
/// The abstraction for different LLM providers (Ollama, Anthropic, etc.)
#[async_trait]
pub trait ModelProvider: Send + Sync {
fn chat(
async fn chat(
&self,
model: &str,
messages: &[Message],