View Source Anthropic.Messages.Request (anthropic_community v0.4.3)

Defines the structure and functionality for creating and sending requests to the Anthropic API.

This module encapsulates the data needed for a request, including model specifications, messages, and various control parameters. It also implements the Jason.Encoder protocol to serialize instances of this struct to JSON format for API requests.

Configuration options:

  • :model - The name of the model to use for generating responses (default: "claude-3-opus-20240229").
  • :max_tokens - The maximum number of tokens allowed in the generated response (default: 1000).
  • :temperature - The sampling temperature for controlling response randomness (default: 1.0).
  • :top_p - The cumulative probability threshold for nucleus sampling (default: 1.0).
  • :top_k - The number of most probable next words considered at each step in sampling for text generation (default: nil).

Summary

Functions

The structure of a request to the Anthropic API.

Creates a new request struct based on the provided configuration.

Encodes the request to JSON and sends it to the Anthropic API via the Finch HTTP client.

Types

@type content_object() ::
  %{type: String.t(), text: String.t()}
  | %{
      type: String.t(),
      source: %{data: String.t(), type: String.t(), media_type: String.t()}
    }
@type message() :: %{content: content_object(), role: String.t()}
@type t() :: %Anthropic.Messages.Request{
  __config__: Anthropic.Config.t(),
  max_tokens: integer() | nil,
  messages: [message()],
  metadata: map() | nil,
  model: String.t() | nil,
  stop_sequences: [String.t()] | nil,
  stream: boolean(),
  system: String.t() | nil,
  temperature: float() | nil,
  tools: MapSet.t(atom()),
  top_k: integer() | nil,
  top_p: float() | nil
}

Functions

Link to this function

%Anthropic.Messages.Request{}

View Source (struct)

The structure of a request to the Anthropic API.

Includes all necessary fields for making a request, such as model information, messages to process, and parameters to control the behavior of the API response.

Creates a new request struct based on the provided configuration.

Initializes a request with the parameters specified in the configuration, setting up default values for the request structure.

Parameters

  • opts: Keyword list containing configuration options for the request.

Returns

A new Anthropic.Messages.Request struct initialized with the provided configuration options.

Link to this function

send_request(request, opts)

View Source

Encodes the request to JSON and sends it to the Anthropic API via the Finch HTTP client.

This function serializes the request struct into JSON, builds the request with the correct headers and path, and sends it using Finch.

Parameters

  • request: The Anthropic.Messages.Request struct to send.
  • opts: Additional options for the Finch request.

Returns

  • {:ok, response} on successful request and response parsing.
  • {:error, reason} if the request fails due to encoding issues or HTTP errors.