# `Xai.Chat`
[🔗](https://github.com/beamlab-xai/xai/blob/main/lib/xai/chat.ex#L1)

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

# `t`

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

# `append`

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

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

# `create`

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

Create a new chat session.

# `extract_delta`

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

# `sample`

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

Send the messages and get a full response.

# `stream`

```elixir
@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`

Build a system message

# `user`

Build a user message

---

*Consult [api-reference.md](api-reference.md) for complete listing*
