kind_openai/
lib.rs

1//! An opinionated wrapper around the [OpenAI API](https://platform.openai.com/docs/api-reference).
2//! This does not support all endpoints, and is not automatically generated.
3
4#![allow(async_fn_in_trait)]
5
6mod auth;
7pub mod endpoints;
8pub mod error;
9
10pub use auth::{AuthTokenProvider, EnvironmentAuthTokenProvider};
11use endpoints::OpenAIRequestProvider;
12pub use error::{OpenAIError, OpenAIResult};
13pub use kind_openai_schema::*;
14use serde::Deserialize;
15
16/// A handle to OpenAI.
17#[derive(Clone)]
18pub struct OpenAI<Auth> {
19    client: reqwest::Client,
20    auth: Auth,
21}
22
23impl<Auth> OpenAI<Auth>
24where
25    Auth: AuthTokenProvider,
26{
27    /// Creates a new instance of OpenAI with the provided auth.
28    pub fn new(auth: Auth) -> Self {
29        Self {
30            client: reqwest::Client::new(),
31            auth,
32        }
33    }
34
35    /// Sends a request to the OpenAI API.
36    pub async fn req<R: OpenAIRequestProvider>(&self, r: &R) -> OpenAIResult<R::Response> {
37        endpoints::send_request(self, r).await
38    }
39}
40
41/// The token usage of a request.
42#[derive(Deserialize, Clone, Copy, Debug)]
43pub struct Usage {
44    pub prompt_tokens: u32,
45    pub completion_tokens: u32,
46    pub total_tokens: u32,
47}