mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-07-04 14:19:19 +00:00
定义ai类
This commit is contained in:
parent
e5929d9268
commit
d37ce5033d
44
models/openai/chatTools.js
Normal file
44
models/openai/chatTools.js
Normal file
@ -0,0 +1,44 @@
|
||||
import OpenAI from 'openai';
|
||||
|
||||
class ChatTools {
|
||||
constructor() {
|
||||
this.openai = null;
|
||||
}
|
||||
|
||||
init(apiKey, baseUrl) {
|
||||
this.openai = new OpenAI({
|
||||
apiKey: apiKey,
|
||||
baseUrl: baseUrl,
|
||||
});
|
||||
}
|
||||
|
||||
async callAi({ prompt, chatHistory = [], model, temperature, customPrompt }) {
|
||||
if (!this.openai) {
|
||||
logger.err('ai未初始化..');
|
||||
return {};
|
||||
}
|
||||
let systemMessage = { role: 'system', content: customPrompt || '' };
|
||||
const messages = [systemMessage, ...chatHistory, { role: 'user', content: prompt }];
|
||||
|
||||
try {
|
||||
const completion = await this.openai.chat.completions.create({
|
||||
messages: messages,
|
||||
model: model,
|
||||
temperature: temperature,
|
||||
frequency_penalty: 0.2,
|
||||
presence_penalty: 0.2,
|
||||
});
|
||||
|
||||
const aiResponse = completion.choices[0].message.content;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
aiResponse: aiResponse,
|
||||
};
|
||||
} catch (err) {
|
||||
logger.err(err);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
export default ChatTools;
|
Loading…
x
Reference in New Issue
Block a user