快速入门
选择您的运行时,几分钟内即可开始发起 AI 调用。
- 任意支持供应商的 API 密钥(如
OPENAI_API_KEY、ANTHROPIC_API_KEY、DEEPSEEK_API_KEY) - AI-Protocol 仓库(若无本地副本,会自动从 GitHub 拉取)
1. 添加依赖
Section titled “1. 添加依赖”[dependencies]ai-lib = "0.7"tokio = { version = "1", features = ["full"] }2. 设置 API 密钥
Section titled “2. 设置 API 密钥”export ANTHROPIC_API_KEY="your-key-here"3. 编写第一个程序
Section titled “3. 编写第一个程序”use ai_lib::{AiClient, StreamingEvent};use futures::StreamExt;
#[tokio::main]async fn main() -> ai_lib::Result<()> { // Create client — protocol manifest is loaded automatically let client = AiClient::new("anthropic/claude-3-5-sonnet").await?;
// Streaming chat let mut stream = client.chat() .user("What is AI-Protocol?") .temperature(0.7) .max_tokens(500) .stream() .execute_stream() .await?;
while let Some(event) = stream.next().await { match event? { StreamingEvent::ContentDelta { text, .. } => print!("{text}"), StreamingEvent::StreamEnd { .. } => println!(), _ => {} } } Ok(())}cargo runPython
Section titled “Python”1. 安装包
Section titled “1. 安装包”pip install ai-lib-python>=0.6.02. 设置 API 密钥
Section titled “2. 设置 API 密钥”export ANTHROPIC_API_KEY="your-key-here"3. 编写第一个脚本
Section titled “3. 编写第一个脚本”import asynciofrom ai_lib_python import AiClient
async def main(): # Create client — protocol manifest loaded automatically client = await AiClient.create("anthropic/claude-3-5-sonnet")
# Streaming chat async for event in client.chat() \ .user("What is AI-Protocol?") \ .temperature(0.7) \ .max_tokens(500) \ .stream(): if event.is_content_delta: print(event.as_content_delta.text, end="") print()
asyncio.run(main())python main.pyAI-Lib 的魔力:只需修改一个字符串即可切换供应商。
// Rust — just change the model IDlet client = AiClient::new("openai/gpt-4o").await?;let client = AiClient::new("deepseek/deepseek-chat").await?;let client = AiClient::new("gemini/gemini-2.0-flash").await?;# Python — same thingclient = await AiClient.create("openai/gpt-4o")client = await AiClient.create("deepseek/deepseek-chat")client = await AiClient.create("gemini/gemini-2.0-flash")无需修改代码。协议清单会处理每个供应商的端点、认证、参数映射和流式格式。
- 生态系统架构 — 各组件如何协同工作
- Chat Completions 指南 — 详细聊天 API 用法
- 函数调用 — 工具使用与函数调用
- Rust SDK 详解 — 深入 Rust
- Python SDK 详解 — 深入 Python