Crates.io | aizs |
lib.rs | aizs |
version | 0.1.3 |
source | src |
created_at | 2025-02-02 18:41:06.38014 |
updated_at | 2025-02-02 19:02:13.015257 |
description | Rust SDK for Chinese LLM APIs including Tongyi, DeepSeek, Wenxin,hunyuan, and Xinghuo |
homepage | |
repository | |
max_upload_size | |
id | 1539788 |
size | 24,227 |
这是一个用 Rust 编写的统一中文大语言模型 API 客户端库,支持多个主流的中文大语言模型服务,包括:
在你的 Cargo.toml
文件中添加以下依赖:
[dependencies]
aic_aizs_api = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
use aic_aizs_api::models::deepseek::DeepseekClient;
use aic_aizs_api::types::Message;
use aic_aizs_api::traits::LLMClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建客户端实例
let client = DeepseekClient::new("your-api-key".to_string());
// 准备消息
let messages = vec![
Message::system("You are a helpful assistant."),
Message::user("What is Rust programming language?"),
];
// 发送请求
let response = client.chat(messages).await?;
println!("Response: {}", response);
Ok(())
}
use aic_aizs_api::{
models::{
deepseek::DeepseekClient,
tongyi::TongyiClient,
wenxin::WenxinClient,
},
traits::LLMClient,
types::Message,
};
async fn chat_with_model(client: &impl LLMClient, message: &str) -> Result<String, Box<dyn std::error::Error>> {
let messages = vec![
Message::system("You are a helpful assistant."),
Message::user(message),
];
Ok(client.chat(messages).await?)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 初始化不同的模型客户端
let deepseek = DeepseekClient::new(std::env::var("DEEPSEEK_API_KEY")?);
let tongyi = TongyiClient::new(std::env::var("TONGYI_API_KEY")?);
let wenxin = WenxinClient::new(
std::env::var("WENXIN_API_KEY")?,
std::env::var("WENXIN_SECRET_KEY")?,
);
// 使用不同的模型
let question = "什么是 Rust 编程语言?";
println!("DeepSeek 回答:{}", chat_with_model(&deepseek, question).await?);
println!("通义千问回答:{}", chat_with_model(&tongyi, question).await?);
println!("文心一言回答:{}", chat_with_model(&wenxin, question).await?);
Ok(())
}
各模型需要配置对应的环境变量。在 Windows 系统中,可以通过以下方式设置:
# DeepSeek
$env:DEEPSEEK_API_KEY="your_api_key"
# 腾讯混元
$env:HUNYUAN_APP_ID="your_app_id"
$env:HUNYUAN_API_KEY="your_api_key"
# 阿里通义千问
$env:TONGYI_API_KEY="your_api_key"
# 百度文心一言
$env:WENXIN_API_KEY="your_api_key"
$env:WENXIN_SECRET_KEY="your_secret_key"
# 讯飞星火
$env:XINGHUO_APP_ID="your_app_id"
$env:XINGHUO_API_KEY="your_api_key"
$env:XINGHUO_API_SECRET="your_api_secret"
所有模型客户端都实现了 LLMClient
trait:
#[async_trait]
pub trait LLMClient {
async fn chat(&self, messages: Vec<Message>) -> Result<String>;
}
用于发送消息的结构体:
pub struct Message {
pub role: String,
pub content: String,
}
impl Message {
// 创建系统消息
pub fn system(content: impl Into<String>) -> Self;
// 创建用户消息
pub fn user(content: impl Into<String>) -> Self;
// 创建助手消息
pub fn assistant(content: impl Into<String>) -> Self;
}
库使用 anyhow
进行错误处理,所有错误都会被包装成 Result
类型返回。常见错误包括:
API 密钥配置问题
网络连接问题
响应解析错误
MIT License
感谢以下项目和团队: