90 lines
2.0 KiB
TypeScript
90 lines
2.0 KiB
TypeScript
/**
|
|
* API配置管理
|
|
* 统一管理所有API相关的配置信息
|
|
*/
|
|
|
|
import ConfigManager from "../manager/ConfigManager";
|
|
|
|
export interface AIConfig {
|
|
/** API密钥 */
|
|
apiKey: string;
|
|
/** 模型名称 */
|
|
model: string;
|
|
/** 生成温度参数 */
|
|
temperature: number;
|
|
/** 最大令牌数 */
|
|
maxTokens?: number;
|
|
/** 请求超时时间(毫秒) */
|
|
timeout?: number;
|
|
}
|
|
|
|
/**
|
|
* API配置管理器
|
|
*/
|
|
export class ApiConfig {
|
|
private static _instance: ApiConfig;
|
|
private config: AIConfig;
|
|
|
|
private constructor() {
|
|
this.initConfig();
|
|
}
|
|
|
|
public static get Instance(): ApiConfig {
|
|
if (!this._instance) {
|
|
this._instance = new ApiConfig();
|
|
}
|
|
return this._instance;
|
|
}
|
|
|
|
/**
|
|
* 初始化配置
|
|
* TODO: 应该从环境变量或安全配置文件中读取
|
|
*/
|
|
private initConfig(): void {
|
|
const config = ConfigManager.tables.TbGlobalConfig;
|
|
this.config = {
|
|
// 警告: API密钥不应该硬编码在代码中
|
|
// 生产环境中应该从环境变量或安全配置文件中读取
|
|
apiKey: config.ApiKey,
|
|
model: config.Model,
|
|
temperature: config.Temperature,
|
|
maxTokens: config.MaxTokens,
|
|
timeout: config.Timeout, // 30秒
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取AI配置
|
|
*/
|
|
public getAIConfig(): AIConfig {
|
|
return { ...this.config };
|
|
}
|
|
|
|
/**
|
|
* 更新生成参数
|
|
* @param temperature 温度参数
|
|
*/
|
|
public updateTemperature(temperature: number): void {
|
|
if (temperature >= 0 && temperature <= 2) {
|
|
this.config.temperature = temperature;
|
|
} else {
|
|
console.warn("Temperature should be between 0 and 2");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证配置是否有效
|
|
*/
|
|
public validateConfig(): boolean {
|
|
if (!this.config.apiKey || this.config.apiKey.trim() === "") {
|
|
console.error("API key is missing");
|
|
return false;
|
|
}
|
|
if (!this.config.model || this.config.model.trim() === "") {
|
|
console.error("Model name is missing");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|