-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatbot For-PDFs-csv-JSON-txt.js
142 lines (130 loc) · 3.92 KB
/
Chatbot For-PDFs-csv-JSON-txt.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
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
const dialogflow = require('dialogflow');
const uuid = require('uuid');
const pdfParse = require('pdf-parse');
const csvParse = require('csv-parser');
const JSONParse = require('JSON.parse');
const multer = require('multer');
// Set up Dialogflow client
const projectId = 'YOUR_PROJECT_ID';
const privateKey = require('path/to/private/key.json');
const sessionClient = new dialogflow.SessionsClient({
projectId: projectId,
credentials: {
private_key: privateKey,
client_email: 'YOUR_CLIENT_EMAIL'
}
});
// Set up file upload middleware
const upload = multer({ dest: 'uploads/' });
// Process uploaded files
const files = {};
const processPDF = (file) => {
const data = pdfParse(file.buffer);
files[file.filename] = data.text;
};
const processCSV = (file) => {
const data = [];
csvParse()
.on('data', (row) => data.push(row))
.on('end', () => files[file.filename] = data);
file.pipe(csvParser);
};
const processJSON = (file) => {
const data = JSONParse(file.buffer.toString());
files[file.filename] = data;
};
const processTXT = (file) => {
files[file.filename] = file.buffer.toString();
};
// Handle chat requests
app.post('/chat', upload.single('file'), async (req, res) => {
const sessionId = uuid.v4();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
const query = req.body.message;
// Detect intent and extract parameters
const request = {
session: sessionPath,
queryInput: {
text: _Validate request if(!req.body.message) {
throw new Error('Message is required') {
text: query,
languageCode: 'en-US'
}
}
};
const response = await sessionClient.detectIntent(request);
const intent = response.queryResult.intent.displayName;
const fileId = response.queryResult.parameters.fileId;
// Return results based on intent and parameters
let result;
if (intent === 'real estate law files') {
result = searchRealEstateLaw(files[fileId]);
} else if (intent === 'phone call transcript files') {
result = searchPhoneCallTranscripts(files[fileId]);
} else {
result = 'Sorry, I don\'t understand.';
}
res.send(result);
} catch (error) {
console.error(error);
res.status(400).send(error.message);
}
});
// Helper functions for searching files
// Helper function for searching real estate law files
const searchRealEstateLaw = (fileContents) => {
// Define search terms
const searchTerms = [
'real estate',
'property law',
'landlord-tenant',
'homeowners association',
'zoning',
'construction law'
];
// Split file contents into words
const words = fileContents.split(/[^\w]/);
// Check for presence of search terms
const results = [];
for (const term of searchTerms) {
if (words.includes(term)) {
results.push(term);
}
}
// Return results
if (results.length > 0) {
return `The file contains the following real estate law topics: ${results.join(', ')}`;
} else {
return 'The file does not appear to contain any real estate law topics';
}
};
// Helper function for searching phone call transcript files
const searchPhoneCallTranscripts = (fileContents) => {
// Define search terms
const searchTerms = [
'customer service',
'sales',
'complaints',
'product inquiries',
'technical issues',
'billing'
];
// Split file contents into lines
const lines = fileContents.split('\n');
// Check for presence of search terms in each line
const results = [];
for (const line of lines) {
for (const term of searchTerms) {
if (line.toLowerCase().includes(term)) {
results.push(line.trim());
break;
}
}
}
// Return results
if (results.length > 0) {
return `The file contains the following phone call topics: ${results.join(' | ')}`;
} else {
return 'The file does not appear to contain any phone call topics';
}
};