-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend_dummy.ts
124 lines (100 loc) · 3.42 KB
/
Backend_dummy.ts
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
import * as functions from 'firebase-functions';
import { OpenAI } from 'openai';
import * as admin from 'firebase-admin';
// Initialize Firebase Admin SDK
admin.initializeApp();
// Initialize OpenAI
const openai = new OpenAI({
apiKey: functions.config().openai.key
});
// Interface for chat message
interface ChatMessage {
role: 'user' | 'assistant';
content: string;
}
// Interface for chat history
interface ChatHistory {
messages: ChatMessage[];
}
// Function to get chat history
async function getChatHistory(userId: string): Promise<ChatHistory> {
try {
const doc = await admin.firestore().collection('chatHistories').doc(userId).get();
if (doc.exists) {
return doc.data() as ChatHistory;
}
return { messages: [] };
} catch (error) {
console.error('Error fetching chat history:', error);
return { messages: [] };
}
}
// Function to update chat history
async function updateChatHistory(userId: string, history: ChatHistory): Promise<void> {
try {
await admin.firestore().collection('chatHistories').doc(userId).set(history);
} catch (error) {
console.error('Error updating chat history:', error);
}
}
// Main chatbot function
export const chatbot = functions.https.onRequest(async (request, response) => {
try {
console.log('Received request:', request.body);
const { message, userId } = request.body;
if (!message || typeof message !== 'string') {
throw new Error('Invalid or missing message');
}
if (!userId || typeof userId !== 'string') {
throw new Error('Invalid or missing userId');
}
// Get chat history
const history = await getChatHistory(userId);
// Add user message to history
history.messages.push({ role: 'user', content: message });
// Prepare messages for OpenAI
const aiMessages = history.messages.slice(-5); // Only use last 5 messages
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: aiMessages,
max_tokens: 150
});
console.log('OpenAI response:', completion.choices[0].message);
const aiReply = completion.choices[0].message.content || '';
// Add AI response to history
history.messages.push({ role: 'assistant', content: aiReply });
// Update chat history
await updateChatHistory(userId, history);
response.json({ reply: aiReply });
} catch (error) {
console.error('Error processing request:', error);
if (error instanceof Error) {
response.status(500).json({
error: 'An error occurred while processing your request.',
details: error.message
});
} else {
response.status(500).json({
error: 'An unexpected error occurred.',
details: 'Unknown error type'
});
}
}
});
// Function to clear chat history
export const clearChatHistory = functions.https.onRequest(async (request, response) => {
try {
const { userId } = request.body;
if (!userId || typeof userId !== 'string') {
throw new Error('Invalid or missing userId');
}
await admin.firestore().collection('chatHistories').doc(userId).delete();
response.json({ message: 'Chat history cleared successfully' });
} catch (error) {
console.error('Error clearing chat history:', error);
response.status(500).json({
error: 'An error occurred while clearing chat history.',
details: error instanceof Error ? error.message : 'Unknown error type'
});
}
});