| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /**
- * 本地模型API服务
- *
- * 提供本地模型的查询和连接测试功能(仅查看,无管理功能)
- * 需求: 4
- */
- import { authService } from './authService';
- const API_BASE = import.meta.env.VITE_API_BASE_URL || '';
- export interface LocalModel {
- id: number;
- name: string;
- supplier?: string;
- base_url: string;
- has_api_key: boolean;
- visibility: string;
- category: number;
- created_at: string;
- updated_at: string;
- }
- export interface ConnectionTestResult {
- success: boolean;
- message: string;
- latency_ms?: number;
- }
- interface ApiResponse<T> {
- code: number;
- message: string;
- data: T;
- }
- async function request<T>(url: string, options: RequestInit = {}): Promise<ApiResponse<T>> {
- const token = authService.getToken();
- const headers: HeadersInit = {
- 'Content-Type': 'application/json',
- ...(token ? { Authorization: `Bearer ${token}` } : {}),
- ...options.headers,
- };
- const response = await fetch(`${API_BASE}${url}`, {
- ...options,
- headers,
- });
- if (!response.ok) {
- const error = await response.json().catch(() => ({ message: '请求失败' }));
- throw new Error(error.detail || error.message || '请求失败');
- }
- return response.json();
- }
- export const localModelApi = {
- /**
- * 获取本地模型列表
- */
- async getLocalModels(): Promise<LocalModel[]> {
- const res = await request<LocalModel[]>('/api/models/local');
- return res.data;
- },
- /**
- * 测试本地模型连接
- */
- async testConnection(base_url: string, api_key?: string, model_name?: string): Promise<ConnectionTestResult> {
- const res = await request<ConnectionTestResult>('/api/models/local/test', {
- method: 'POST',
- body: JSON.stringify({ base_url, api_key, model_name }),
- });
- return res.data;
- },
- };
|