Skip to content

Commit

Permalink
Extend OpenAI provider to accept Organization and Project
Browse files Browse the repository at this point in the history
Fixes block#1067

Extend the OpenAI provider to accept `openai_organization` and `openai_project` as new configuration keys.

* Add `openai_organization` and `openai_project` to the `Settings` struct in `crates/goose-server/src/configuration.rs`.
* Update the `test_socket_addr_conversion` function in `crates/goose-server/src/configuration.rs` to include `openai_organization` and `openai_project`.
* Add `openai_organization` and `openai_project` to the `OpenAiProvider` struct in `crates/goose/src/providers/openai.rs`.
* Update the `from_env` function in `crates/goose/src/providers/openai.rs` to load `openai_organization` and `openai_project`.
* Modify the `post` function in `crates/goose/src/providers/openai.rs` to include `openai_organization` and `openai_project` in the request headers.
* Add `OPENAI_ORGANIZATION` and `OPENAI_PROJECT` to the configuration keys in `crates/goose/src/providers/openai.rs`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/block/goose/issues/1067?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
jasonkneen committed Feb 6, 2025
1 parent ec0e87f commit a614868
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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

0 comments on commit a614868

Please sign in to comment.