nah_chat

Crates.ionah_chat
lib.rsnah_chat
version0.5.1
created_at2025-07-24 04:00:10.944056+00
updated_at2025-08-07 03:35:08.032719+00
descriptionLightweight LLM chat completion API.
homepagehttps://github.com/linmx0130/nah
repositoryhttps://github.com/linmx0130/nah
max_upload_size
id1765486
size65,295
Mengxiao Lin (linmx0130)

documentation

README

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

Commit count: 0

cargo fmt