Thanks to visit codestin.com
Credit goes to docs.rs

Crate nah_chat

Crate nah_chat 

Source
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, &params)
       .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§

ChatClient
The object to hold information about the model server and reqwest HTTP client.
ChatCompletionParamsBuilder
A builder for creating parameters for chat completion requests.
ChatMessage
Data structure of a chat message, could be from the user, the assistant or the tool.
ChatResponseChunkDelta
Chunk delta of chat message from the assistant.
Error
Error type of nah_chat.
FunctionCallRequest
A function call request.
FunctionCallRequestChunkDelta
A function call request chunk received from stream api.
ToolCallRequest
A tool call request. Only function call is supported now.
ToolCallRequestChunkDelta
A tool call request chunk received from stream api.
TypedChatMessageContent
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§

ChatMessageContentValue
Type for message contents.
ChatResponseChunk
A chunk of chat message response from the assistant.
ErrorKind
Error kinds that may occur in nah_chat.

Type Aliases§

Result