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
@type t() :: %Xai.Chat{ client: Xai.Client.t(), messages: [map()], model: String.t(), opts: keyword() }
Functions
Append a message. Pass a string for a simple user message.
@spec create( Xai.Client.t(), keyword() ) :: t()
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.
@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()
Build a system message
Build a user message