-
Notifications
You must be signed in to change notification settings - Fork 19
/
chatService.js
47 lines (39 loc) · 1.42 KB
/
chatService.js
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
const OpenAI = require('openai');
const Repository = require('./repository');
require('dotenv').config();
const openai = new OpenAI(process.env.OPENAI_API_KEY);
async function interactWithRepository(uuid, userMessage) {
try {
const repository = await Repository.findOne({ uuid: uuid }).exec();
if (!repository || !repository.isProcessed) {
throw new Error('Repository not found or not processed yet.');
}
// gpt_pilot_debugging_log
console.log('Repository file summaries:', repository.fileSummaries);
const systemMessage = {
role: 'system',
content: `This is a summary of the project: ${repository.summary}`
};
const userMessageObj = {
role: 'user',
content: userMessage
};
const response = await openai.chat.completions.create({
model: 'gpt-4-turbo-preview',
messages: [systemMessage, ...(repository.fileSummaries || []).map(summary => ({ role: 'system', content: summary })), userMessageObj],
max_tokens: 1024,
temperature: 0.5
});
if (!response.choices || response.choices.length === 0 || !response.choices[0].message) {
throw new Error('Invalid response from OpenAI');
}
return response.choices[0].message.content.trim();
} catch (error) {
// gpt_pilot_debugging_log
console.error('Error in interactWithRepository:', error.message, error.stack);
throw error;
}
}
module.exports = {
interactWithRepository
};