1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! # Basic Chat Text Example
//!
//! This example demonstrates how to use the ZAI-RS SDK for basic text-based
//! chat completion with the Zhipu AI API.
//!
//! ## Features Demonstrated
//!
//! - Model selection (GLM-4.5-Flash)
//! - Text message creation
//! - Request parameter configuration
//! - Response handling and parsing
//! - Thinking capability control
//!
//! ## Prerequisites
//!
//! Set the `ZHIPU_API_KEY` environment variable with your API key:
//! ```bash
//! export ZHIPU_API_KEY="your-api-key-here"
//! ```
//!
//! ## Running the Example
//!
//! ```bash
//! cargo run --example chat_text
//! ```
use zai_rs::model::{chat_base_response::ChatCompletionResponse, *};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging for debugging
env_logger::init();
// Select the AI model - GLM-4.5-Flash for fast, efficient responses
let model = GLM4_5_flash {};
// Get API key from environment variable
let key = std::env::var("ZHIPU_API_KEY").expect("ZHIPU_API_KEY must be set");
// User input text (Chinese: "Hello")
let user_text = "你好";
// Build the chat completion request with custom parameters
let client = ChatCompletion::new(model, TextMessage::user(user_text), key)
.with_temperature(0.7) // Control randomness (0.0-1.0)
.with_top_p(0.9) // Control diversity (0.0-1.0)
.with_thinking(ThinkingType::disabled()); // Disable thinking for faster response
// Send the request and await response (non-stream)
let body: ChatCompletionResponse = client.send().await?;
println!("{:#?}", body);
Ok(())
}