From 84004fd5767abc94a5360b742e7fd0c8a79c6c55 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 31 Oct 2023 06:56:00 +0800 Subject: feat: support OPENAI_API_BASE (#186) --- src/client/openai.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/client/openai.rs b/src/client/openai.rs index 4ccb1b7..e0d321d 100644 --- a/src/client/openai.rs +++ b/src/client/openai.rs @@ -14,7 +14,7 @@ use serde_json::{json, Value}; use std::env; use std::time::Duration; -const API_URL: &str = "https://api.openai.com/v1/chat/completions"; +const API_BASE: &str = "https://api.openai.com/v1"; #[derive(Debug)] pub struct OpenAIClient { @@ -161,7 +161,12 @@ impl OpenAIClient { .with_context(|| "Failed to build client")? }; - let mut builder = client.post(API_URL).bearer_auth(api_key).json(&body); + let api_base = env::var("OPENAI_API_BASE") + .ok() + .unwrap_or_else(|| API_BASE.to_string()); + let url = format!("{api_base}/chat/completions"); + + let mut builder = client.post(url).bearer_auth(api_key).json(&body); if let Some(organization_id) = &self.local_config.organization_id { builder = builder.header("OpenAI-Organization", organization_id); -- cgit v1.2.3