-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
521 lines (434 loc) · 17.6 KB
/
Copy pathserver.js
File metadata and controls
521 lines (434 loc) · 17.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
// AI API endpoint
app.post('/api/ai/generate-answers', async (req, res) => {
try {
const { question } = req.body;
if (!question || question.trim() === '') {
return res.status(400).json({ error: 'Question is required' });
}
// Get AI service type from environment (default to 'gemini')
const aiService = process.env.AI_SERVICE_TYPE || 'gemini';
console.log(`\n=== AI Request ===`);
console.log(`Service: ${aiService}`);
console.log(`Question: ${question.substring(0, 100)}...`);
let answers = [];
if (aiService === 'ollama') {
answers = await generateOllamaAnswers(question);
} else {
// Only require API key for external services
const apiKey = process.env.AI_API_KEY;
if (!apiKey) {
console.error('ERROR: AI_API_KEY not found in environment');
return res.status(500).json({
error: 'AI API key not configured. Please set AI_API_KEY in .env file'
});
}
if (aiService === 'gemini') {
console.log('Calling Gemini API...');
answers = await generateGeminiAnswers(question, apiKey);
} else if (aiService === 'openai') {
console.log('Calling OpenAI API...');
answers = await generateOpenAIAnswers(question, apiKey);
} else if (aiService === 'anthropic') {
console.log('Calling Anthropic API...');
answers = await generateAnthropicAnswers(question, apiKey);
} else if (aiService === 'cerebras') {
console.log('Calling Cerebras AI...');
answers = await generateCerebrasAnswers(question, apiKey);
} else {
return res.status(400).json({ error: 'Invalid AI service type. Use: ollama, gemini, openai, anthropic, or cerebras' });
}
}
if (!answers || answers.length === 0) {
console.error('ERROR: No answers generated');
return res.status(500).json({ error: 'Failed to generate answers' });
}
// Ensure we have exactly 3 answers
while (answers.length < 3) {
answers.push(answers[answers.length - 1] || 'No additional answer available');
}
console.log(`✓ Successfully generated ${answers.length} answers`);
console.log('=== End Request ===\n');
res.json({ answers: answers.slice(0, 3) });
} catch (error) {
console.error('\n!!! ERROR in generate-answers endpoint !!!');
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
console.error('Error details:', error);
console.error('=== End Error ===\n');
res.status(500).json({
error: 'Failed to generate answers',
message: error.message,
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
// Ollama (Local AI) implementation
async function generateOllamaAnswers(question) {
const ollamaUrl = process.env.OLLAMA_URL || 'http://localhost:11434';
const model = process.env.OLLAMA_MODEL || 'llama2'; // or 'mistral', 'codellama', etc.
const prompt = `Please provide exactly 3 different, well-written answers to the following question. Format your response as follows:
Answer 1: [first answer here]
Answer 2: [second answer here]
Answer 3: [third answer here]
Make each answer distinct, complete, and well-structured.
Question: ${question}`;
try {
const response = await fetch(`${ollamaUrl}/api/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: model,
prompt: prompt,
stream: false,
options: {
temperature: 0.7,
num_predict: 1000
}
})
});
if (!response.ok) {
if (response.status === 404) {
throw new Error(`Model "${model}" not found. Please install it with: ollama pull ${model}`);
}
throw new Error(`Ollama API error: ${response.statusText}`);
}
const data = await response.json();
const aiResponse = data.response || '';
// Parse the response into 3 answers
return parseAnswers(aiResponse);
} catch (error) {
if (error.message.includes('fetch') || error.code === 'ECONNREFUSED') {
throw new Error('Cannot connect to Ollama. Make sure Ollama is installed and running. Download from https://ollama.ai');
}
throw error;
}
}
// Shared parsing function for all AI responses
function parseAnswers(response) {
let answers = [];
// Method 1: Look for "Answer 1:", "Answer 2:", "Answer 3:" pattern
const answerPattern = /Answer\s*[123]:?\s*(.+?)(?=Answer\s*[123]:|$)/gis;
const matches = [...response.matchAll(answerPattern)];
if (matches.length >= 3) {
answers = matches.slice(0, 3).map(m => m[1].trim()).filter(a => a.length > 10);
}
// Method 2: Look for numbered lists (1., 2., 3.)
if (answers.length < 3) {
const numberedPattern = /^\s*[123]\.\s*(.+?)(?=^\s*[123]\.|$)/gims;
const numberedMatches = [...response.matchAll(numberedPattern)];
if (numberedMatches.length >= 3) {
answers = numberedMatches.slice(0, 3).map(m => m[1].trim()).filter(a => a.length > 10);
}
}
// Method 3: Split by double newlines (paragraphs)
if (answers.length < 3) {
const paragraphs = response.split(/\n\n+/).filter(p => {
const trimmed = p.trim();
return trimmed.length > 20 && !trimmed.match(/^(Answer|Option)\s*[123]/i);
});
if (paragraphs.length >= 3) {
answers = paragraphs.slice(0, 3).map(p => p.trim());
}
}
// Method 4: Split by single newlines if they're substantial
if (answers.length < 3) {
const lines = response.split('\n').filter(line => {
const trimmed = line.trim();
return trimmed.length > 30 && !trimmed.match(/^(Answer|Option|Question)/i);
});
if (lines.length >= 3) {
answers = lines.slice(0, 3).map(l => l.trim());
}
}
// If still no good answers, use the whole response split intelligently
if (answers.length === 0) {
const fallback = response.trim();
if (fallback.length > 50) {
// Split into roughly equal parts
const partLength = Math.floor(fallback.length / 3);
answers = [
fallback.substring(0, partLength).trim(),
fallback.substring(partLength, partLength * 2).trim(),
fallback.substring(partLength * 2).trim()
].filter(a => a.length > 10);
}
}
// Ensure we have at least 3 answers (duplicate if needed)
while (answers.length < 3 && answers.length > 0) {
answers.push(answers[answers.length - 1]);
}
// If still no answers, return error
if (answers.length === 0) {
throw new Error('Could not parse answers from AI response');
}
return answers.slice(0, 3);
}
// OpenAI implementation
async function generateOpenAIAnswers(question, apiKey) {
let OpenAI;
try {
OpenAI = require('openai');
} catch (error) {
throw new Error('OpenAI package not installed. Run: npm install openai');
}
const openai = new OpenAI({ apiKey });
const prompt = `Please provide exactly 3 different, well-written answers to the following question. Format your response as follows:
Answer 1: [first answer here]
Answer 2: [second answer here]
Answer 3: [third answer here]
Make each answer distinct, complete, and well-structured.
Question: ${question}`;
let completion;
try {
completion = await openai.chat.completions.create({
model: process.env.OPENAI_MODEL || 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: 'You are a helpful assistant. Always format your responses with "Answer 1:", "Answer 2:", and "Answer 3:" labels.' },
{ role: 'user', content: prompt }
],
temperature: 0.7,
max_tokens: 1500
});
} catch (apiError) {
if (apiError.status === 429) {
throw new Error('OpenAI API quota exceeded. Please check your billing and plan at https://platform.openai.com/account/billing');
} else if (apiError.status === 401) {
throw new Error('Invalid OpenAI API key. Please check your API key in the .env file.');
} else if (apiError.status === 402) {
throw new Error('OpenAI account payment required. Please add a payment method at https://platform.openai.com/account/billing');
} else {
throw new Error(`OpenAI API error: ${apiError.message || 'Unknown error'}`);
}
}
const response = completion.choices[0].message.content;
console.log('OpenAI Response:', response.substring(0, 200) + '...');
return parseAnswers(response);
}
// Shared parsing function for all AI responses
function parseAnswers(response) {
let answers = [];
// Method 1: Look for "Answer 1:", "Answer 2:", "Answer 3:" pattern
const answerPattern = /Answer\s*[123]:?\s*(.+?)(?=Answer\s*[123]:|$)/gis;
const matches = [...response.matchAll(answerPattern)];
if (matches.length >= 3) {
answers = matches.slice(0, 3).map(m => m[1].trim()).filter(a => a.length > 10);
}
// Method 2: Look for numbered lists (1., 2., 3.)
if (answers.length < 3) {
const numberedPattern = /^\s*[123]\.\s*(.+?)(?=^\s*[123]\.|$)/gims;
const numberedMatches = [...response.matchAll(numberedPattern)];
if (numberedMatches.length >= 3) {
answers = numberedMatches.slice(0, 3).map(m => m[1].trim()).filter(a => a.length > 10);
}
}
// Method 3: Split by double newlines (paragraphs)
if (answers.length < 3) {
const paragraphs = response.split(/\n\n+/).filter(p => {
const trimmed = p.trim();
return trimmed.length > 20 && !trimmed.match(/^(Answer|Option)\s*[123]/i);
});
if (paragraphs.length >= 3) {
answers = paragraphs.slice(0, 3).map(p => p.trim());
}
}
// Method 4: Split by single newlines if they're substantial
if (answers.length < 3) {
const lines = response.split('\n').filter(line => {
const trimmed = line.trim();
return trimmed.length > 30 && !trimmed.match(/^(Answer|Option|Question)/i);
});
if (lines.length >= 3) {
answers = lines.slice(0, 3).map(l => l.trim());
}
}
// If still no good answers, use the whole response split intelligently
if (answers.length === 0) {
const fallback = response.trim();
if (fallback.length > 50) {
// Split into roughly equal parts
const partLength = Math.floor(fallback.length / 3);
answers = [
fallback.substring(0, partLength).trim(),
fallback.substring(partLength, partLength * 2).trim(),
fallback.substring(partLength * 2).trim()
].filter(a => a.length > 10);
}
}
// Ensure we have at least 3 answers (duplicate if needed)
while (answers.length < 3 && answers.length > 0) {
answers.push(answers[answers.length - 1]);
}
// If still no answers, return error
if (answers.length === 0) {
throw new Error('Could not parse answers from AI response');
}
return answers.slice(0, 3);
}
// Anthropic Claude implementation
async function generateAnthropicAnswers(question, apiKey) {
let Anthropic;
try {
Anthropic = require('@anthropic-ai/sdk');
} catch (error) {
throw new Error('Anthropic SDK not installed. Run: npm install @anthropic-ai/sdk');
}
const anthropic = new Anthropic({ apiKey });
const prompt = `Please provide exactly 3 different, well-written answers to the following question. Format your response as follows:
Answer 1: [first answer here]
Answer 2: [second answer here]
Answer 3: [third answer here]
Make each answer distinct, complete, and well-structured.
Question: ${question}`;
let message;
try {
message = await anthropic.messages.create({
model: process.env.ANTHROPIC_MODEL || 'claude-3-sonnet-20240229',
max_tokens: 1500,
messages: [
{ role: 'user', content: prompt }
]
});
} catch (apiError) {
if (apiError.status === 429) {
throw new Error('Anthropic API rate limit exceeded. Please try again later or check your plan.');
} else if (apiError.status === 401) {
throw new Error('Invalid Anthropic API key. Please check your API key in the .env file.');
} else if (apiError.status === 402) {
throw new Error('Anthropic account payment required. Please add a payment method.');
} else {
throw new Error(`Anthropic API error: ${apiError.message || 'Unknown error'}`);
}
}
const response = message.content[0].text;
return parseAnswers(response);
}
// Cerebras AI implementation (OpenAI-compatible API)
async function generateCerebrasAnswers(question, apiKey) {
let OpenAI;
try {
OpenAI = require('openai');
} catch (error) {
throw new Error('OpenAI package not installed. Run: npm install openai');
}
const cerebras = new OpenAI({
apiKey: apiKey,
baseURL: 'https://api.cerebras.ai/v1'
});
const prompt = `Please provide exactly 3 different, well-written answers to the following question. Format your response as follows:
Answer 1: [first answer here]
Answer 2: [second answer here]
Answer 3: [third answer here]
Make each answer distinct, complete, and well-structured.
Question: ${question}`;
let completion;
try {
completion = await cerebras.chat.completions.create({
model: process.env.CEREBRAS_MODEL || 'llama3.1-8b',
messages: [
{ role: 'system', content: 'You are a helpful assistant. Always format your responses with "Answer 1:", "Answer 2:", and "Answer 3:" labels.' },
{ role: 'user', content: prompt }
],
temperature: 0.7,
max_tokens: 1500
});
} catch (apiError) {
console.error('Cerebras API Error Details:', apiError);
console.error('Status:', apiError.status);
console.error('Response:', apiError.response?.data);
if (apiError.status === 429) {
throw new Error('Cerebras API rate limit exceeded. Please try again later.');
} else if (apiError.status === 401 || apiError.status === 403) {
throw new Error('Invalid Cerebras API key. Please check your API key in the .env file.');
} else if (apiError.status === 402) {
throw new Error('Cerebras account payment required. Please check your account.');
} else if (apiError.message) {
throw new Error(`Cerebras API error: ${apiError.message}`);
} else {
throw new Error(`Cerebras API error: ${JSON.stringify(apiError.response?.data || 'Unknown error')}`);
}
}
const response = completion.choices[0].message.content;
console.log('Cerebras Response:', response.substring(0, 200) + '...');
return parseAnswers(response);
}
// Google Gemini implementation
async function generateGeminiAnswers(question, apiKey) {
const { GoogleGenerativeAI } = require('@google/generative-ai');
const genAI = new GoogleGenerativeAI(apiKey);
// Use the model name without the version prefix
const modelName = process.env.GEMINI_MODEL || 'gemini-1.5-flash';
console.log(`Using Gemini model: ${modelName}`);
const model = genAI.getGenerativeModel({
model: modelName
});
const prompt = `Please provide exactly 3 different, well-written answers to the following question. Format your response as follows:
Answer 1: [first answer here]
Answer 2: [second answer here]
Answer 3: [third answer here]
Make each answer distinct, complete, and well-structured.
Question: ${question}`;
let result;
try {
// Use generateContent method
result = await model.generateContent(prompt);
// Check if the response was blocked or empty
if (!result || !result.response) {
throw new Error('No response received from Gemini API');
}
const response = result.response;
// Check for safety ratings that might block the content
if (response.promptFeedback && response.promptFeedback.blockReason) {
throw new Error(`Content blocked by Gemini: ${response.promptFeedback.blockReason}`);
}
} catch (apiError) {
console.error('Gemini API Error Details:', apiError);
// Handle different error types
if (apiError.message && apiError.message.includes('blocked')) {
throw apiError;
} else if (apiError.message && apiError.message.includes('API key')) {
throw new Error('Invalid or inactive Gemini API key. Please verify your API key at https://makersuite.google.com/app/apikey');
} else if (apiError.status === 404) {
throw new Error(`Model ${modelName} not found. Try using: gemini-pro, gemini-1.5-pro, gemini-1.5-flash-latest, or gemini-1.0-pro`);
} else if (apiError.status === 429) {
throw new Error('Gemini API rate limit exceeded. Please try again later or check your quota.');
} else if (apiError.status === 401 || apiError.status === 403) {
throw new Error('Invalid Gemini API key. Please check your API key in the .env file.');
} else if (apiError.status === 400) {
throw new Error(`Gemini API error: ${apiError.message || 'Invalid request'}`);
} else if (apiError.message) {
throw new Error(`Gemini API error: ${apiError.message}`);
} else {
throw new Error(`Gemini API error: Unknown error occurred`);
}
}
try {
const response = result.response;
const text = response.text();
if (!text || text.trim().length === 0) {
throw new Error('Empty response received from Gemini');
}
console.log('Gemini Response:', text.substring(0, 200) + '...');
return parseAnswers(text);
} catch (parseError) {
console.error('Error parsing Gemini response:', parseError);
throw new Error(`Failed to parse Gemini response: ${parseError.message}`);
}
}
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', service: 'text-to-handwriting-api' });
});
app.listen(PORT, () => {
console.log(`AI Backend server running on http://localhost:${PORT}`);
console.log(`AI Service Type: ${process.env.AI_SERVICE_TYPE || 'gemini'}`);
});