-
Notifications
You must be signed in to change notification settings - Fork 21
/
chatService.js
44 lines (36 loc) · 1.33 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
const OpenAI = require('openai');
const Repository = require('./repository');
async function interactWithRepository(uuid, userMessage, apiKey) {
try {
const repository = await Repository.findOne({ uuid: uuid }).exec();
if (!repository || !repository.isProcessed) {
throw new Error('Repository not found or not processed yet.');
}
console.log('Repository file summaries:', repository.fileSummaries);
const openai = new OpenAI({ apiKey });
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) {
console.error('Error in interactWithRepository:', error.message, error.stack);
throw error;
}
}
module.exports = {
interactWithRepository
};