openai-core 0.1.1

Rust SDK for OpenAI-compatible ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Vector Stores 命名空间实现。

use std::collections::BTreeMap;

use http::Method;
use serde::{Deserialize, Serialize};
use serde_json::{Number, Value};

use super::{
    DeleteResponse, JsonRequestBuilder, ListRequestBuilder, VectorStoreFileBatchesResource,
    VectorStoreFilesResource, VectorStoresResource, encode_path_segment,
};
use crate::Page;
use crate::generated::endpoints;

/// Vector store 元数据。
pub type VectorStoreMetadata = BTreeMap<String, String>;

/// Vector store 文件属性。
pub type VectorStoreAttributes = BTreeMap<String, VectorStoreAttributeValue>;

/// 表示 metadata / attributes 中的标量值。
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum VectorStoreAttributeValue {
    /// 字符串值。
    String(String),
    /// 数字值。
    Number(Number),
    /// 布尔值。
    Bool(bool),
}

/// 表示 vector store 文件计数。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileCounts {
    /// 已取消文件数量。
    pub cancelled: Option<u64>,
    /// 已完成文件数量。
    pub completed: Option<u64>,
    /// 失败文件数量。
    pub failed: Option<u64>,
    /// 处理中数量。
    pub in_progress: Option<u64>,
    /// 文件总数。
    pub total: Option<u64>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 过期策略。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreExpiresAfter {
    /// 锚点。
    pub anchor: Option<String>,
    /// 过期天数。
    pub days: Option<u64>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示静态分块策略配置。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreStaticFileChunkingStrategy {
    /// chunk 重叠 token 数。
    pub chunk_overlap_tokens: Option<u64>,
    /// 每个 chunk 的最大 token 数。
    pub max_chunk_size_tokens: Option<u64>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示文件分块策略。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum VectorStoreFileChunkingStrategy {
    /// 静态分块。
    Static {
        /// 静态分块配置。
        #[serde(rename = "static")]
        configuration: VectorStoreStaticFileChunkingStrategy,
    },
    /// 旧文件或未知策略返回的 other 类型。
    Other,
    /// 向前兼容的未知策略。
    #[serde(other)]
    Unknown,
}

/// 表示 vector store 文件处理错误。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileLastError {
    /// 错误码。
    pub code: Option<String>,
    /// 错误描述。
    pub message: Option<String>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 文件内容项。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileContent {
    /// 内容文本。
    pub text: Option<String>,
    /// 内容类型。
    #[serde(rename = "type")]
    pub content_type: Option<String>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 搜索命中的内容片段。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchContent {
    /// 返回的文本内容。
    pub text: Option<String>,
    /// 内容类型。
    #[serde(rename = "type")]
    pub content_type: Option<String>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 搜索命中项。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchResult {
    /// 文件属性过滤数据。
    pub attributes: Option<VectorStoreAttributes>,
    /// 内容片段。
    #[serde(default)]
    pub content: Vec<VectorStoreSearchContent>,
    /// 文件 ID。
    pub file_id: Option<String>,
    /// 文件名。
    pub filename: Option<String>,
    /// 相似度分数。
    pub score: Option<f64>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 对象。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStore {
    /// 对象 ID。
    pub id: String,
    /// 对象类型。
    #[serde(default)]
    pub object: String,
    /// 创建时间戳。
    pub created_at: Option<u64>,
    /// 描述。
    pub description: Option<String>,
    /// 名称。
    pub name: Option<String>,
    /// 状态。
    pub status: Option<String>,
    /// 最后活跃时间。
    pub last_active_at: Option<u64>,
    /// 占用字节数。
    pub usage_bytes: Option<u64>,
    /// 文件计数。
    pub file_counts: Option<VectorStoreFileCounts>,
    /// 元数据。
    pub metadata: Option<VectorStoreMetadata>,
    /// 过期策略。
    pub expires_after: Option<VectorStoreExpiresAfter>,
    /// 过期时间。
    pub expires_at: Option<u64>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 文件对象。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFile {
    /// 对象 ID。
    pub id: String,
    /// 对象类型。
    #[serde(default)]
    pub object: String,
    /// 创建时间戳。
    pub created_at: Option<u64>,
    /// 关联的 vector store ID。
    pub vector_store_id: Option<String>,
    /// 状态。
    pub status: Option<String>,
    /// 最近错误。
    pub last_error: Option<VectorStoreFileLastError>,
    /// 占用字节数。
    pub usage_bytes: Option<u64>,
    /// 属性。
    pub attributes: Option<VectorStoreAttributes>,
    /// 分块策略。
    pub chunking_strategy: Option<VectorStoreFileChunkingStrategy>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store file batch 对象。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileBatch {
    /// 对象 ID。
    pub id: String,
    /// 对象类型。
    #[serde(default)]
    pub object: String,
    /// 创建时间戳。
    pub created_at: Option<u64>,
    /// 关联的 vector store ID。
    pub vector_store_id: Option<String>,
    /// 状态。
    pub status: Option<String>,
    /// 文件计数。
    pub file_counts: Option<VectorStoreFileCounts>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// 表示 vector store 搜索返回值。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchResponse {
    /// 对象类型。
    #[serde(default)]
    pub object: String,
    /// 搜索结果。
    #[serde(default)]
    pub data: Vec<VectorStoreSearchResult>,
    /// 搜索查询。
    pub search_query: Option<String>,
    /// 额外字段。
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl VectorStoresResource {
    /// 创建 vector store。
    pub fn create(&self) -> JsonRequestBuilder<VectorStore> {
        let endpoint = endpoints::vector_stores::VECTOR_STORES_CREATE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.template,
        )
    }

    /// 获取 vector store。
    pub fn retrieve(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<VectorStore> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_RETRIEVE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::GET,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 更新 vector store。
    pub fn update(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<VectorStore> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_UPDATE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 列出 vector store。
    pub fn list(&self) -> ListRequestBuilder<VectorStore> {
        let endpoint = endpoints::vector_stores::VECTOR_STORES_LIST;
        vector_store_list(self.client.clone(), endpoint.id, endpoint.template)
    }

    /// 删除 vector store。
    pub fn delete(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<DeleteResponse> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_DELETE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::DELETE,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 搜索 vector store。
    pub fn search(
        &self,
        vector_store_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreSearchResponse> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_SEARCH;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 返回 files 子资源。
    pub fn files(&self) -> VectorStoreFilesResource {
        VectorStoreFilesResource::new(self.client.clone())
    }

    /// 返回 file_batches 子资源。
    pub fn file_batches(&self) -> VectorStoreFileBatchesResource {
        VectorStoreFileBatchesResource::new(self.client.clone())
    }
}

impl VectorStoreFilesResource {
    /// 创建 vector store 文件。
    pub fn create(
        &self,
        vector_store_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFile> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_CREATE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 获取 vector store 文件。
    pub fn retrieve(
        &self,
        vector_store_id: impl Into<String>,
        file_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFile> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let file_id = encode_path_segment(file_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_RETRIEVE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::GET,
            endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
        )
    }

    /// 更新 vector store 文件。
    pub fn update(
        &self,
        vector_store_id: impl Into<String>,
        file_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFile> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let file_id = encode_path_segment(file_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_UPDATE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
        )
    }

    /// 列出 vector store 文件。
    pub fn list(&self, vector_store_id: impl Into<String>) -> ListRequestBuilder<VectorStoreFile> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_LIST;
        vector_store_list(
            self.client.clone(),
            endpoint.id,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 删除 vector store 文件。
    pub fn delete(
        &self,
        vector_store_id: impl Into<String>,
        file_id: impl Into<String>,
    ) -> JsonRequestBuilder<DeleteResponse> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let file_id = encode_path_segment(file_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_DELETE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::DELETE,
            endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
        )
    }

    /// 获取 vector store 文件内容。
    pub fn content(
        &self,
        vector_store_id: impl Into<String>,
        file_id: impl Into<String>,
    ) -> JsonRequestBuilder<Page<VectorStoreFileContent>> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let file_id = encode_path_segment(file_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_CONTENT;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::GET,
            endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
        )
    }
}

impl VectorStoreFileBatchesResource {
    /// 创建 file batch。
    pub fn create(
        &self,
        vector_store_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFileBatch> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_CREATE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[("vector_store_id", &vector_store_id)]),
        )
    }

    /// 获取 file batch。
    pub fn retrieve(
        &self,
        vector_store_id: impl Into<String>,
        batch_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFileBatch> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let batch_id = encode_path_segment(batch_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_RETRIEVE;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::GET,
            endpoint.render(&[
                ("vector_store_id", &vector_store_id),
                ("batch_id", &batch_id),
            ]),
        )
    }

    /// 取消 file batch。
    pub fn cancel(
        &self,
        vector_store_id: impl Into<String>,
        batch_id: impl Into<String>,
    ) -> JsonRequestBuilder<VectorStoreFileBatch> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let batch_id = encode_path_segment(batch_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_CANCEL;
        vector_store_json(
            self.client.clone(),
            endpoint.id,
            Method::POST,
            endpoint.render(&[
                ("vector_store_id", &vector_store_id),
                ("batch_id", &batch_id),
            ]),
        )
    }

    /// 列出 file batch 文件。
    pub fn list_files(
        &self,
        vector_store_id: impl Into<String>,
        batch_id: impl Into<String>,
    ) -> ListRequestBuilder<VectorStoreFile> {
        let vector_store_id = encode_path_segment(vector_store_id.into());
        let batch_id = encode_path_segment(batch_id.into());
        let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_LIST_FILES;
        vector_store_list(
            self.client.clone(),
            endpoint.id,
            endpoint.render(&[
                ("vector_store_id", &vector_store_id),
                ("batch_id", &batch_id),
            ]),
        )
    }
}

fn vector_store_json<T>(
    client: crate::Client,
    endpoint_id: &'static str,
    method: Method,
    path: impl Into<String>,
) -> JsonRequestBuilder<T> {
    JsonRequestBuilder::new(client, endpoint_id, method, path)
        .extra_header("openai-beta", "assistants=v2")
}

fn vector_store_list<T>(
    client: crate::Client,
    endpoint_id: &'static str,
    path: impl Into<String>,
) -> ListRequestBuilder<T> {
    ListRequestBuilder::new(client, endpoint_id, path).extra_header("openai-beta", "assistants=v2")
}