-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-manager.js
More file actions
190 lines (166 loc) · 5.98 KB
/
api-manager.js
File metadata and controls
190 lines (166 loc) · 5.98 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
/**
* API Manager for Grammar Corrector Extension
*
* Bu dosya, farklı AI API'leri ile iletişim kurma işlemlerini yönetir.
* Yeni bir API eklemek için bir fonksiyon ekleyin ve apiHandlers nesnesine kaydedin.
*/
// Desteklenen API türleri
const API_TYPES = {
OPENAI: 'openai',
GEMINI: 'gemini',
DEEPSEEK: 'deepseek'
};
// Her API türü için işleyiciler
const apiHandlers = {
// OpenAI (ChatGPT) API işleyicisi
[API_TYPES.OPENAI]: async (text, apiKey) => {
try {
console.log('Calling ChatGPT API with text:', text);
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: 'You are a helpful assistant that corrects grammar. Only return the corrected text without any additional explanation.'
},
{
role: 'user',
content: `Correct the grammar in the following text, but preserve the meaning and style: "${text}"`
}
],
temperature: 0.3,
max_tokens: 150
})
});
const data = await response.json();
console.log('OpenAI API response:', data);
if (data.error) {
throw new Error(data.error.message || 'Unknown OpenAI API error');
}
// Extract only the corrected text from the response
const correctedText = data.choices[0].message.content.trim();
console.log('Corrected text from OpenAI:', correctedText);
return correctedText;
} catch (error) {
console.error('Error calling ChatGPT API:', error);
throw error;
}
},
// Google Gemini API işleyicisi
[API_TYPES.GEMINI]: async (text, apiKey) => {
try {
console.log('Calling Gemini API with text:', text);
// API endpoint
const apiEndpoint = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent?key=${apiKey}`;
console.log('API endpoint:', apiEndpoint.replace(apiKey, apiKey.substring(0, 3) + "..."));
const requestBody = {
contents: [
{
parts: [
{
text: `Correct the grammar in the following text, but preserve the meaning and style. Only return the corrected text without any additional explanation: "${text}"`
}
]
}
],
generationConfig: {
temperature: 0.2,
maxOutputTokens: 256
}
};
console.log('Request body:', JSON.stringify(requestBody));
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
console.log('Response status:', response.status);
console.log('Response status text:', response.statusText);
const data = await response.json();
console.log('Gemini API response:', JSON.stringify(data, null, 2));
if (data.error) {
throw new Error(data.error.message || 'Unknown Gemini API error');
}
if (!data.candidates || !data.candidates[0] || !data.candidates[0].content) {
throw new Error('Unexpected response from Gemini API');
}
// Extract only the corrected text from the response
const content = data.candidates[0].content;
const textParts = content.parts.filter(part => part.text).map(part => part.text);
const correctedText = textParts.join(' ').trim();
console.log('Corrected text from Gemini:', correctedText);
return correctedText;
} catch (error) {
console.error('Error calling Gemini API:', error);
throw error;
}
},
// Deepseek API işleyicisi
[API_TYPES.DEEPSEEK]: async (text, apiKey) => {
try {
console.log('Calling Deepseek API with text:', text);
const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{
role: 'system',
content: 'You are a helpful assistant that corrects grammar. Only return the corrected text without any additional explanation.'
},
{
role: 'user',
content: `Correct the grammar in the following text, but preserve the meaning and style: "${text}"`
}
],
temperature: 0.3,
max_tokens: 200
})
});
const data = await response.json();
console.log('Deepseek API response:', data);
if (data.error) {
throw new Error(data.error.message || 'Unknown Deepseek API error');
}
// Extract only the corrected text from the response
const correctedText = data.choices[0].message.content.trim();
console.log('Corrected text from Deepseek:', correctedText);
return correctedText;
} catch (error) {
console.error('Error calling Deepseek API:', error);
throw error;
}
}
};
/**
* Ana API işleyici fonksiyonu
* @param {string} text - Düzeltilecek metin
* @param {string} apiType - Kullanılacak API türü (openai, gemini, vs.)
* @param {string} apiKey - API anahtarı
* @returns {Promise<string>} - Düzeltilmiş metin
*/
async function correctGrammarWithAPI(text, apiType, apiKey) {
// API türünü kontrol et
if (!apiHandlers[apiType]) {
throw new Error(`Desteklenmeyen API türü: ${apiType}`);
}
// İlgili API işleyicisini çağır
return await apiHandlers[apiType](text, apiKey);
}
// API işlevlerini dışa aktar
export {
correctGrammarWithAPI,
API_TYPES
};