Expand description
Rust library for Zhipu AI (GLM) APIs.
§Creating a client
use novel_zhipu::Client;
use novel_zhipu::config::ZhipuConfig;
// Create a client with default configuration from environment variables.
let client = Client::new();
// Or create with custom configuration.
let config = ZhipuConfig::new().with_api_key("your-api-key");
let client = Client::with_config(config);§Chat Completions
use novel_zhipu::Client;
use novel_zhipu::spec::chat::{ChatMessage, CreateChatCompletionRequestArgs, Model};
let client = Client::new();
let request = CreateChatCompletionRequestArgs::default()
.model(Model::Glm4)
.messages(vec![
ChatMessage::system("You are a helpful assistant."),
ChatMessage::user("Hello!"),
])
.build()?;
let response = client.chat().create(request).await?;
println!("{}", response.choices[0].message.content);§Streaming Chat Completions
use futures_util::StreamExt;
use novel_zhipu::Client;
use novel_zhipu::spec::chat::{ChatMessage, CreateChatCompletionRequestArgs};
let client = Client::new();
let request = CreateChatCompletionRequestArgs::default()
.model("glm-4")
.messages(vec![ChatMessage::user("Tell me a story.")])
.build()?;
let mut stream = client.chat().create_stream(request).await?;
while let Some(chunk) = stream.next().await {
if let Ok(chunk) = chunk {
if let Some(content) = &chunk.choices[0].delta.content {
print!("{}", content);
}
}
}§Embeddings
use novel_zhipu::Client;
let client = Client::new();
let embedding = client
.embeddings()
.embed_text("embedding-2", "Hello, world!")
.await?;
println!("Embedding dimension: {}", embedding.len());§Image Generation
use novel_zhipu::Client;
let client = Client::new();
let image = client
.images()
.generate("cogview-3", "A beautiful sunset over the ocean")
.await?;
image.save("sunset.png").await?;§Environment Variables
ZHIPUAI_API_KEYorZHIPU_API_KEY: API keyZHIPUAI_BASE_URLorZHIPU_API_BASE: API base URL (default: https://open.bigmodel.cn/api/paas/v4)
§Text-to-Speech
use novel_zhipu::Client;
let client = Client::new();
// Simple synthesis
let audio = client.tts().synthesize("你好,今天天气怎么样?").await?;
audio.save("output.wav").await?;§Speech-to-Text
use novel_zhipu::Client;
let client = Client::new();
let result = client.asr().transcribe_file("audio.wav").await?;
println!("Transcription: {}", result.text);§Environment Variables
ZHIPUAI_API_KEYorZHIPU_API_KEY: API keyZHIPUAI_BASE_URLorZHIPU_API_BASE: API base URL (default: https://open.bigmodel.cn/api/paas/v4)
§Features
chat: Enable Chat Completions APIembeddings: Enable Embeddings APIimages: Enable Images APItts: Enable Text-to-Speech APIasr: Enable Speech-to-Text APIasync-task: Enable Async Task APIs (async chat, video, image generation)voice: Enable Voice APIs (clone, list, delete)reranking: Enable Text Reranking APItokenizer: Enable Text Tokenizer APItools: Enable Tool APIs (web search, web reader, moderation, file parser)full: Enable all featuresrustls: Use rustls for TLS (default)native-tls: Use native-tls for TLS
Modules§
- config
- Configuration for Zhipu AI API client.
- error
- Error types for Zhipu AI API client.
- spec
- Type definitions for Zhipu AI APIs.
Structs§
- Agents
agents - Agents API.
- Asr
asr - Speech-to-text (ASR) API.
- Assistant
assistant - Assistant API.
- Async
Task async-task - Async task API.
- Batches
batch - Batch API.
- Chat
chat - Chat completions API.
- Client
- Zhipu AI API client.
- Embeddings
embeddings - Embeddings API.
- File
Parser tools - File parsing API.
- Files
files - Files API.
- Images
images - Images API.
- Moderation
tools - Content moderation API.
- Ocr
ocr - OCR API.
- Reranking
reranking - Text reranking API.
- Tokenizer
tokenizer - Text tokenizer API.
- Tts
tts - Text-to-speech API.
- Videos
videos - Videos API.
- Voice
voice - Voice API.
- WebReader
tools - Web reader API.
- WebSearch
tools - Web search API.