支持调用ollama

This commit is contained in:
Jerry 2025-04-24 17:37:22 +08:00
parent d37ce5033d
commit 4cc09172ea
6 changed files with 1192 additions and 6 deletions

View File

View File

View File

@ -0,0 +1,61 @@
import axios from 'axios';
class OllamaChat {
constructor() {
this.apiUrl = null;
this.apiKey = null;
}
/**
*
* @param apiKey 密钥
* @param baseUrl ollamaAPI地址
*/
init(apiKey, baseUrl) {
this.apiKey = apiKey;
this.apiUrl = baseUrl;
}
/**
*
* @param prompt 用户命令+提示词融合
* @param chatHistory 历史记录
* @param model 模型
* @param temperature 温度
* @returns {Promise<{success: boolean}|{success: boolean, aiResponse: (*|string)}>}
*/
async callAi({ prompt, chatHistory = [], model, temperature }) {
if (!this.apiUrl || !this.apiKey) {
logger.err('ollama未初始化..');
return { success: false };
}
const requestData = {
model: model,
prompt: prompt,
temperature: temperature,
history: chatHistory,
};
try {
const response = await axios.post(`${this.apiUrl}/v1/complete`, requestData, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
});
const aiResponse = response.data?.choices[o]?.text || '';
return {
success: true,
aiResponse: aiResponse,
};
} catch (err) {
logger.err(err);
return { success: false };
}
}
}
export default OllamaChat;

View File

@ -1,10 +1,14 @@
import OpenAI from 'openai'; import OpenAI from 'openai';
class ChatTools { class OpenaiChat {
constructor() { constructor() {
this.openai = null; this.openai = null;
} }
/**
* @param apiKey 密钥
* @param baseUrl openaiAPI地址
*/
init(apiKey, baseUrl) { init(apiKey, baseUrl) {
this.openai = new OpenAI({ this.openai = new OpenAI({
apiKey: apiKey, apiKey: apiKey,
@ -12,13 +16,31 @@ class ChatTools {
}); });
} }
/**
* @param prompt 主内容
* @param chatHistory 聊天历史记录
* @param model 模型
* @param temperature 温度
* @param customPrompt 提示词
* @returns {Promise<{success: boolean, aiResponse: string}|{}>}
*/
async callAi({ prompt, chatHistory = [], model, temperature, customPrompt }) { async callAi({ prompt, chatHistory = [], model, temperature, customPrompt }) {
if (!this.openai) { if (!this.openai) {
logger.err('ai未初始化..'); logger.err('ai未初始化..');
return {}; return { success: false };
} }
let systemMessage = { role: 'system', content: customPrompt || '' }; let systemMessage = {
const messages = [systemMessage, ...chatHistory, { role: 'user', content: prompt }]; role: 'system',
content: customPrompt || '',
};
const messages = [
systemMessage,
...chatHistory,
{
role: 'user',
content: prompt,
},
];
try { try {
const completion = await this.openai.chat.completions.create({ const completion = await this.openai.chat.completions.create({
@ -37,8 +59,9 @@ class ChatTools {
}; };
} catch (err) { } catch (err) {
logger.err(err); logger.err(err);
return {}; return { success: false };
} }
} }
} }
export default ChatTools;
export default OpenaiChat;

View File

@ -16,6 +16,7 @@
"author": "Jerry", "author": "Jerry",
"License": "MIT", "License": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.8.4",
"chalk": "^5.4.1", "chalk": "^5.4.1",
"openai": "^4.89.0" "openai": "^4.89.0"
}, },

1101
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff