From f5e8e872b08d2aa9b3c063b0a98c59f842f94ecc Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 27 Jul 2024 21:33:04 +0800 Subject: feat: support patching request url, headers and body (#756) --- src/client/ollama.rs | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) (limited to 'src/client/ollama.rs') diff --git a/src/client/ollama.rs b/src/client/ollama.rs index 299cae7..5c26b99 100644 --- a/src/client/ollama.rs +++ b/src/client/ollama.rs @@ -1,7 +1,7 @@ use super::*; use anyhow::{bail, Context, Result}; -use reqwest::{Client as ReqwestClient, RequestBuilder}; +use reqwest::RequestBuilder; use serde::Deserialize; use serde_json::{json, Value}; @@ -12,7 +12,7 @@ pub struct OllamaConfig { pub api_auth: Option, #[serde(default)] pub models: Vec, - pub patch: Option, + pub patch: Option, pub extra: Option, } @@ -32,52 +32,41 @@ impl OllamaClient { ), ]; - fn chat_completions_builder( - &self, - client: &ReqwestClient, - data: ChatCompletionsData, - ) -> Result { + fn prepare_chat_completions(&self, data: ChatCompletionsData) -> Result { let api_base = self.get_api_base()?; let api_auth = self.get_api_auth().ok(); - let mut body = build_chat_completions_body(data, &self.model)?; - self.patch_chat_completions_body(&mut body); - let url = format!("{api_base}/api/chat"); - debug!("Ollama Chat Completions Request: {url} {body}"); + let body = build_chat_completions_body(data, &self.model)?; + + let mut request_data = RequestData::new(url, body); - let mut builder = client.post(url).json(&body); if let Some(api_auth) = api_auth { - builder = builder.header("Authorization", api_auth) + request_data.header("Authorization", api_auth) } - Ok(builder) + Ok(request_data) } - fn embeddings_builder( - &self, - client: &ReqwestClient, - data: EmbeddingsData, - ) -> Result { + fn prepare_embeddings(&self, data: EmbeddingsData) -> Result { let api_base = self.get_api_base()?; let api_auth = self.get_api_auth().ok(); + let url = format!("{api_base}/api/embed"); + let body = json!({ "model": self.model.name(), "input": data.texts, }); - let url = format!("{api_base}/api/embed"); - - debug!("Ollama Embeddings Request: {url} {body}"); + let mut request_data = RequestData::new(url, body); - let mut builder = client.post(url).json(&body); if let Some(api_auth) = api_auth { - builder = builder.header("Authorization", api_auth) + request_data.header("Authorization", api_auth) } - Ok(builder) + Ok(request_data) } } -- cgit v1.2.3