Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ unused_qualifications = "warn"
[features]
default = []
bedrock = ["polaris_internal/bedrock"]
openai = ["polaris_internal/openai"]

[dependencies]
polaris_internal = { path = "crates/polaris_internal", version = "0.0.1" }
Expand Down
1 change: 1 addition & 0 deletions crates/polaris_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ keywords = []

[features]
default = []
openai = ["polaris_model_providers/openai"]
bedrock = ["polaris_model_providers/bedrock"]

[dependencies]
Expand Down
8 changes: 8 additions & 0 deletions crates/polaris_model_providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true
[features]
default = ["anthropic"]
anthropic = []
openai = ["dep:async-openai"]
bedrock = [
"dep:aws-sdk-bedrockruntime",
"dep:aws-config",
Expand All @@ -27,6 +28,13 @@ serde_json = "1.0"
reqwest = { version = "0.13.1", features = ["json"] }
tracing = "0.1"

# OpenAI dependencies
async-openai = { version = "0.33", optional = true, default-features = false, features = [
"rustls",
"responses",
"response-types",
] }

# AWS Bedrock dependencies
aws-sdk-bedrockruntime = { version = "1.124.0", optional = true }
aws-config = { version = "1.5", features = [
Expand Down
26 changes: 24 additions & 2 deletions crates/polaris_model_providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! | Provider | Feature Flag | Description |
//! |----------|--------------|-------------|
//! | Anthropic | `anthropic` (default) | Direct Anthropic API access |
//! | `OpenAI` | `openai` | `OpenAI` Responses API |
//! | AWS Bedrock | `bedrock` | AWS Bedrock Converse API |
//!
//! # Feature Flags
Expand All @@ -20,8 +21,11 @@
//! # Enable only Bedrock
//! polaris_model_providers = { path = "../polaris_model_providers", default-features = false, features = ["bedrock"] }
//!
//! # Enable both providers
//! polaris_model_providers = { path = "../polaris_model_providers", features = ["bedrock"] }
//! # Enable OpenAI
//! polaris_model_providers = { path = "../polaris_model_providers", default-features = false, features = ["openai"] }
//!
//! # Enable multiple providers
//! polaris_model_providers = { path = "../polaris_model_providers", features = ["openai", "bedrock"] }
//! ```
//!
//! # Usage
Expand Down Expand Up @@ -49,6 +53,18 @@
//! server.add_plugins(ModelsPlugin);
//! server.add_plugins(BedrockPlugin::from_env());
//! ```
//!
//! For `OpenAI`, provide an API key via environment variable:
//!
//! ```ignore
//! use polaris_model_providers::OpenAiPlugin;
//! use polaris_models::ModelsPlugin;
//! use polaris_system::server::Server;
//!
//! let mut server = Server::new();
//! server.add_plugins(ModelsPlugin);
//! server.add_plugins(OpenAiPlugin::from_env("OPENAI_API_KEY"));
//! ```

mod schema;

Expand All @@ -58,6 +74,12 @@ pub mod anthropic;
#[cfg(feature = "anthropic")]
pub use anthropic::AnthropicPlugin;

#[cfg(feature = "openai")]
pub mod openai;

#[cfg(feature = "openai")]
pub use openai::OpenAiPlugin;

#[cfg(feature = "bedrock")]
pub mod bedrock;

Expand Down
9 changes: 9 additions & 0 deletions crates/polaris_model_providers/src/openai/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! `OpenAI` provider backend.
//!
//! Uses the `OpenAI` Responses API.

mod plugin;
mod provider;

pub use plugin::OpenAiPlugin;
pub use provider::OpenAiProvider;
50 changes: 50 additions & 0 deletions crates/polaris_model_providers/src/openai/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! `OpenAI` provider plugin.

use super::provider::OpenAiProvider;
use polaris_models::{ModelRegistry, ModelsPlugin};
use polaris_system::plugin::{Plugin, PluginId, Version};
use polaris_system::server::Server;
use std::sync::Arc;

/// Plugin providing support for `OpenAI` models via the Responses API.
///
/// ```ignore
/// server.add_plugins(OpenAiPlugin::from_env("OPENAI_API_KEY"));
/// ```
pub struct OpenAiPlugin {
api_key: String,
}

impl OpenAiPlugin {
/// Creates a plugin that reads the API key from the specified environment variable.
///
/// # Panics
///
/// Panics if the environment variable is not set.
#[must_use]
pub fn from_env(env_var: &str) -> Self {
let api_key = std::env::var(env_var).unwrap_or_else(|_| {
panic!("Environment variable {env_var} for OpenAiPlugin not set. Please set it to your OpenAI API key.");
});
Self { api_key }
}
}

impl Plugin for OpenAiPlugin {
const ID: &'static str = "polaris::provider::openai";
const VERSION: Version = Version::new(0, 0, 1);

fn dependencies(&self) -> Vec<PluginId> {
vec![PluginId::of::<ModelsPlugin>()]
}

fn build(&self, server: &mut Server) {
let provider = OpenAiProvider::new(self.api_key.clone());

let Some(mut registry) = server.get_resource_mut::<ModelRegistry>() else {
panic!("ModelRegistry not found. Make sure to add ModelsPlugin before OpenAiPlugin.");
};

registry.register_llm_provider("openai", Arc::new(provider));
}
}
Loading
Loading