rc-plugin/utils/openai-builder.js
zhiyu1998 85da3f04f1 🔧 refactor: 优化 OpenaiBuilder 类超时设置和响应数据处理
- 调整 utils/openai-builder.js 中的 axios 超时时间从 10000 毫秒增加到 100000 毫秒
- 更新 utils/openai-builder.js 中的响应数据处理逻辑,以匹配新的 API 响应结构
- 确保代码清晰和功能正常运行,提升代码可维护性
- 优化 API 交互流程,提高响应效率和稳定性
2024-09-19 21:51:23 +08:00

72 lines
1.7 KiB
JavaScript

import axios from "axios";
export class OpenaiBuilder {
constructor() {
this.baseURL = "https://api.moonshot.cn"; // 默认模型
this.apiKey = ""; // 默认API密钥
this.prompt = "描述一下这个图片"; // 默认提示
this.model = 'claude-3-haiku-20240307'
this.path = ''; // 上传文件的路径
}
setBaseURL(baseURL) {
this.baseURL = baseURL;
return this;
}
setApiKey(apiKey) {
this.apiKey = apiKey;
return this;
}
setPrompt(prompt) {
this.prompt = prompt;
return this;
}
setModel(model) {
this.model = model;
return this;
}
setPath(path) {
this.path = path;
return this;
}
async build() {
// logger.info(this.baseURL, this.apiKey)
// 创建客户端
this.client = axios.create({
baseURL: this.baseURL,
timeout: 100000,
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.apiKey
}
});
return this;
}
async kimi(query) {
// 请求Kimi
const completion = await this.client.post("/v1/chat/completions", {
model: "moonshot-v1-8k",
messages: [
{
"role": "system",
"content": this.prompt,
},
{
role: "user",
content: query
},
],
});
return {
"model": "月之暗面 Kimi",
"ans": completion.data.choices[0].message.content
}
}
}