/** * 腾讯云向量数据库接口 */ import { Logger } from "./Logger"; export class TencentCloudVectorApiClient { private baseUrl: string; private apiKey: string; private apiAccount: string; constructor(options?: { apiKey: string, apiAccount: string, baseUrl: string }) { this.apiKey = options?.apiKey || process.env.TENCENT_CLOUD_VECTOR_DB_API_KEY as string; this.apiAccount = options?.apiAccount || process.env.TENCENT_CLOUD_VECTOR_DB_ACCOUNT || 'root' this.baseUrl = options?.baseUrl || process.env.TENCENT_CLOUD_VECTOR_DB_API_HOST as string } async request(endpoint: string, method: string = 'GET', data?: any): Promise { const url = `${this.baseUrl}${endpoint}`; try { const response = await fetch(url, { method: method, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer account=${this.apiAccount}&api_key=${this.apiKey}` }, body: data ? JSON.stringify(data) : undefined }) if (!response.ok || response.status !== 200) { // Logger.debug(await response.json()) try { const json = await response.json() if (json) { return json as T } } catch (err) { Logger.error("no json") } throw new Error(`${response.status}: ${response.statusText}`) } return await response.json() as T } catch (error: any) { return Promise.reject(error) } } }