MistralClient.Client (mistralex_ai v0.1.0)
View SourceHTTP client for the Mistral AI API.
This module provides the core HTTP functionality for making requests to the Mistral API, including authentication, retry logic, error handling, and response parsing.
Features
- Automatic authentication with API key
- Exponential backoff retry logic
- Comprehensive error handling
- Request/response logging
- Rate limit handling
- Timeout management
Usage
# Create a client with default configuration
client = MistralClient.Client.new()
# Create a client with custom configuration
client = MistralClient.Client.new(api_key: "custom-key", timeout: 60_000)
# Make a request
{:ok, response} = MistralClient.Client.request(client, :post, "/chat/completions", body)
Summary
Functions
Create a new HTTP client.
Make an HTTP request to the Mistral API.
Make a streaming request to the Mistral API.
Types
@type t() :: %MistralClient.Client{ config: MistralClient.Config.t(), http_client: module(), req_options: keyword() }
Functions
@spec new(keyword() | MistralClient.Config.t()) :: t()
Create a new HTTP client.
Parameters
options
- Configuration options (keyword list) or Config struct (optional)
Examples
client = MistralClient.Client.new()
client = MistralClient.Client.new(api_key: "custom-key")
client = MistralClient.Client.new(config_struct)
@spec request(t(), atom(), String.t(), map() | nil, keyword()) :: {:ok, map()} | {:error, Exception.t()}
Make an HTTP request to the Mistral API.
Parameters
client
- The HTTP clientmethod
- HTTP method (:get, :post, :put, :delete)path
- API endpoint pathbody
- Request body (optional)options
- Additional request options (optional)
Examples
{:ok, response} = MistralClient.Client.request(client, :get, "/models")
{:ok, response} = MistralClient.Client.request(client, :post, "/chat/completions", %{...})
@spec stream_request(t(), atom(), String.t(), map() | nil, function(), keyword()) :: :ok | {:error, Exception.t()}
Make a streaming request to the Mistral API.
Parameters
client
- The HTTP clientmethod
- HTTP method (:get, :post, :put, :delete)path
- API endpoint pathbody
- Request body (optional)callback
- Function to handle each chunkoptions
- Additional request options (optional)
Examples
MistralClient.Client.stream_request(client, :post, "/chat/completions", body, fn chunk ->
IO.write(chunk)
end)