feat: allow setting openai base path (#1369)

This commit is contained in:
Yingjie He
2025-02-25 14:27:03 -08:00
committed by GitHub
parent b332c1c36e
commit e51b7be367
4 changed files with 25 additions and 17 deletions
@@ -3,7 +3,7 @@
"name": "OpenAI",
"description": "Use GPT-4 and other OpenAI models",
"models": ["gpt-4o", "gpt-4-turbo","o1"],
"required_keys": ["OPENAI_API_KEY", "OPENAI_HOST"]
"required_keys": ["OPENAI_API_KEY", "OPENAI_HOST", "OPENAI_BASE_PATH"]
},
"anthropic": {
"name": "Anthropic",
+7 -1
View File
@@ -28,6 +28,7 @@ pub struct OpenAiProvider {
#[serde(skip)]
client: Client,
host: String,
base_path: String,
api_key: String,
organization: Option<String>,
project: Option<String>,
@@ -48,6 +49,9 @@ impl OpenAiProvider {
let host: String = config
.get("OPENAI_HOST")
.unwrap_or_else(|_| "https://api.openai.com".to_string());
let base_path: String = config
.get("OPENAI_BASE_PATH")
.unwrap_or_else(|_| "v1/chat/completions".to_string());
let organization: Option<String> = config.get("OPENAI_ORGANIZATION").ok();
let project: Option<String> = config.get("OPENAI_PROJECT").ok();
let client = Client::builder()
@@ -57,6 +61,7 @@ impl OpenAiProvider {
Ok(Self {
client,
host,
base_path,
api_key,
organization,
project,
@@ -67,7 +72,7 @@ impl OpenAiProvider {
async fn post(&self, payload: Value) -> Result<Value, ProviderError> {
let base_url = url::Url::parse(&self.host)
.map_err(|e| ProviderError::RequestFailed(format!("Invalid base URL: {e}")))?;
let url = base_url.join("v1/chat/completions").map_err(|e| {
let url = base_url.join(&self.base_path).map_err(|e| {
ProviderError::RequestFailed(format!("Failed to construct endpoint URL: {e}"))
})?;
@@ -108,6 +113,7 @@ impl Provider for OpenAiProvider {
vec![
ConfigKey::new("OPENAI_API_KEY", true, true, None),
ConfigKey::new("OPENAI_HOST", true, false, Some("https://api.openai.com")),
ConfigKey::new("OPENAI_BASE_PATH", true, false, Some("v1/chat/completions")),
ConfigKey::new("OPENAI_ORGANIZATION", false, false, None),
ConfigKey::new("OPENAI_PROJECT", false, false, None),
],