localModelApi.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * 本地模型API服务
  3. *
  4. * 提供本地模型的查询和连接测试功能(仅查看,无管理功能)
  5. * 需求: 4
  6. */
  7. import { authService } from './authService';
  8. const API_BASE = import.meta.env.VITE_API_BASE_URL || '';
  9. export interface LocalModel {
  10. id: number;
  11. name: string;
  12. supplier?: string;
  13. base_url: string;
  14. has_api_key: boolean;
  15. visibility: string;
  16. category: number;
  17. created_at: string;
  18. updated_at: string;
  19. }
  20. export interface ConnectionTestResult {
  21. success: boolean;
  22. message: string;
  23. latency_ms?: number;
  24. }
  25. interface ApiResponse<T> {
  26. code: number;
  27. message: string;
  28. data: T;
  29. }
  30. async function request<T>(url: string, options: RequestInit = {}): Promise<ApiResponse<T>> {
  31. const token = authService.getToken();
  32. const headers: HeadersInit = {
  33. 'Content-Type': 'application/json',
  34. ...(token ? { Authorization: `Bearer ${token}` } : {}),
  35. ...options.headers,
  36. };
  37. const response = await fetch(`${API_BASE}${url}`, {
  38. ...options,
  39. headers,
  40. });
  41. if (!response.ok) {
  42. const error = await response.json().catch(() => ({ message: '请求失败' }));
  43. throw new Error(error.detail || error.message || '请求失败');
  44. }
  45. return response.json();
  46. }
  47. export const localModelApi = {
  48. /**
  49. * 获取本地模型列表
  50. */
  51. async getLocalModels(): Promise<LocalModel[]> {
  52. const res = await request<LocalModel[]>('/api/models/local');
  53. return res.data;
  54. },
  55. /**
  56. * 测试本地模型连接
  57. */
  58. async testConnection(base_url: string, api_key?: string, model_name?: string): Promise<ConnectionTestResult> {
  59. const res = await request<ConnectionTestResult>('/api/models/local/test', {
  60. method: 'POST',
  61. body: JSON.stringify({ base_url, api_key, model_name }),
  62. });
  63. return res.data;
  64. },
  65. };