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

Support Azure OpenAI Service #35

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/config/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Api {
Groq,
Mistral,
Openai,
AzureOpenai,
}

impl FromStr for Api {
Expand All @@ -29,6 +30,7 @@ impl FromStr for Api {
match s.to_lowercase().as_str() {
"ollama" => Ok(Api::Ollama),
"openai" => Ok(Api::Openai),
"azureopenai" => Ok(Api::AzureOpenai),
"mistral" => Ok(Api::Mistral),
"groq" => Ok(Api::Groq),
"anthropic" => Ok(Api::Anthropic),
Expand All @@ -42,6 +44,7 @@ impl ToString for Api {
match self {
Api::Ollama => "ollama".to_string(),
Api::Openai => "openai".to_string(),
Api::AzureOpenai => "azureopenai".to_string(),
Api::Mistral => "mistral".to_string(),
Api::Groq => "groq".to_string(),
Api::Anthropic => "anthropic".to_string(),
Expand Down Expand Up @@ -132,6 +135,17 @@ impl ApiConfig {
}
}

pub(super) fn azureopenai() -> Self {
ApiConfig {
api_key_command: None,
api_key: None,
url: String::from("https://your-azure-endpoint.azure.com/openai/deployments/your-deployment-id/chat/completions?api-version=2024-06-01"),
default_model: Some(String::from("gpt-4o")),
version: None,
timeout_seconds: Some(180),
d5ve marked this conversation as resolved.
Show resolved Hide resolved
}
}

pub(super) fn mistral() -> Self {
ApiConfig {
api_key_command: None,
Expand Down Expand Up @@ -174,6 +188,7 @@ pub(super) fn generate_api_keys_file() -> std::io::Result<()> {
let mut api_config = HashMap::new();
api_config.insert(Api::Ollama.to_string(), ApiConfig::ollama());
api_config.insert(Api::Openai.to_string(), ApiConfig::openai());
api_config.insert(Api::AzureOpenai.to_string(), ApiConfig::azureopenai());
api_config.insert(Api::Mistral.to_string(), ApiConfig::mistral());
api_config.insert(Api::Groq.to_string(), ApiConfig::groq());
api_config.insert(Api::Anthropic.to_string(), ApiConfig::anthropic());
Expand Down
5 changes: 3 additions & 2 deletions src/text/api_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn post_prompt_and_get_answer(
.expect("Unable to initialize HTTP client");

let prompt_format = match prompt.api {
Api::Ollama | Api::Openai | Api::Mistral | Api::Groq => {
Api::Ollama | Api::Openai | Api::AzureOpenai | Api::Mistral | Api::Groq => {
PromptFormat::OpenAi(OpenAiPrompt::from(prompt.clone()))
}
Api::Anthropic => PromptFormat::Anthropic(AnthropicPrompt::from(prompt.clone())),
Expand All @@ -67,6 +67,7 @@ pub fn post_prompt_and_get_answer(
"Authorization",
&format!("Bearer {}", &api_config.get_api_key()),
),
Api::AzureOpenai => request.header("api-key", &api_config.get_api_key()),
Api::Anthropic => request
.header("x-api-key", &api_config.get_api_key())
.header(
Expand All @@ -80,7 +81,7 @@ pub fn post_prompt_and_get_answer(

let response_text: String = match prompt.api {
Api::Ollama => handle_api_response::<OllamaResponse>(request.send()?),
Api::Openai | Api::Mistral | Api::Groq => {
Api::Openai | Api::AzureOpenai | Api::Mistral | Api::Groq => {
handle_api_response::<OpenAiResponse>(request.send()?)
}
Api::Anthropic => handle_api_response::<AnthropicResponse>(request.send()?),
Expand Down