Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extra_fields to models of localai/ollama clients #298

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ clients:
models:
- name: mistral
max_tokens: 8192
extra_fields: # Optional field, set custom parameters
key: value
- name: llava
max_tokens: 8192
capabilities: text,vision # Optional field, possible values: text, vision
Expand Down
4 changes: 3 additions & 1 deletion src/client/localai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl LocalAIClient {
Model::new(client_name, &v.name)
.set_capabilities(v.capabilities)
.set_max_tokens(v.max_tokens)
.set_extra_fields(v.extra_fields.clone())
.set_tokens_count_factors(OPENAI_TOKENS_COUNT_FACTORS)
})
.collect()
Expand All @@ -53,7 +54,8 @@ impl LocalAIClient {
fn request_builder(&self, client: &ReqwestClient, data: SendData) -> Result<RequestBuilder> {
let api_key = self.get_api_key().ok();

let body = openai_build_body(data, self.model.name.clone());
let mut body = openai_build_body(data, self.model.name.clone());
self.model.merge_extra_fields(&mut body);

let chat_endpoint = self
.config
Expand Down
21 changes: 21 additions & 0 deletions src/client/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Model {
pub client_name: String,
pub name: String,
pub max_tokens: Option<usize>,
pub extra_fields: Option<serde_json::Map<String, serde_json::Value>>,
pub tokens_count_factors: TokensCountFactors,
pub capabilities: ModelCapabilities,
}
Expand All @@ -27,6 +28,7 @@ impl Model {
Self {
client_name: client_name.into(),
name: name.into(),
extra_fields: None,
max_tokens: None,
tokens_count_factors: Default::default(),
capabilities: ModelCapabilities::Text,
Expand Down Expand Up @@ -73,6 +75,14 @@ impl Model {
self
}

pub fn set_extra_fields(
mut self,
extra_fields: Option<serde_json::Map<String, serde_json::Value>>,
) -> Self {
self.extra_fields = extra_fields;
self
}

pub fn set_max_tokens(mut self, max_tokens: Option<usize>) -> Self {
match max_tokens {
None | Some(0) => self.max_tokens = None,
Expand Down Expand Up @@ -122,12 +132,23 @@ impl Model {
}
Ok(())
}

pub fn merge_extra_fields(&self, body: &mut serde_json::Value) {
if let (Some(body), Some(extra_fields)) = (body.as_object_mut(), &self.extra_fields) {
for (k, v) in extra_fields {
if !body.contains_key(k) {
body.insert(k.clone(), v.clone());
}
}
}
}
}

#[derive(Debug, Clone, Deserialize)]
pub struct ModelConfig {
pub name: String,
pub max_tokens: Option<usize>,
pub extra_fields: Option<serde_json::Map<String, serde_json::Value>>,
#[serde(deserialize_with = "deserialize_capabilities")]
#[serde(default = "default_capabilities")]
pub capabilities: ModelCapabilities,
Expand Down
5 changes: 4 additions & 1 deletion src/client/ollama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl OllamaClient {
Model::new(client_name, &v.name)
.set_capabilities(v.capabilities)
.set_max_tokens(v.max_tokens)
.set_extra_fields(v.extra_fields.clone())
.set_tokens_count_factors(TOKENS_COUNT_FACTORS)
})
.collect()
Expand All @@ -77,7 +78,9 @@ impl OllamaClient {
fn request_builder(&self, client: &ReqwestClient, data: SendData) -> Result<RequestBuilder> {
let api_key = self.get_api_key().ok();

let body = build_body(data, self.model.name.clone())?;
let mut body = build_body(data, self.model.name.clone())?;

self.model.merge_extra_fields(&mut body);

let chat_endpoint = self.config.chat_endpoint.as_deref().unwrap_or("/api/chat");

Expand Down
Loading