-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (205 loc) · 6.25 KB
/
index.js
File metadata and controls
225 lines (205 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const axios = require("axios");
const { get } = require("lodash");
const { G4F } = require("g4f");
const getGPT4js = require("gpt4js");
const OpenAI = require("openai");
const { OpenAIApi: AzureOpenAIApi, Configuration } = require("azure-openai");
const {
GoogleGenerativeAI,
HarmBlockThreshold,
HarmCategory,
} = require("@google/generative-ai");
const defaultLLMConfig = require("./llm");
class AIInterface {
constructor(config) {
this.config = config;
this.llmConfig = this.getLLMConfig();
}
getLLMConfig() {
const provider = this.config.provider.toLowerCase();
let llmConfig = { ...defaultLLMConfig.default, ...this.config.default };
const providerConfigs = {
moonshot: { ...defaultLLMConfig.moonshot, ...this.config.moonshot },
openai: { ...defaultLLMConfig.openai, ...this.config.openai },
azure: { ...defaultLLMConfig.azure, ...this.config.azure },
gemini: { ...defaultLLMConfig.gemini, ...this.config.gemini },
g4f: { ...defaultLLMConfig.g4f, ...this.config.g4f },
gpt4js: { ...defaultLLMConfig.gpt4js, ...this.config.gpt4js },
other: { ...defaultLLMConfig.otherAI, ...this.config.otherAI },
deepseek: this.config.deepseek, // add deepseek config
};
if (provider in providerConfigs) {
const providerConfig = providerConfigs[provider];
if (providerConfig) {
llmConfig = { ...llmConfig, ...providerConfig };
}
} else {
throw new Error("LLM provider is not set in the config file.");
}
return llmConfig;
}
async callAIInterface(prompt) {
let content = "";
const provider = this.config.provider.toLowerCase();
switch (provider) {
case "g4f":
content = await this.callG4F(prompt);
break;
case "gpt4js":
content = await this.callGPT4JS(prompt);
break;
case "gemini":
content = await this.callGemini(prompt);
break;
case "azure":
content = await this.callAzure(prompt);
break;
case "other":
content = await this.callOther(prompt);
break;
case "deepseek":
content = await this.callDeepseek(prompt);
break;
default:
content = await this.callOpenAI(prompt);
}
return content ? content.replace("\n", "") : "";
}
async callG4F(prompt) {
const g4f = new G4F();
const modelName = this.llmConfig.modelName;
const messages = [{ role: "user", content: prompt }];
try {
return await g4f.chatCompletion(messages, { model: modelName });
} catch (e) {
console.log(`Sorry, there was an error calling g4f.`, e);
return "";
}
}
async callGPT4JS(prompt) {
const providerName = this.llmConfig?.provider || "Nextway";
const gpt4js = await getGPT4js();
const provider = gpt4js.createProvider(providerName);
const modelName = this.llmConfig.modelName;
const messages = [{ role: "user", content: prompt }];
try {
return await provider.chatCompletion(
messages,
{
provider: providerName,
model: modelName,
},
(data) => {}
);
} catch (e) {
console.log(`Sorry, there was an error calling gpt4js.`, e);
return "";
}
}
async callGemini(prompt) {
const generationConfig = {
temperature: 0.5,
top_p: 1,
top_k: 1,
max_output_tokens: 2048,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
const genAI = new GoogleGenerativeAI(this.llmConfig.apiKey);
const model = genAI.getGenerativeModel({
model: this.llmConfig.modelName || "",
generationConfig,
safetySettings,
});
const chat = model.startChat();
try {
const result = await chat.sendMessage(prompt);
return result.response.text();
} catch (e) {
console.log(`Sorry, there was an error calling gemini.`, e);
return "";
}
}
async callAzure(prompt) {
const client = new AzureOpenAIApi(
new Configuration({
apiKey: this.llmConfig.apiKey,
basePath: this.llmConfig.baseUrl,
})
);
const response = await client.createCompletion({
model: this.llmConfig.modelName,
prompt: prompt,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
return response.data.choices[0].text.trim();
}
async callOther(prompt) {
try {
const data = { message: prompt, ...this.llmConfig.data };
const response = await axios.post(
this.llmConfig.baseUrl,
data,
this.llmConfig.requestConfig
);
if (response && response.data) {
const responsePath =
this.llmConfig.responsePath || "data.choices[0].message.content";
return get(response.data, responsePath, "");
}
return "";
} catch (error) {
console.error("Error making other API call:", error);
throw error;
}
}
async callOpenAI(prompt) {
console.log(this.config.provider, JSON.stringify(this.llmConfig));
const client = new OpenAI({
apiKey: this.llmConfig.apiKey,
baseURL: this.llmConfig.baseUrl,
});
const response = await client.chat.completions.create({
model: this.llmConfig.modelName,
messages: [{ role: "user", content: prompt }],
});
if (response) {
return response.choices[0]?.message?.content || "";
}
return "";
}
async callDeepseek(prompt) {
const client = new OpenAI({
apiKey: this.llmConfig.apiKey,
baseURL: this.llmConfig.baseUrl,
});
const response = await client.chat.completions.create({
model: this.llmConfig.modelName,
messages: [{ role: "user", content: prompt }],
});
if (response) {
return response.choices[0]?.message?.content || "";
}
return "";
}
}
module.exports = AIInterface;