Introduce the support of Azure, Gemini, vLLM

This commit is contained in:
Clément Renault 2025-06-06 12:08:37 +02:00
parent 4dfb89168b
commit 70670c3be4
No known key found for this signature in database
GPG key ID: F250A4C4E3AE5F5F
8 changed files with 261 additions and 18 deletions

View file

@ -391,6 +391,10 @@ EditDocumentsByFunctionError , InvalidRequest , BAD_REQU
InvalidSettingsIndexChat , InvalidRequest , BAD_REQUEST ;
// Experimental features - Chat Completions
ChatWorkspaceNotFound , InvalidRequest , NOT_FOUND ;
InvalidChatCompletionOrgId , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionProjectId , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionApiVersion , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionDeploymentId , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionSource , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionBaseApi , InvalidRequest , BAD_REQUEST ;
InvalidChatCompletionApiKey , InvalidRequest , BAD_REQUEST ;

View file

@ -51,6 +51,14 @@ pub struct Network {
pub struct ChatCompletionSettings {
pub source: ChatCompletionSource,
#[serde(default)]
pub org_id: Option<String>,
#[serde(default)]
pub project_id: Option<String>,
#[serde(default)]
pub api_version: Option<String>,
#[serde(default)]
pub deployment_id: Option<String>,
#[serde(default)]
pub base_api: Option<String>,
#[serde(default)]
pub api_key: Option<String>,
@ -88,6 +96,43 @@ impl ChatCompletionSettings {
pub enum ChatCompletionSource {
#[default]
OpenAi,
AzureOpenAi,
Mistral,
Gemini,
VLlm,
}
impl ChatCompletionSource {
pub fn system_role(&self, model: &str) -> &'static str {
match self {
ChatCompletionSource::OpenAi if Self::old_openai_model(model) => "system",
ChatCompletionSource::OpenAi => "developer",
ChatCompletionSource::AzureOpenAi if Self::old_openai_model(model) => "system",
ChatCompletionSource::AzureOpenAi => "developer",
ChatCompletionSource::Mistral => "system",
ChatCompletionSource::Gemini => "system",
ChatCompletionSource::VLlm => "system",
}
}
/// Returns true if the model is an old OpenAI model.
///
/// Old OpenAI models use the system role while new ones use the developer role.
fn old_openai_model(model: &str) -> bool {
["gpt-3.5", "gpt-4", "gpt-4.1", "gpt-4.5", "gpt-4o", "chatgpt-4o"].iter().any(|old| {
model.starts_with(old) && model.chars().nth(old.len()).is_none_or(|last| last == '-')
})
}
pub fn base_url(&self) -> Option<&'static str> {
use ChatCompletionSource::*;
match self {
OpenAi => Some("https://api.openai.com/v1/"),
Mistral => Some("https://api.mistral.ai/v1/"),
Gemini => Some("https://generativelanguage.googleapis.com/v1beta/openai/"),
AzureOpenAi | VLlm => None,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
@ -111,3 +156,85 @@ impl Default for ChatCompletionPrompts {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL_OPENAI_MODELS_OLDINESS: &[(&str, bool)] = &[
("gpt-4-0613", true),
("gpt-4", true),
("gpt-3.5-turbo", true),
("gpt-4o-audio-preview-2025-06-03", true),
("gpt-4.1-nano", true),
("gpt-4o-realtime-preview-2025-06-03", true),
("gpt-3.5-turbo-instruct", true),
("gpt-3.5-turbo-instruct-0914", true),
("gpt-4-1106-preview", true),
("gpt-3.5-turbo-1106", true),
("gpt-4-0125-preview", true),
("gpt-4-turbo-preview", true),
("gpt-3.5-turbo-0125", true),
("gpt-4-turbo", true),
("gpt-4-turbo-2024-04-09", true),
("gpt-4o", true),
("gpt-4o-2024-05-13", true),
("gpt-4o-mini-2024-07-18", true),
("gpt-4o-mini", true),
("gpt-4o-2024-08-06", true),
("chatgpt-4o-latest", true),
("gpt-4o-realtime-preview-2024-10-01", true),
("gpt-4o-audio-preview-2024-10-01", true),
("gpt-4o-audio-preview", true),
("gpt-4o-realtime-preview", true),
("gpt-4o-realtime-preview-2024-12-17", true),
("gpt-4o-audio-preview-2024-12-17", true),
("gpt-4o-mini-realtime-preview-2024-12-17", true),
("gpt-4o-mini-audio-preview-2024-12-17", true),
("gpt-4o-mini-realtime-preview", true),
("gpt-4o-mini-audio-preview", true),
("gpt-4o-2024-11-20", true),
("gpt-4.5-preview", true),
("gpt-4.5-preview-2025-02-27", true),
("gpt-4o-search-preview-2025-03-11", true),
("gpt-4o-search-preview", true),
("gpt-4o-mini-search-preview-2025-03-11", true),
("gpt-4o-mini-search-preview", true),
("gpt-4o-transcribe", true),
("gpt-4o-mini-transcribe", true),
("gpt-4o-mini-tts", true),
("gpt-4.1-2025-04-14", true),
("gpt-4.1", true),
("gpt-4.1-mini-2025-04-14", true),
("gpt-4.1-mini", true),
("gpt-4.1-nano-2025-04-14", true),
("gpt-3.5-turbo-16k", true),
//
// new models
("o1-preview-2024-09-12", false),
("o1-preview", false),
("o1-mini-2024-09-12", false),
("o1-mini", false),
("o1-2024-12-17", false),
("o1", false),
("o3-mini", false),
("o3-mini-2025-01-31", false),
("o1-pro-2025-03-19", false),
("o1-pro", false),
("o3-2025-04-16", false),
("o4-mini-2025-04-16", false),
("o3", false),
("o4-mini", false),
];
#[test]
fn old_openai_models() {
for (name, is_old) in ALL_OPENAI_MODELS_OLDINESS.iter().copied() {
assert_eq!(
ChatCompletionSource::old_openai_model(name),
is_old,
"Model {name} is not considered old"
);
}
}
}