| Crates.io | aiassistant |
| lib.rs | aiassistant |
| version | 0.1.2 |
| created_at | 2024-12-30 19:49:31.59758+00 |
| updated_at | 2024-12-30 20:03:26.480196+00 |
| description | AI API library |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1499392 |
| size | 6,894 |
一个封装的 OpenAI API Rust 客户端库,支持与 OpenAI 的 GPT 模型进行交互。
在你的 Cargo.toml 中添加以下依赖:
[dependencies]
aiassistant = { path = "../aiassistant" }
use aiassistant::{OpenAIClient, ChatMessage, Role};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// 创建客户端实例
let client = OpenAIClient::new("your-api-key");
// 准备消息
let messages = vec![
ChatMessage::new(Role::System, "You are a helpful assistant."),
ChatMessage::new(Role::User, "Hello!"),
];
// 发送请求
let response = client.chat_completion("gpt-3.5-turbo", messages).await?;
println!("Assistant: {}", response);
Ok(())
}
主要的客户端类,用于与 OpenAI API 交互。
new(api_key: impl Into<String>) -> Self
api_key: OpenAI API 密钥chat_completion(model: impl Into<String>, messages: Vec<ChatMessage>) -> Result<String>
model: 模型名称(如 "gpt-3.5-turbo")messages: 聊天消息列表表示聊天对话中的一条消息。
new(role: Role, content: impl Into<String>) -> Self
role: 消息角色(System/User/Assistant)content: 消息内容MIT