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

Extend OpenAI provider to accept Organization and Project #1113

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions crates/goose-server/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Settings {
pub host: String,
#[serde(default = "default_port")]
pub port: u16,
pub openai_organization: Option<String>, // Pbbfe
pub openai_project: Option<String>, // Pbbfe
}

impl Settings {
Expand Down Expand Up @@ -83,6 +85,8 @@ mod tests {
let server_settings = Settings {
host: "127.0.0.1".to_string(),
port: 3000,
openai_organization: None,
openai_project: None,
};
let addr = server_settings.socket_addr();
assert_eq!(addr.to_string(), "127.0.0.1:3000");
Expand Down
24 changes: 20 additions & 4 deletions crates/goose/src/providers/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct OpenAiProvider {
host: String,
api_key: String,
model: ModelConfig,
openai_organization: Option<String>,
openai_project: Option<String>,
}

impl Default for OpenAiProvider {
Expand All @@ -46,6 +48,8 @@ impl OpenAiProvider {
let host: String = config
.get("OPENAI_HOST")
.unwrap_or_else(|_| "https://api.openai.com".to_string());
let openai_organization: Option<String> = config.get("OPENAI_ORGANIZATION").ok();
let openai_project: Option<String> = config.get("OPENAI_PROJECT").ok();
let client = Client::builder()
.timeout(Duration::from_secs(600))
.build()?;
Expand All @@ -55,6 +59,8 @@ impl OpenAiProvider {
host,
api_key,
model,
openai_organization,
openai_project,
})
}

Expand All @@ -65,10 +71,18 @@ impl OpenAiProvider {
ProviderError::RequestFailed(format!("Failed to construct endpoint URL: {e}"))
})?;

let response = self
.client
.post(url)
.header("Authorization", format!("Bearer {}", self.api_key))
let mut request = self.client.post(url)
.header("Authorization", format!("Bearer {}", self.api_key));

if let Some(ref org) = self.openai_organization {
request = request.header("OpenAI-Organization", org);
}

if let Some(ref project) = self.openai_project {
request = request.header("OpenAI-Project", project);
}

let response = request
.json(&payload)
.send()
.await?;
Expand All @@ -93,6 +107,8 @@ impl Provider for OpenAiProvider {
vec![
ConfigKey::new("OPENAI_API_KEY", true, true, None),
ConfigKey::new("OPENAI_HOST", false, false, Some("https://api.openai.com")),
ConfigKey::new("OPENAI_ORGANIZATION", false, false, None),
ConfigKey::new("OPENAI_PROJECT", false, false, None),
],
)
}
Expand Down