ReqAnthropic.Conversation (ReqAnthropic v0.2.1)

Copy Markdown View Source

A small client-side helper for multi-turn Messages conversations.

This is NOT a server resource — the Anthropic Messages API is stateless and each request carries the full history. Conversation just holds the accumulated messages plus optional system prompt and default request options so you don't have to rebuild the payload yourself.

Example

convo =
  ReqAnthropic.Conversation.new(
    model: "claude-haiku-4-5",
    max_tokens: 512,
    system: "You are a concise assistant."
  )
  |> ReqAnthropic.Conversation.user("Tell me about Elixir.")

{:ok, convo} = ReqAnthropic.Conversation.send(convo)
IO.puts(ReqAnthropic.Conversation.last_text(convo))

convo =
  convo
  |> ReqAnthropic.Conversation.user("What about OTP?")

{:ok, convo} = ReqAnthropic.Conversation.send(convo)

Summary

Functions

Append an assistant message (e.g. from a prior response).

Return the plain text from the most recent assistant reply, if any.

Build a new conversation. :model is required. Any other keyword is forwarded to ReqAnthropic.Messages.create/1 on each send/2 call, which includes :api_key, :max_tokens, :temperature, :tools, :metadata, :system, etc.

Send the conversation to the Messages API and return an updated Conversation with the assistant's reply appended.

Append a user message. content may be a string or a content-block list.

Types

message()

@type message() :: %{role: String.t(), content: term()}

t()

@type t() :: %ReqAnthropic.Conversation{
  last_response: map() | nil,
  messages: [message()],
  model: String.t(),
  request_options: keyword(),
  system: String.t() | [map()] | nil
}

Functions

assistant(convo, content)

@spec assistant(t(), String.t() | [map()]) :: t()

Append an assistant message (e.g. from a prior response).

last_text(conversation)

@spec last_text(t()) :: String.t()

Return the plain text from the most recent assistant reply, if any.

new(opts)

@spec new(keyword()) :: t()

Build a new conversation. :model is required. Any other keyword is forwarded to ReqAnthropic.Messages.create/1 on each send/2 call, which includes :api_key, :max_tokens, :temperature, :tools, :metadata, :system, etc.

send(convo, extra_opts \\ [])

@spec send(
  t(),
  keyword()
) :: {:ok, t()} | {:error, term()}

Send the conversation to the Messages API and return an updated Conversation with the assistant's reply appended.

extra_opts are merged over the conversation's stored request options, so you can override :max_tokens, :temperature, etc. on a per-turn basis.

user(convo, content)

@spec user(t(), String.t() | [map()]) :: t()

Append a user message. content may be a string or a content-block list.