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: vertexai support mistral models #746

Merged
merged 1 commit into from
Jul 25, 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
16 changes: 15 additions & 1 deletion models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
- platform: vertexai
# docs:
# - https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models
# - https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude
# - https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models
# - https://cloud.google.com/vertex-ai/generative-ai/pricing
# - https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini
# notes:
Expand Down Expand Up @@ -335,6 +335,20 @@
output_price: 1.25
supports_vision: true
supports_function_calling: true
- name: mistral-large@2407
max_input_tokens: 128000
input_price: 3
output_price: 9
supports_function_calling: true
- name: mistral-nemo@2407
max_input_tokens: 128000
input_price: 0.3
output_price: 0.3
supports_function_calling: true
- name: codestral@2405
max_input_tokens: 32000
input_price: 1
output_price: 3
- name: text-embedding-004
type: embedding
max_input_tokens: 3072
Expand Down
54 changes: 42 additions & 12 deletions src/client/vertexai.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::access_token::*;
use super::claude::*;
use super::openai::*;
use super::*;

use anyhow::{anyhow, bail, Context, Result};
Expand Down Expand Up @@ -56,6 +57,13 @@ impl VertexAIClient {
ModelCategory::Claude => {
format!("{base_url}/anthropic/models/{model_name}:streamRawPredict")
}
ModelCategory::Mistral => {
let func = match data.stream {
true => "streamRawPredict",
false => "rawPredict",
};
format!("{base_url}/mistralai/models/{model_name}:{func}")
}
};

let mut body = match model_category {
Expand All @@ -68,6 +76,13 @@ impl VertexAIClient {
body["anthropic_version"] = "vertex-2023-10-16".into();
body
}
ModelCategory::Mistral => {
let mut body = openai_build_chat_completions_body(data, &self.model);
if let Some(body_obj) = body.as_object_mut() {
body_obj["model"] = strip_model_version(self.model.name()).into();
}
body
}
};
self.patch_chat_completions_body(&mut body);

Expand Down Expand Up @@ -122,6 +137,7 @@ impl Client for VertexAIClient {
match model_category {
ModelCategory::Gemini => gemini_chat_completions(builder).await,
ModelCategory::Claude => claude_chat_completions(builder).await,
ModelCategory::Mistral => openai_chat_completions(builder).await,
}
}

Expand All @@ -137,6 +153,7 @@ impl Client for VertexAIClient {
match model_category {
ModelCategory::Gemini => gemini_chat_completions_streaming(builder, handler).await,
ModelCategory::Claude => claude_chat_completions_streaming(builder, handler).await,
ModelCategory::Mistral => openai_chat_completions_streaming(builder, handler).await,
}
}

Expand Down Expand Up @@ -379,16 +396,19 @@ pub fn gemini_build_chat_completions_body(

if let Some(functions) = functions {
// Gemini doesn't support functions with parameters that have empty properties, so we need to patch it.
let function_declarations: Vec<_> = functions.into_iter().map(|function| {
if function.parameters.is_empty_properties() {
json!({
"name": function.name,
"description": function.description,
})
} else {
json!(function)
}
}).collect();
let function_declarations: Vec<_> = functions
.into_iter()
.map(|function| {
if function.parameters.is_empty_properties() {
json!({
"name": function.name,
"description": function.description,
})
} else {
json!(function)
}
})
.collect();
body["tools"] = json!([{ "functionDeclarations": function_declarations }]);
}

Expand All @@ -399,16 +419,19 @@ pub fn gemini_build_chat_completions_body(
enum ModelCategory {
Gemini,
Claude,
Mistral,
}

impl FromStr for ModelCategory {
type Err = anyhow::Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if s.starts_with("gemini-") {
if s.starts_with("gemini") {
Ok(ModelCategory::Gemini)
} else if s.starts_with("claude-") {
} else if s.starts_with("claude") {
Ok(ModelCategory::Claude)
} else if s.starts_with("mistral") || s.starts_with("codestral") {
Ok(ModelCategory::Mistral)
} else {
unsupported_model!(s)
}
Expand Down Expand Up @@ -496,3 +519,10 @@ fn default_adc_file() -> Option<PathBuf> {
path.push("application_default_credentials.json");
Some(path)
}

fn strip_model_version(name: &str) -> &str {
match name.split_once('@') {
Some((v, _)) => v,
None => name,
}
}