Skip to content

Commit 557fec7

Browse files
authored
feat: crate features flags (#493)
* cargo.toml with feature flags * examples having correct feature flags * response-types: cargo b -F response-types --no-default-features * webhook-types: cargo b -F webhook-types --no-default-features * audio-types: cargo b -F audio-types --no-default-features * video-types: cargo b -F video-types --no-default-features * image-types: cargo b -F image-types --no-default-features * embedding-types: cargo b -F embedding-types --no-default-features * file-types: cargo b -F file-types --no-default-features * upload-types: cargo b -F upload-types --no-default-features * container-types: cargo b -F container-types --no-default-features * realtime-types: cargo b -F realtime-types --no-default-features * assistant-types: cargo b -F assistant-types --no-default-features * administration-types: cargo b -F administration-types --no-default-features * completion-types and chat-completino-types * grader-types * shared types with feature flags * src/types works with all type feature flags * add byot to realtime apis * feature flags added to client.rs * feature flag for test in config.rs * feature flags in util.rs * feature flag in error.rs * feature flag for api wrappers in impl.rs * remove unused feature flag * updated cargo.toml with feature flags * updated lib.rs for feature flags * github workflow to build all features * add types in feature list * add feature flag docs in readme * updated README * webhook clippy fix: module has the same name as its containing module * clippy: image feature: derive default on ImageInput instead of manually impl * clippy: realtime feature: box variant to reduce enum size * remove tls feature flags for reqwest from the pr checks * move default implementation for InputSource in its own file
1 parent 2c50245 commit 557fec7

File tree

100 files changed

+1147
-610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1147
-610
lines changed

.github/workflows/pr-checks.yml

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
types: [opened, synchronize, reopened]
66

77
jobs:
8-
build:
9-
name: Build Workspace
8+
format:
9+
name: Check Formatting
1010
runs-on: ubuntu-latest
1111

1212
steps:
@@ -17,7 +17,77 @@ jobs:
1717
uses: actions-rs/toolchain@v1
1818
with:
1919
toolchain: stable
20-
components: rustfmt, clippy
20+
components: rustfmt
21+
override: true
22+
23+
- name: Check formatting
24+
run: cargo fmt --all -- --check
25+
26+
build-features:
27+
name: Build Feature - ${{ matrix.feature }}
28+
runs-on: ubuntu-latest
29+
strategy:
30+
fail-fast: false
31+
matrix:
32+
feature:
33+
[
34+
byot,
35+
responses,
36+
webhook,
37+
audio,
38+
video,
39+
image,
40+
embedding,
41+
evals,
42+
finetuning,
43+
grader,
44+
batch,
45+
file,
46+
upload,
47+
model,
48+
moderation,
49+
vectorstore,
50+
chatkit,
51+
container,
52+
realtime,
53+
chat-completion,
54+
assistant,
55+
administration,
56+
completions,
57+
response-types,
58+
webhook-types,
59+
audio-types,
60+
video-types,
61+
image-types,
62+
embedding-types,
63+
eval-types,
64+
finetuning-types,
65+
grader-types,
66+
batch-types,
67+
file-types,
68+
upload-types,
69+
model-types,
70+
moderation-types,
71+
vectorstore-types,
72+
chatkit-types,
73+
container-types,
74+
realtime-types,
75+
chat-completion-types,
76+
assistant-types,
77+
administration-types,
78+
completion-types,
79+
types,
80+
]
81+
82+
steps:
83+
- name: Checkout code
84+
uses: actions/checkout@v4
85+
86+
- name: Install Rust
87+
uses: actions-rs/toolchain@v1
88+
with:
89+
toolchain: stable
90+
components: clippy
2191
override: true
2292

2393
- name: Cache cargo registry
@@ -27,15 +97,15 @@ jobs:
2797
~/.cargo/registry
2898
~/.cargo/git
2999
target
30-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
100+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ matrix.feature }}
31101
restore-keys: |
102+
${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-
32103
${{ runner.os }}-cargo-
33104
34-
- name: Build workspace
35-
run: cargo build --workspace --verbose
36-
37-
- name: Check formatting
38-
run: cargo fmt --all -- --check
105+
- name: Build with feature ${{ matrix.feature }}
106+
env:
107+
RUSTFLAGS: "-D warnings"
108+
run: cargo build --no-default-features --features ${{ matrix.feature }} --verbose
39109

40-
# - name: Run clippy
41-
# run: cargo clippy --workspace -- -D warnings
110+
- name: Run clippy with feature ${{ matrix.feature }}
111+
run: cargo clippy --no-default-features --features ${{ matrix.feature }} -- -D warnings

async-openai/Cargo.toml

Lines changed: 151 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,148 @@ homepage = "https://github.com/64bit/async-openai"
1313
repository = "https://github.com/64bit/async-openai"
1414

1515
[features]
16-
default = ["rustls"]
16+
default = []
1717
# Enable rustls for TLS support
18-
rustls = ["reqwest/rustls-tls-native-roots"]
18+
rustls = ["_api", "dep:reqwest", "reqwest/rustls-tls-native-roots"]
1919
# Enable rustls and webpki-roots
20-
rustls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
20+
rustls-webpki-roots = ["_api", "dep:reqwest", "reqwest/rustls-tls-webpki-roots"]
2121
# Enable native-tls for TLS support
22-
native-tls = ["reqwest/native-tls"]
22+
native-tls = ["_api","dep:reqwest", "reqwest/native-tls"]
2323
# Remove dependency on OpenSSL
24-
native-tls-vendored = ["reqwest/native-tls-vendored"]
25-
realtime = ["dep:tokio-tungstenite"]
24+
native-tls-vendored = ["_api", "dep:reqwest", "reqwest/native-tls-vendored"]
2625
# Bring your own types
27-
byot = []
28-
webhook = ["dep:hmac", "dep:sha2", "dep:hex"]
26+
byot = ["dep:async-openai-macros"]
27+
28+
# API feature flags - these enable both the API wrapper and types
29+
responses = ["response-types", "_api"]
30+
webhook = ["webhook-types", "dep:base64", "dep:thiserror", "dep:hmac", "dep:sha2", "dep:hex"]
31+
# start - Platform APIs
32+
audio = ["audio-types", "_api"]
33+
video = ["video-types", "_api"]
34+
image = ["image-types", "_api"]
35+
embedding = ["embedding-types", "_api"]
36+
evals = ["eval-types", "_api"]
37+
finetuning = ["finetuning-types", "_api"]
38+
grader = ["grader-types"]
39+
batch = ["batch-types", "_api"]
40+
file = ["file-types", "_api"]
41+
upload = ["upload-types", "_api"]
42+
model = ["model-types", "_api"]
43+
moderation = ["moderation-types", "_api"]
44+
# end - Platform APIs
45+
vectorstore = ["vectorstore-types", "_api"]
46+
chatkit = ["chatkit-types", "_api"]
47+
container = ["container-types", "_api"]
48+
realtime = ["realtime-types", "_api", "dep:tokio-tungstenite"]
49+
chat-completion = ["chat-completion-types", "_api"]
50+
assistant = ["assistant-types", "_api", ]
51+
administration = ["administration-types", "_api"]
52+
completions = ["completion-types", "_api"]
53+
54+
# Type feature flags - these enable only the types
55+
response-types = ["dep:derive_builder"]
56+
webhook-types = ["dep:derive_builder"]
57+
audio-types = ["dep:derive_builder", "dep:bytes"]
58+
video-types = ["dep:derive_builder", "dep:bytes"]
59+
image-types = ["dep:derive_builder", "dep:bytes"]
60+
embedding-types = ["dep:derive_builder"]
61+
eval-types = ["dep:derive_builder", "chat-completion-types", "response-types", "grader-types"]
62+
finetuning-types = ["dep:derive_builder", "grader-types"]
63+
grader-types = ["dep:derive_builder", "eval-types"]
64+
batch-types = ["dep:derive_builder"]
65+
file-types = ["dep:derive_builder", "dep:bytes"]
66+
upload-types = ["dep:derive_builder", "dep:bytes", "file-types"]
67+
model-types = ["dep:derive_builder"]
68+
moderation-types = ["dep:derive_builder"]
69+
vectorstore-types = ["dep:derive_builder"]
70+
chatkit-types = ["dep:derive_builder"]
71+
container-types = ["dep:derive_builder", "dep:bytes"]
72+
realtime-types = ["dep:derive_builder", "dep:bytes", "response-types"]
73+
chat-completion-types = ["dep:derive_builder", "dep:bytes"]
74+
assistant-types = ["dep:derive_builder"]
75+
administration-types = ["dep:derive_builder"]
76+
completion-types = ["dep:derive_builder", "chat-completion-types" ]
77+
78+
# Enable all types
79+
types = [
80+
"response-types",
81+
"webhook-types",
82+
"audio-types",
83+
"video-types",
84+
"image-types",
85+
"embedding-types",
86+
"eval-types",
87+
"finetuning-types",
88+
"grader-types",
89+
"batch-types",
90+
"file-types",
91+
"upload-types",
92+
"model-types",
93+
"moderation-types",
94+
"vectorstore-types",
95+
"chatkit-types",
96+
"container-types",
97+
"realtime-types",
98+
"chat-completion-types",
99+
"assistant-types",
100+
"administration-types",
101+
"completion-types",
102+
]
103+
104+
# Internal feature to enable API dependencies
105+
_api = [
106+
"dep:async-openai-macros",
107+
"dep:backoff",
108+
"dep:base64",
109+
"dep:bytes",
110+
"dep:futures",
111+
"dep:rand",
112+
"dep:reqwest",
113+
"dep:reqwest-eventsource",
114+
"dep:thiserror",
115+
"dep:tokio",
116+
"dep:tokio-stream",
117+
"dep:tokio-util",
118+
"dep:tracing",
119+
"dep:secrecy",
120+
"dep:eventsource-stream",
121+
"dep:serde_urlencoded",
122+
"dep:url",
123+
]
124+
29125

30126
[dependencies]
31-
async-openai-macros = { path = "../async-openai-macros", version = "0.1.0" }
32-
backoff = { version = "0.4.0", features = ["tokio"] }
33-
base64 = "0.22.1"
34-
futures = "0.3.31"
35-
rand = "0.9.0"
127+
# Core dependencies - always needed for types
128+
serde = { version = "1.0.217", features = ["derive", "rc"] }
129+
serde_json = "1.0.135"
130+
derive_builder = { version = "0.20.2", optional = true }
131+
bytes = { version = "1.9.0", optional = true }
132+
133+
# API dependencies - only needed when API features are enabled
134+
# We use a feature gate to enable these when any API feature is enabled
135+
async-openai-macros = { path = "../async-openai-macros", version = "0.1.0", optional = true }
136+
backoff = { version = "0.4.0", features = ["tokio"], optional = true }
137+
base64 = { version = "0.22.1", optional = true }
138+
futures = { version = "0.3.31", optional = true }
139+
rand = { version = "0.9.0", optional = true }
36140
reqwest = { version = "0.12.12", features = [
37141
"json",
38142
"stream",
39143
"multipart",
40-
], default-features = false }
41-
reqwest-eventsource = "0.6.0"
42-
serde = { version = "1.0.217", features = ["derive", "rc"] }
43-
serde_json = "1.0.135"
44-
thiserror = "2.0.11"
45-
tokio = { version = "1.43.0", features = ["fs", "macros"] }
46-
tokio-stream = "0.1.17"
47-
tokio-util = { version = "0.7.13", features = ["codec", "io-util"] }
48-
tracing = "0.1.41"
49-
derive_builder = "0.20.2"
50-
secrecy = { version = "0.10.3", features = ["serde"] }
51-
bytes = "1.9.0"
52-
eventsource-stream = "0.2.3"
53-
serde_urlencoded = "0.7.1"
54-
url = "2.5"
144+
], default-features = false, optional = true }
145+
reqwest-eventsource = { version = "0.6.0", optional = true }
146+
thiserror = { version = "2.0.11", optional = true }
147+
tokio = { version = "1.43.0", features = ["fs", "macros"], optional = true }
148+
tokio-stream = { version = "0.1.17", optional = true }
149+
tokio-util = { version = "0.7.13", features = ["codec", "io-util"], optional = true }
150+
tracing = { version = "0.1.41", optional = true }
151+
secrecy = { version = "0.10.3", features = ["serde"], optional = true }
152+
eventsource-stream = { version = "0.2.3", optional = true }
153+
serde_urlencoded = { version = "0.7.1", optional = true }
154+
url = { version = "2.5", optional = true }
155+
# For Realtime websocket
55156
tokio-tungstenite = { version = "0.26.1", optional = true, default-features = false }
157+
# For Webhook signature verification
56158
hmac = { version = "0.12", optional = true, default-features = false}
57159
sha2 = { version = "0.10", optional = true, default-features = false }
58160
hex = { version = "0.4", optional = true, default-features = false }
@@ -63,7 +165,27 @@ serde_json = "1.0"
63165

64166
[[test]]
65167
name = "bring_your_own_type"
66-
required-features = ["byot"]
168+
required-features = ["byot", "file", "assistant", "model", "moderation", "image", "chat-completion", "completions", "audio", "embedding", "finetuning", "batch", "administration", "upload", "vectorstore", "responses", "chatkit", "container", "evals", "video"]
169+
170+
[[test]]
171+
name = "boxed_future"
172+
required-features = ["completions", "chat-completion-types"]
173+
174+
[[test]]
175+
name = "chat_completion"
176+
required-features = ["chat-completion-types"]
177+
178+
[[test]]
179+
name = "embeddings"
180+
required-features = ["embedding-types", "chat-completion-types"]
181+
182+
[[test]]
183+
name = "ser_de"
184+
required-features = ["chat-completion-types"]
185+
186+
[[test]]
187+
name = "whisper"
188+
required-features = ["audio", "file-types"]
67189

68190
[package.metadata.docs.rs]
69191
all-features = true

0 commit comments

Comments
 (0)