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
//! # Chat Coding Plan Example
//!
//! This example demonstrates how to use the ZAI-RS SDK with the coding plan API
//! endpoint for specialized coding assistance with the Zhipu AI API.
//!
//! ## Features Demonstrated
//!
//! - Model selection (GLM-4.5-Flash)
//! - Text message creation
//! - Coding plan endpoint configuration
//! - Request parameter configuration
//! - Response handling and parsing
//!
//! ## 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_coding_plan
//! ```
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_6 {};
// Get API key from environment variable
let key = std::env::var("ZHIPU_API_KEY").expect("ZHIPU_API_KEY must be set");
// User input for coding assistance (Chinese: "Help me write a Rust function to
// calculate factorial")
let user_text = "帮我写一个计算阶乘的 Rust 函数。只返回函数。其他内容不要返回";
// Build the chat completion request with coding plan endpoint
let client = ChatCompletion::new(model, TextMessage::user(user_text), key).with_coding_plan();
// Send the request and await response (non-stream)
let body: ChatCompletionResponse = client.send().await?;
println!("{:#?}", body);
Ok(())
}