outfox_zhipu/lib.rs
1//! Rust library for Zhipu AI (GLM) APIs.
2//!
3//! ## Creating a client
4//!
5//! ```no_run
6//! use outfox_zhipu::Client;
7//! use outfox_zhipu::config::ZhipuConfig;
8//!
9//! // Create a client with default configuration from environment variables.
10//! let client = Client::new();
11//!
12//! // Or create with custom configuration.
13//! let config = ZhipuConfig::new().with_api_key("your-api-key");
14//! let client = Client::with_config(config);
15//! ```
16//!
17//! ## Chat Completions
18//!
19//! ```no_run
20//! # tokio_test::block_on(async {
21//! use outfox_zhipu::Client;
22//! use outfox_zhipu::spec::chat::{ChatMessage, CreateChatCompletionRequestArgs, Model};
23//!
24//! let client = Client::new();
25//!
26//! let request = CreateChatCompletionRequestArgs::default()
27//! .model(Model::Glm4)
28//! .messages(vec![
29//! ChatMessage::system("You are a helpful assistant."),
30//! ChatMessage::user("Hello!"),
31//! ])
32//! .build()?;
33//!
34//! let response = client.chat().create(request).await?;
35//! println!("{}", response.choices[0].message.content);
36//! # Ok::<(), Box<dyn std::error::Error>>(())
37//! # });
38//! ```
39//!
40//! ## Streaming Chat Completions
41//!
42//! ```no_run
43//! # tokio_test::block_on(async {
44//! use futures_util::StreamExt;
45//! use outfox_zhipu::Client;
46//! use outfox_zhipu::spec::chat::{ChatMessage, CreateChatCompletionRequestArgs};
47//!
48//! let client = Client::new();
49//!
50//! let request = CreateChatCompletionRequestArgs::default()
51//! .model("glm-4")
52//! .messages(vec![ChatMessage::user("Tell me a story.")])
53//! .build()?;
54//!
55//! let mut stream = client.chat().create_stream(request).await?;
56//! while let Some(chunk) = stream.next().await {
57//! if let Ok(chunk) = chunk {
58//! if let Some(content) = &chunk.choices[0].delta.content {
59//! print!("{}", content);
60//! }
61//! }
62//! }
63//! # Ok::<(), Box<dyn std::error::Error>>(())
64//! # });
65//! ```
66//!
67//! ## Embeddings
68//!
69//! ```no_run
70//! # tokio_test::block_on(async {
71//! use outfox_zhipu::Client;
72//!
73//! let client = Client::new();
74//!
75//! let embedding = client
76//! .embeddings()
77//! .embed_text("embedding-2", "Hello, world!")
78//! .await?;
79//!
80//! println!("Embedding dimension: {}", embedding.len());
81//! # Ok::<(), Box<dyn std::error::Error>>(())
82//! # });
83//! ```
84//!
85//! ## Image Generation
86//!
87//! ```no_run
88//! # tokio_test::block_on(async {
89//! use outfox_zhipu::Client;
90//!
91//! let client = Client::new();
92//!
93//! let image = client
94//! .images()
95//! .generate("cogview-3", "A beautiful sunset over the ocean")
96//! .await?;
97//!
98//! image.save("sunset.png").await?;
99//! # Ok::<(), Box<dyn std::error::Error>>(())
100//! # });
101//! ```
102//!
103//! ## Environment Variables
104//!
105//! - `ZHIPUAI_API_KEY` or `ZHIPU_API_KEY`: API key
106//! - `ZHIPUAI_BASE_URL` or `ZHIPU_API_BASE`: API base URL (default: https://open.bigmodel.cn/api/paas/v4)
107//!
108//! ## Text-to-Speech
109//!
110//! ```no_run
111//! # tokio_test::block_on(async {
112//! use outfox_zhipu::Client;
113//!
114//! let client = Client::new();
115//!
116//! // Simple synthesis
117//! let audio = client.tts().synthesize("你好,今天天气怎么样?").await?;
118//!
119//! audio.save("output.wav").await?;
120//! # Ok::<(), Box<dyn std::error::Error>>(())
121//! # });
122//! ```
123//!
124//! ## Speech-to-Text
125//!
126//! ```no_run
127//! # tokio_test::block_on(async {
128//! use outfox_zhipu::Client;
129//!
130//! let client = Client::new();
131//!
132//! let result = client.asr().transcribe_file("audio.wav").await?;
133//!
134//! println!("Transcription: {}", result.text);
135//! # Ok::<(), Box<dyn std::error::Error>>(())
136//! # });
137//! ```
138//!
139//! ## Environment Variables
140//!
141//! - `ZHIPUAI_API_KEY` or `ZHIPU_API_KEY`: API key
142//! - `ZHIPUAI_BASE_URL` or `ZHIPU_API_BASE`: API base URL (default: https://open.bigmodel.cn/api/paas/v4)
143//!
144//! ## Features
145//!
146//! - `chat`: Enable Chat Completions API
147//! - `embeddings`: Enable Embeddings API
148//! - `images`: Enable Images API
149//! - `tts`: Enable Text-to-Speech API
150//! - `asr`: Enable Speech-to-Text API
151//! - `async-task`: Enable Async Task APIs (async chat, video, image generation)
152//! - `voice`: Enable Voice APIs (clone, list, delete)
153//! - `reranking`: Enable Text Reranking API
154//! - `tokenizer`: Enable Text Tokenizer API
155//! - `tools`: Enable Tool APIs (web search, web reader, moderation, file parser)
156//! - `full`: Enable all features
157//! - `rustls`: Use rustls for TLS (default)
158//! - `native-tls`: Use native-tls for TLS
159#![cfg_attr(docsrs, feature(doc_cfg))]
160
161#[cfg(feature = "agents")]
162mod agents;
163#[cfg(feature = "asr")]
164mod asr;
165#[cfg(feature = "assistant")]
166mod assistant;
167#[cfg(feature = "async-task")]
168mod async_task;
169#[cfg(feature = "batch")]
170mod batch;
171#[cfg(feature = "chat")]
172mod chat;
173mod client;
174pub mod config;
175#[cfg(feature = "embeddings")]
176mod embeddings;
177pub mod error;
178#[cfg(feature = "files")]
179mod files;
180#[cfg(feature = "images")]
181mod images;
182#[cfg(feature = "ocr")]
183mod ocr;
184#[cfg(feature = "reranking")]
185mod reranking;
186pub mod spec;
187#[cfg(feature = "tokenizer")]
188mod tokenizer;
189#[cfg(feature = "tools")]
190mod tools;
191#[cfg(feature = "tts")]
192mod tts;
193#[cfg(feature = "videos")]
194mod videos;
195#[cfg(feature = "voice")]
196mod voice;
197
198#[cfg(feature = "agents")]
199#[cfg_attr(docsrs, doc(cfg(feature = "agents")))]
200pub use agents::Agents;
201#[cfg(feature = "asr")]
202#[cfg_attr(docsrs, doc(cfg(feature = "asr")))]
203pub use asr::Asr;
204#[cfg(feature = "assistant")]
205#[cfg_attr(docsrs, doc(cfg(feature = "assistant")))]
206pub use assistant::Assistant;
207#[cfg(feature = "async-task")]
208#[cfg_attr(docsrs, doc(cfg(feature = "async-task")))]
209pub use async_task::AsyncTask;
210#[cfg(feature = "batch")]
211#[cfg_attr(docsrs, doc(cfg(feature = "batch")))]
212pub use batch::Batches;
213#[cfg(feature = "chat")]
214#[cfg_attr(docsrs, doc(cfg(feature = "chat")))]
215pub use chat::Chat;
216pub use client::Client;
217#[cfg(feature = "embeddings")]
218#[cfg_attr(docsrs, doc(cfg(feature = "embeddings")))]
219pub use embeddings::Embeddings;
220#[cfg(feature = "files")]
221#[cfg_attr(docsrs, doc(cfg(feature = "files")))]
222pub use files::Files;
223#[cfg(feature = "images")]
224#[cfg_attr(docsrs, doc(cfg(feature = "images")))]
225pub use images::Images;
226#[cfg(feature = "ocr")]
227#[cfg_attr(docsrs, doc(cfg(feature = "ocr")))]
228pub use ocr::Ocr;
229#[cfg(feature = "reranking")]
230#[cfg_attr(docsrs, doc(cfg(feature = "reranking")))]
231pub use reranking::Reranking;
232#[cfg(feature = "tokenizer")]
233#[cfg_attr(docsrs, doc(cfg(feature = "tokenizer")))]
234pub use tokenizer::Tokenizer;
235#[cfg(feature = "tools")]
236#[cfg_attr(docsrs, doc(cfg(feature = "tools")))]
237pub use tools::{FileParser, Moderation, WebReader, WebSearch};
238#[cfg(feature = "tts")]
239#[cfg_attr(docsrs, doc(cfg(feature = "tts")))]
240pub use tts::Tts;
241#[cfg(feature = "videos")]
242#[cfg_attr(docsrs, doc(cfg(feature = "videos")))]
243pub use videos::Videos;
244#[cfg(feature = "voice")]
245#[cfg_attr(docsrs, doc(cfg(feature = "voice")))]
246pub use voice::Voice;