Expand description
§Introduction
This crate exposes an async stream API for the widely-used OpenAI chat completion API.
Supported features:
- Stream generation
- Tool calls
- Reasoning content (Qwen3, Deepseek R1, etc)
This crate is built on top of tokio, reqwest and serde_json.
use nah_chat::{ChatClient, ChatMessage};
use futures_util::{pin_mut, StreamExt};
let chat_client = ChatClient::init(base_url, auth_token);
// create and pin the stream
let stream = chat_client
.chat_completion_stream(model_name, &messages, ¶ms)
.await
.unwrap();
pin_mut!(stream);
// buffer for the new message
let mut message = ChatMessage::new();
// consume the stream
while let Some(delta_result) = stream.next().await {
match delta_result {
Ok(delta) => {
message.apply_model_response_chunk(delta);
}
Err(e) => {
eprintln!("Error occurred while processing the chat completion: {}", e);
}
}
}§Notice
Copyright 2025, Mengxiao Lin.
This is a part of nah project. nah means “Not A
Human”. Source code is available under MPL-2.0.
Structs§
- Chat
Client - The object to hold information about the model server and
reqwestHTTP client. - Chat
Completion Params Builder - A builder for creating parameters for chat completion requests.
- Chat
Message - Data structure of a chat message, could be from the user, the assistant or the tool.
- Chat
Response Chunk Delta - Chunk delta of chat message from the assistant.
- Error
- Error type of
nah_chat. - Function
Call Request - A function call request.
- Function
Call Request Chunk Delta - A function call request chunk received from stream api.
- Tool
Call Request - A tool call request. Only function call is supported now.
- Tool
Call Request Chunk Delta - A tool call request chunk received from stream api.
- Typed
Chat Message Content - This is used to represent a message content that can have multiple types with a type annotation. Currently, it supports text and image_url.
- URLObject
- This is used to represent a URL in the message content. Currently, only images will be represented in this format.
Enums§
- Chat
Message Content Value - Type for message contents.
- Chat
Response Chunk - A chunk of chat message response from the assistant.
- Error
Kind - Error kinds that may occur in
nah_chat.