Skip to content

Commit

Permalink
feat: OpenAI resource (#1794)
Browse files Browse the repository at this point in the history
* Added OpenAI resource

* Update scripts/patches.toml

Co-authored-by: jonaro00 <[email protected]>

* Update resources/openai/src/lib.rs

Co-authored-by: jonaro00 <[email protected]>

* Update resources/openai/src/lib.rs

Co-authored-by: jonaro00 <[email protected]>

* Update resources/openai/Cargo.toml

Co-authored-by: jonaro00 <[email protected]>

* feat: update to 0.47.0

* fmt

---------

Co-authored-by: jonaro00 <[email protected]>
  • Loading branch information
christos-h and jonaro00 authored Jul 22, 2024
1 parent bcd78d2 commit e2e178c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,12 @@ workflows:
parameters:
path:
- resources/aws-rds
- resources/openai
- resources/opendal
- resources/persist
- resources/qdrant
- resources/shared-db
- resources/turso
- resources/opendal
- services/shuttle-actix-web
- services/shuttle-axum
- services/shuttle-poem
Expand Down Expand Up @@ -883,11 +884,12 @@ workflows:
parameters:
path:
- resources/aws-rds
- resources/openai
- resources/opendal
- resources/persist
- resources/qdrant
- resources/shared-db
- resources/turso
- resources/opendal
name: publish-<< matrix.path >>
requires:
- publish-service
Expand Down
15 changes: 15 additions & 0 deletions resources/openai/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "shuttle-openai"
version = "0.47.0"
edition = "2021"
license = "Apache-2.0"
description = "Shuttle plugin for connecting to OpenAI"
repository = "https://github.com/shuttle-hq/shuttle"
keywords = ["shuttle-service", "openai"]

[dependencies]
async-openai = "0.23.0"
async-trait = "0.1.56"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1"
shuttle-service = { path = "../../service", version = "0.47.0" }
78 changes: 78 additions & 0 deletions resources/openai/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use async_openai::config::OpenAIConfig;
use async_openai::Client;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use shuttle_service::{CustomError, Error, IntoResource, ResourceFactory, ResourceInputBuilder};

pub use async_openai;

#[derive(Default, Serialize)]
pub struct OpenAI {
api_base: Option<String>,
api_key: Option<String>,
org_id: Option<String>,
project_id: Option<String>,
}

impl OpenAI {
pub fn api_base(mut self, api_base: &str) -> Self {
self.api_base = Some(api_base.to_string());
self
}
pub fn api_key(mut self, api_key: &str) -> Self {
self.api_key = Some(api_key.to_string());
self
}
pub fn org_id(mut self, org_id: &str) -> Self {
self.org_id = Some(org_id.to_string());
self
}
pub fn project_id(mut self, project_id: &str) -> Self {
self.project_id = Some(project_id.to_string());
self
}
}

#[derive(Serialize, Deserialize)]
pub struct Config {
api_base: Option<String>,
api_key: String,
org_id: Option<String>,
project_id: Option<String>,
}

#[async_trait]
impl ResourceInputBuilder for OpenAI {
type Input = Config;
type Output = Config;

async fn build(self, _factory: &ResourceFactory) -> Result<Self::Input, Error> {
let api_key = self
.api_key
.ok_or(Error::Custom(CustomError::msg("Open AI API key required")))?;
let config = Config {
api_base: self.api_base,
api_key,
org_id: self.org_id,
project_id: self.project_id,
};
Ok(config)
}
}

#[async_trait]
impl IntoResource<Client<OpenAIConfig>> for Config {
async fn into_resource(self) -> Result<Client<OpenAIConfig>, Error> {
let mut openai_config = OpenAIConfig::new().with_api_key(self.api_key);
if let Some(api_base) = self.api_base {
openai_config = openai_config.with_api_base(api_base)
}
if let Some(org_id) = self.org_id {
openai_config = openai_config.with_org_id(org_id)
}
if let Some(project_id) = self.project_id {
openai_config = openai_config.with_project_id(project_id)
}
Ok(Client::with_config(openai_config))
}
}
1 change: 1 addition & 0 deletions scripts/patches.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ shuttle-runtime = { path = "BASE/runtime" }
shuttle-service = { path = "BASE/service" }

shuttle-aws-rds = { path = "BASE/resources/aws-rds" }
shuttle-openai = { path = "BASE/resources/openai" }
shuttle-opendal = { path = "BASE/resources/opendal" }
shuttle-persist = { path = "BASE/resources/persist" }
shuttle-qdrant = { path = "BASE/resources/qdrant" }
Expand Down

0 comments on commit e2e178c

Please sign in to comment.