UsageBuilder

Struct UsageBuilder 

Source
pub struct UsageBuilder { /* private fields */ }
Expand description

Builder for querying usage data from the OpenAI API.

§Examples

use openai_ergonomic::builders::usage::{UsageBuilder, BucketWidth};

let builder = UsageBuilder::new(1704067200, None) // Start time (Unix timestamp)
    .bucket_width(BucketWidth::Day)
    .limit(100);

Implementations§

Source§

impl UsageBuilder

Source

pub fn new(start_time: i32, end_time: Option<i32>) -> Self

Create a new usage builder with the specified start time.

§Arguments
  • start_time - Unix timestamp (in seconds) for the start of the query range
  • end_time - Optional Unix timestamp (in seconds) for the end of the query range
Examples found in repository?
examples/usage.rs (line 73)
72async fn basic_usage_query(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
73    let builder = UsageBuilder::new(start_time, Some(end_time));
74
75    let usage = client.usage().completions(builder).await?;
76
77    println!("Completions usage:");
78    println!("  Data points: {}", usage.data.len());
79
80    if usage.has_more {
81        println!("  Has more: yes");
82    }
83
84    Ok(())
85}
86
87async fn usage_with_aggregation(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
88    let builder = UsageBuilder::new(start_time, Some(end_time))
89        .bucket_width(BucketWidth::Day)
90        .limit(10);
91
92    let usage = client.usage().completions(builder).await?;
93
94    println!("Daily aggregated completions usage:");
95    println!("  Bucket width: 1 day");
96    println!("  Data points: {}", usage.data.len());
97
98    Ok(())
99}
100
101async fn usage_by_model(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
102    let builder = UsageBuilder::new(start_time, Some(end_time))
103        .model("gpt-4")
104        .limit(100);
105
106    let usage = client.usage().completions(builder).await?;
107
108    println!("Completions usage for gpt-4:");
109    println!("  Data points: {}", usage.data.len());
110
111    Ok(())
112}
113
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
127
128async fn cost_data(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
129    let builder = UsageBuilder::new(start_time, Some(end_time))
130        .bucket_width(BucketWidth::Day)
131        .limit(10);
132
133    let costs = client.usage().costs(builder).await?;
134
135    println!("Cost data:");
136    println!("  Data points: {}", costs.data.len());
137
138    Ok(())
139}
140
141async fn audio_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
142    let builder = UsageBuilder::new(start_time, Some(end_time)).limit(10);
143
144    // Audio speeches (text-to-speech)
145    let speeches = client.usage().audio_speeches(builder.clone()).await?;
146    println!("Audio speeches usage: {} data points", speeches.data.len());
147
148    // Audio transcriptions
149    let transcriptions = client.usage().audio_transcriptions(builder).await?;
150    println!(
151        "Audio transcriptions usage: {} data points",
152        transcriptions.data.len()
153    );
154
155    Ok(())
156}
157
158async fn image_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
159    let builder = UsageBuilder::new(start_time, Some(end_time))
160        .bucket_width(BucketWidth::Day)
161        .limit(10);
162
163    let usage = client.usage().images(builder).await?;
164
165    println!("Image generation usage:");
166    println!("  Data points: {}", usage.data.len());
167
168    Ok(())
169}
170
171async fn embeddings_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
172    let builder = UsageBuilder::new(start_time, Some(end_time))
173        .model("text-embedding-3-small")
174        .limit(100);
175
176    let usage = client.usage().embeddings(builder).await?;
177
178    println!("Embeddings usage for text-embedding-3-small:");
179    println!("  Data points: {}", usage.data.len());
180
181    Ok(())
182}
Source

pub fn bucket_width(self, width: BucketWidth) -> Self

Set the bucket width for aggregation.

Examples found in repository?
examples/usage.rs (line 89)
87async fn usage_with_aggregation(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
88    let builder = UsageBuilder::new(start_time, Some(end_time))
89        .bucket_width(BucketWidth::Day)
90        .limit(10);
91
92    let usage = client.usage().completions(builder).await?;
93
94    println!("Daily aggregated completions usage:");
95    println!("  Bucket width: 1 day");
96    println!("  Data points: {}", usage.data.len());
97
98    Ok(())
99}
100
101async fn usage_by_model(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
102    let builder = UsageBuilder::new(start_time, Some(end_time))
103        .model("gpt-4")
104        .limit(100);
105
106    let usage = client.usage().completions(builder).await?;
107
108    println!("Completions usage for gpt-4:");
109    println!("  Data points: {}", usage.data.len());
110
111    Ok(())
112}
113
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
127
128async fn cost_data(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
129    let builder = UsageBuilder::new(start_time, Some(end_time))
130        .bucket_width(BucketWidth::Day)
131        .limit(10);
132
133    let costs = client.usage().costs(builder).await?;
134
135    println!("Cost data:");
136    println!("  Data points: {}", costs.data.len());
137
138    Ok(())
139}
140
141async fn audio_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
142    let builder = UsageBuilder::new(start_time, Some(end_time)).limit(10);
143
144    // Audio speeches (text-to-speech)
145    let speeches = client.usage().audio_speeches(builder.clone()).await?;
146    println!("Audio speeches usage: {} data points", speeches.data.len());
147
148    // Audio transcriptions
149    let transcriptions = client.usage().audio_transcriptions(builder).await?;
150    println!(
151        "Audio transcriptions usage: {} data points",
152        transcriptions.data.len()
153    );
154
155    Ok(())
156}
157
158async fn image_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
159    let builder = UsageBuilder::new(start_time, Some(end_time))
160        .bucket_width(BucketWidth::Day)
161        .limit(10);
162
163    let usage = client.usage().images(builder).await?;
164
165    println!("Image generation usage:");
166    println!("  Data points: {}", usage.data.len());
167
168    Ok(())
169}
Source

pub fn project_id(self, id: impl Into<String>) -> Self

Filter by a single project ID.

Source

pub fn project_ids<I, S>(self, ids: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Filter by multiple project IDs.

Source

pub fn user_id(self, id: impl Into<String>) -> Self

Filter by a single user ID.

Source

pub fn user_ids<I, S>(self, ids: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Filter by multiple user IDs.

Source

pub fn api_key_id(self, id: impl Into<String>) -> Self

Filter by a single API key ID.

Source

pub fn api_key_ids<I, S>(self, ids: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Filter by multiple API key IDs.

Source

pub fn model(self, model: impl Into<String>) -> Self

Filter by a single model.

Examples found in repository?
examples/usage.rs (line 103)
101async fn usage_by_model(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
102    let builder = UsageBuilder::new(start_time, Some(end_time))
103        .model("gpt-4")
104        .limit(100);
105
106    let usage = client.usage().completions(builder).await?;
107
108    println!("Completions usage for gpt-4:");
109    println!("  Data points: {}", usage.data.len());
110
111    Ok(())
112}
113
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
127
128async fn cost_data(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
129    let builder = UsageBuilder::new(start_time, Some(end_time))
130        .bucket_width(BucketWidth::Day)
131        .limit(10);
132
133    let costs = client.usage().costs(builder).await?;
134
135    println!("Cost data:");
136    println!("  Data points: {}", costs.data.len());
137
138    Ok(())
139}
140
141async fn audio_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
142    let builder = UsageBuilder::new(start_time, Some(end_time)).limit(10);
143
144    // Audio speeches (text-to-speech)
145    let speeches = client.usage().audio_speeches(builder.clone()).await?;
146    println!("Audio speeches usage: {} data points", speeches.data.len());
147
148    // Audio transcriptions
149    let transcriptions = client.usage().audio_transcriptions(builder).await?;
150    println!(
151        "Audio transcriptions usage: {} data points",
152        transcriptions.data.len()
153    );
154
155    Ok(())
156}
157
158async fn image_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
159    let builder = UsageBuilder::new(start_time, Some(end_time))
160        .bucket_width(BucketWidth::Day)
161        .limit(10);
162
163    let usage = client.usage().images(builder).await?;
164
165    println!("Image generation usage:");
166    println!("  Data points: {}", usage.data.len());
167
168    Ok(())
169}
170
171async fn embeddings_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
172    let builder = UsageBuilder::new(start_time, Some(end_time))
173        .model("text-embedding-3-small")
174        .limit(100);
175
176    let usage = client.usage().embeddings(builder).await?;
177
178    println!("Embeddings usage for text-embedding-3-small:");
179    println!("  Data points: {}", usage.data.len());
180
181    Ok(())
182}
Source

pub fn models<I, S>(self, models: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Filter by multiple models.

Source

pub fn group_by(self, field: GroupBy) -> Self

Add a group by field.

Examples found in repository?
examples/usage.rs (line 116)
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
Source

pub fn group_by_fields<I>(self, fields: I) -> Self
where I: IntoIterator<Item = GroupBy>,

Add multiple group by fields.

Source

pub fn limit(self, limit: i32) -> Self

Set the maximum number of results to return.

Examples found in repository?
examples/usage.rs (line 90)
87async fn usage_with_aggregation(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
88    let builder = UsageBuilder::new(start_time, Some(end_time))
89        .bucket_width(BucketWidth::Day)
90        .limit(10);
91
92    let usage = client.usage().completions(builder).await?;
93
94    println!("Daily aggregated completions usage:");
95    println!("  Bucket width: 1 day");
96    println!("  Data points: {}", usage.data.len());
97
98    Ok(())
99}
100
101async fn usage_by_model(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
102    let builder = UsageBuilder::new(start_time, Some(end_time))
103        .model("gpt-4")
104        .limit(100);
105
106    let usage = client.usage().completions(builder).await?;
107
108    println!("Completions usage for gpt-4:");
109    println!("  Data points: {}", usage.data.len());
110
111    Ok(())
112}
113
114async fn usage_grouped_by_project(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
115    let builder = UsageBuilder::new(start_time, Some(end_time))
116        .group_by(GroupBy::ProjectId)
117        .group_by(GroupBy::Model)
118        .limit(50);
119
120    let usage = client.usage().completions(builder).await?;
121
122    println!("Completions usage grouped by project and model:");
123    println!("  Data points: {}", usage.data.len());
124
125    Ok(())
126}
127
128async fn cost_data(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
129    let builder = UsageBuilder::new(start_time, Some(end_time))
130        .bucket_width(BucketWidth::Day)
131        .limit(10);
132
133    let costs = client.usage().costs(builder).await?;
134
135    println!("Cost data:");
136    println!("  Data points: {}", costs.data.len());
137
138    Ok(())
139}
140
141async fn audio_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
142    let builder = UsageBuilder::new(start_time, Some(end_time)).limit(10);
143
144    // Audio speeches (text-to-speech)
145    let speeches = client.usage().audio_speeches(builder.clone()).await?;
146    println!("Audio speeches usage: {} data points", speeches.data.len());
147
148    // Audio transcriptions
149    let transcriptions = client.usage().audio_transcriptions(builder).await?;
150    println!(
151        "Audio transcriptions usage: {} data points",
152        transcriptions.data.len()
153    );
154
155    Ok(())
156}
157
158async fn image_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
159    let builder = UsageBuilder::new(start_time, Some(end_time))
160        .bucket_width(BucketWidth::Day)
161        .limit(10);
162
163    let usage = client.usage().images(builder).await?;
164
165    println!("Image generation usage:");
166    println!("  Data points: {}", usage.data.len());
167
168    Ok(())
169}
170
171async fn embeddings_usage(client: &Client, start_time: i32, end_time: i32) -> Result<()> {
172    let builder = UsageBuilder::new(start_time, Some(end_time))
173        .model("text-embedding-3-small")
174        .limit(100);
175
176    let usage = client.usage().embeddings(builder).await?;
177
178    println!("Embeddings usage for text-embedding-3-small:");
179    println!("  Data points: {}", usage.data.len());
180
181    Ok(())
182}
Source

pub fn page(self, page: impl Into<String>) -> Self

Set the pagination cursor.

Source

pub fn start_time(&self) -> i32

Get the start time.

Source

pub fn end_time(&self) -> Option<i32>

Get the end time.

Source

pub fn bucket_width_ref(&self) -> Option<BucketWidth>

Get the bucket width.

Source

pub fn project_ids_ref(&self) -> &[String]

Get the project IDs.

Source

pub fn user_ids_ref(&self) -> &[String]

Get the user IDs.

Source

pub fn api_key_ids_ref(&self) -> &[String]

Get the API key IDs.

Source

pub fn models_ref(&self) -> &[String]

Get the models.

Source

pub fn group_by_ref(&self) -> &[GroupBy]

Get the group by fields.

Source

pub fn limit_ref(&self) -> Option<i32>

Get the limit.

Source

pub fn page_ref(&self) -> Option<&str>

Get the page cursor.

Source

pub fn project_ids_option(&self) -> Option<Vec<String>>

Convert project IDs to Option<Vec<String>>.

Source

pub fn user_ids_option(&self) -> Option<Vec<String>>

Convert user IDs to Option<Vec<String>>.

Source

pub fn api_key_ids_option(&self) -> Option<Vec<String>>

Convert API key IDs to Option<Vec<String>>.

Source

pub fn models_option(&self) -> Option<Vec<String>>

Convert models to Option<Vec<String>>.

Source

pub fn group_by_option(&self) -> Option<Vec<String>>

Convert group by fields to Option<Vec<String>>.

Source

pub fn bucket_width_str(&self) -> Option<&str>

Get bucket width as Option<&str>.

Trait Implementations§

Source§

impl Clone for UsageBuilder

Source§

fn clone(&self) -> UsageBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UsageBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,