embeddingConfigApi.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { API_BASE_URL } from "@/lib/config";
  2. // 知识库向量配置(API 返回,不含明文 API Key)
  3. export interface EmbeddingConfig {
  4. id?: number;
  5. embedding_type: string;
  6. api_url: string;
  7. api_key_masked?: string;
  8. model: string;
  9. customer_can_use_kb: boolean;
  10. updated_at?: string;
  11. }
  12. // 更新入参(api_key 可选,不传则保留原密钥)
  13. export interface UpdateEmbeddingConfigRequest {
  14. embedding_type?: string;
  15. api_url?: string;
  16. api_key?: string;
  17. model?: string;
  18. customer_can_use_kb?: boolean;
  19. }
  20. /** 获取当前知识库向量配置(需传 user_id 以通过代理) */
  21. export async function fetchEmbeddingConfig(userId: number): Promise<EmbeddingConfig> {
  22. const res = await fetch(`${API_BASE_URL}/agent/embedding-config?user_id=${userId}`, {
  23. cache: "no-store",
  24. });
  25. if (!res.ok) {
  26. throw new Error("获取知识库向量配置失败");
  27. }
  28. return res.json();
  29. }
  30. /** 更新知识库向量配置(仅管理员);修改后需重启后端生效 */
  31. export async function updateEmbeddingConfig(
  32. userId: number,
  33. data: UpdateEmbeddingConfigRequest
  34. ): Promise<EmbeddingConfig> {
  35. const res = await fetch(`${API_BASE_URL}/agent/embedding-config`, {
  36. method: "PUT",
  37. headers: { "Content-Type": "application/json" },
  38. body: JSON.stringify({ user_id: userId, ...data }),
  39. });
  40. if (!res.ok) {
  41. const err = await res.json();
  42. throw new Error(err.error || "更新知识库向量配置失败");
  43. }
  44. return res.json();
  45. }