rust_cnb/
ai.rs

1//! Ai API 客户端
2
3use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8/// Ai API 客户端
9pub struct AiClient {
10    base_url: String,
11    client: Client,
12}
13
14impl AiClient {
15    /// 创建新的 Ai API 客户端
16    pub fn new(base_url: String, client: Client) -> Self {
17        Self { base_url, client }
18    }
19
20    /// 设置认证信息
21    pub fn with_auth(self, token: &str) -> Self {
22        // 这里可以扩展认证逻辑
23        self
24    }
25
26    /// Ai 对话,参数根据模型不同会有区别。Ai chat completions, params may differ by model.
27    pub async fn post_repo_ai_chat_completions(
28        &self,
29        repo: String,
30        request_data: serde_json::Value,
31    ) -> Result<Value> {
32        let path = format!("/{}/-/ai/chat/completions", repo);
33        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
34        
35
36        
37        let mut request = self.client.request(
38            reqwest::Method::POST,
39            url
40        );
41
42
43
44        request = request.json(&request_data);
45
46        let response = request.send().await?;
47        
48        if response.status().is_success() {
49            let json: Value = response.json().await?;
50            Ok(json)
51        } else {
52            Err(ApiError::HttpError(response.status().as_u16()))
53        }
54    }
55
56}