pub struct Client { /* private fields */ }Expand description
Asynchronous API client for querying OpenAI and OpenRouter endpoints.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(api_key: impl Into<String>) -> Result<Self, ClientError>
pub fn new(api_key: impl Into<String>) -> Result<Self, ClientError>
Create a new client targeting OpenAI with an API key.
Sourcepub fn openai(api_key: impl Into<String>) -> Result<Self, ClientError>
pub fn openai(api_key: impl Into<String>) -> Result<Self, ClientError>
Create a client targeting the official OpenAI API.
Sourcepub fn openrouter(api_key: impl Into<String>) -> Result<Self, ClientError>
pub fn openrouter(api_key: impl Into<String>) -> Result<Self, ClientError>
Create a client targeting the OpenRouter API.
Sourcepub fn openrouter_with_attribution(
api_key: impl Into<String>,
app_url: impl Into<String>,
app_name: impl Into<String>,
) -> Result<Self, ClientError>
pub fn openrouter_with_attribution( api_key: impl Into<String>, app_url: impl Into<String>, app_name: impl Into<String>, ) -> Result<Self, ClientError>
Create a client targeting OpenRouter with application attribution metadata.
Sourcepub fn custom(
base_url: impl Into<String>,
api_key: Option<String>,
) -> Result<Self, ClientError>
pub fn custom( base_url: impl Into<String>, api_key: Option<String>, ) -> Result<Self, ClientError>
Create a client targeting a custom OpenAI-compatible server.
Sourcepub fn from_env() -> Result<Self, ClientError>
pub fn from_env() -> Result<Self, ClientError>
Automatically configure a client from available environment variables.
Checks OPENROUTER_API_KEY first; if present, connects to OpenRouter.
Then checks OPENAI_BASE_URL; if present, connects to that custom endpoint
with optional OPENAI_API_KEY.
Otherwise checks OPENAI_API_KEY to connect to OpenAI.
Sourcepub fn builder(provider: Provider) -> ClientBuilder
pub fn builder(provider: Provider) -> ClientBuilder
Initialize a builder with a specific provider.
Sourcepub async fn chat(
&self,
request: ChatCompletionRequest,
) -> Result<ChatCompletionResponse, ClientError>
pub async fn chat( &self, request: ChatCompletionRequest, ) -> Result<ChatCompletionResponse, ClientError>
Sends a non-streaming chat completion request to /chat/completions.
Sourcepub async fn chat_stream(
&self,
request: ChatCompletionRequest,
) -> Result<ChatCompletionStream<impl Stream<Item = Result<Bytes, Error>> + Unpin>, ClientError>
pub async fn chat_stream( &self, request: ChatCompletionRequest, ) -> Result<ChatCompletionStream<impl Stream<Item = Result<Bytes, Error>> + Unpin>, ClientError>
Sends a streaming chat completion request to /chat/completions, returning an SSE chunk stream.
The request handshake awaits HTTP response headers. Once headers arrive and status is
validated, the connection remains open for streaming tokens. Total request timeout is omitted
to support long generations; connection timeout is governed by ClientBuilder::connect_timeout.
§Stalls and Inactivity Timeouts
Because total request timeouts are omitted during streaming to permit long generations,
a network disruption or hung server connection mid-stream may not terminate automatically.
Callers in production environments should wrap stream consumption in an inactivity or idle
timeout (for example, using tokio::time::timeout between successive chunk yields) so that
mid-stream stalls can be detected and recovered from.
Sourcepub async fn embeddings(
&self,
request: EmbeddingRequest,
) -> Result<EmbeddingResponse, ClientError>
pub async fn embeddings( &self, request: EmbeddingRequest, ) -> Result<EmbeddingResponse, ClientError>
Generates vector embeddings for input text via /embeddings.
Sourcepub async fn models(&self) -> Result<ListModelsResponse, ClientError>
pub async fn models(&self) -> Result<ListModelsResponse, ClientError>
Lists models available from the provider via /models.