MistralClient.Client (mistralex_ai v0.1.0)

View Source

HTTP 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.

Types

t()

@type t() :: %MistralClient.Client{
  config: MistralClient.Config.t(),
  http_client: module(),
  req_options: keyword()
}

Functions

new(options \\ [])

@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)

request(client, method, path, body \\ nil, options \\ [])

@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 client
  • method - HTTP method (:get, :post, :put, :delete)
  • path - API endpoint path
  • body - 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", %{...})

stream_request(client, method, path, body \\ nil, callback, options \\ [])

@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 client
  • method - HTTP method (:get, :post, :put, :delete)
  • path - API endpoint path
  • body - Request body (optional)
  • callback - Function to handle each chunk
  • options - Additional request options (optional)

Examples

MistralClient.Client.stream_request(client, :post, "/chat/completions", body, fn chunk ->
  IO.write(chunk)
end)