Xai.Chat (xai v0.1.0)

Copy Markdown View Source

High-level chat interface, modeled after client.chat in the official Python xai-sdk.

Example

client = Xai.Client.new(api_key: System.get_env("XAI_API_KEY"))

chat = Xai.Chat.create(client, model: "grok-4.5")
chat = Xai.Chat.append(chat, Xai.Chat.user("Hello from Elixir!"))

{:ok, response} = Xai.Chat.sample(chat)
IO.puts(response.outputs |> hd() |> Map.get(:message) |> Map.get(:content))

Summary

Functions

Append a message. Pass a string for a simple user message.

Create a new chat session.

Helper to pull text delta from a chunk (adjust as the chunk shape evolves).

Send the messages and get a full response.

Returns a Stream of completion chunks for real-time streaming (gRPC server streaming).

Build a system message

Build a user message

Types

t()

@type t() :: %Xai.Chat{
  client: Xai.Client.t(),
  messages: [map()],
  model: String.t(),
  opts: keyword()
}

Functions

append(chat, text)

@spec append(t(), String.t() | map()) :: t()

Append a message. Pass a string for a simple user message.

create(client, opts)

@spec create(
  Xai.Client.t(),
  keyword()
) :: t()

Create a new chat session.

extract_delta(arg1)

Helper to pull text delta from a chunk (adjust as the chunk shape evolves).

sample(chat, opts \\ [])

@spec sample(
  t(),
  keyword()
) :: {:ok, XaiApi.GetChatCompletionResponse.t()} | {:error, term()}

Send the messages and get a full response.

stream(chat, opts \\ [])

@spec stream(
  t(),
  keyword()
) :: Enumerable.t()

Returns a Stream of completion chunks for real-time streaming (gRPC server streaming).

Mirrors the Python chat.stream().

Usage:

Xai.Chat.stream(chat)
|> Stream.each(fn chunk ->
  # chunk is %XaiApi.GetChatCompletionChunk{}
  IO.write(extract_delta(chunk))
end)
|> Stream.run()

system(text)

Build a system message

user(text)

Build a user message