-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
274 lines (240 loc) · 8.47 KB
/
index.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
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
/**
* AI SDR: Searches for contacts in a selected cell range
* And returns nicely formatted results
* Author: Philipp Tsipman
*/
// INPUTS
// Contact info to search for
const INFO =
"Full Name, Email, Full LinkedIn Web URL, Twitter Handle, Employer's Name, Job Title, Employer's Website, and Contact's Bio";
const OPENAI_API_KEY = ""; //ADD YOUR OPEN AI API KEY HERE
const PERPLEXITY_API_KEY = ""; //ADD YOUR PERPLEXITY API KEY HERE
const GOOGLE_API_KEY = ""; //ADD YOUR GOOGLE API KEY HERE
const CUSTOM_SEARCH_ENGINE = ""; //ADD YOUR GOOGLE CUSTOM SEARCH ENGINE ID HERE. Takes a minute to create one: https://programmablesearchengine.google.com/controlpanel/all
// ------------------------------ //
// CONSTANTS
const SEARCH_SYSTEM_PROMPT =
"Respond in this format: A bulleted list: " +
INFO +
". If certain information is not available, leave that part blank. No not write (not found in search results). If you don't find information on the first try, try again. You can do it.";
const SEARCH_QUERY_PROMPT = "who is ";
const FORMAT_SYSTEM_PROMPT =
"Format this text precisely in this format: A bulleted list: " +
INFO +
". If certain information is not available, leave that part blank. Do not write 'Not available' or 'Not provided'. Provide URLs as is, do not format them as markdown. If you can't do it on the first try, try again. You can do it.";
// Default = PERPLEXITY_70_MODEL for smarts and OPEN_AI_35_MODEL for speed
const OPENAI_35_MODEL = "gpt-3.5-turbo-1106";
const OPENAI_4_MODEL = "gpt-4-1106-preview";
const PERPLEXITY_7_MODEL = "pplx-7b-online";
const PERPLEXITY_70_MODEL = "pplx-70b-online";
const MAX_TOKENS = 400;
// FUNCTIONS
function cleanContact(str) {
if (
str.toLowerCase().includes("not provided") ||
str.toLowerCase().includes("not available") ||
str.toLowerCase().includes("not applicable")
) {
return "";
}
// Fake URL
if (str.match(/\[[a-zA-Z\s]+\]\([a-zA-Z\s]+(URL|Website)\)/i)) {
return ""
}
// Clean URL
if (str.match(/\[[a-zA-Z\d*\',\s]+\]\(((https?:?\/\/)?[^\s)]+)\)/)) {
return str.match(/\[[a-zA-Z\d*\',\s]+\]\(((https?:?\/\/)?[^\s)]+)\)/)[1];
}
return str;
}
// Format contact into a JSON object
function formatContact(contact) {
const contactArray = contact.split("\n");
let foundContact = {};
contactArray.forEach((line) => {
const lineArray = line.split(": ");
// If the string contains: "Full Name: ", then split by ": " and save the second part as full_name key in foundContact
if (line.includes("Full Name: ")) {
foundContact["full_name"] = cleanContact(lineArray[1]);
} else if (line.includes("Email: ")) {
foundContact["email"] = cleanContact(lineArray[1]);
} else if (line.includes("Full LinkedIn Web URL: ")) {
foundContact["linkedin"] = cleanContact(lineArray[1]);
} else if (line.includes("Twitter Handle: ")) {
foundContact["twitter"] = cleanContact(lineArray[1]);
} else if (line.includes("Employer's Name: ")) {
foundContact["employer"] = cleanContact(lineArray[1]);
} else if (line.includes("Job Title: ")) {
foundContact["job_title"] = cleanContact(lineArray[1]);
} else if (line.includes("Employer's Website: ")) {
foundContact["employer_website"] = cleanContact(lineArray[1]);
} else if (line.includes("Contact's Bio: ")) {
foundContact["bio"] = cleanContact(lineArray[1]);
}
});
console.log(foundContact);
return foundContact;
}
// Google search
async function googleSearch(
query = "Philipp Tsipman",
siteSearch = "twitter.com"
) {
const url =
"https://www.googleapis.com/customsearch/v1?" +
"key=" +
GOOGLE_API_KEY +
"&cx=" +
CUSTOM_SEARCH_ENGINE +
"&q=" +
query +
"&num=1" +
"&siteSearch=" +
siteSearch +
"&siteSearchFilter=i";
try {
const response = JSON.parse(UrlFetchApp.fetch(url).getContentText());
let data = "";
if (response && response.items && response.items.length > 0) {
data = response.items[0].link;
}
console.log(data);
return data;
} catch (error) {
console.log(error);
return error;
}
}
// LLM API call
async function llm(
url = "https://api.perplexity.ai/chat/completions",
model = PERPLEXITY_70_MODEL,
key = PERPLEXITY_API_KEY,
systemPrompt = SEARCH_SYSTEM_PROMPT,
userPrompt = SEARCH_QUERY_PROMPT + prompt,
max_tokens = MAX_TOKENS
// url = "https://api.openai.com/v1/chat/completions",
// model = OPENAI_35_MODEL,
// key = OPENAI_API_KEY,
// systemPrompt = FORMAT_SYSTEM_PROMPT,
// userPrompt = prompt,
// max_tokens = MAX_TOKENS
) {
// request
const request = {
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json",
authorization: "Bearer " + key,
},
payload: JSON.stringify({
model,
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: userPrompt,
},
],
max_tokens,
}),
};
// connect to the LLM API, sending the request and parse the JSON response
try {
const response = JSON.parse(
UrlFetchApp.fetch(url, request).getContentText()
);
return response.choices[0].message.content;
} catch (error) {
console.log(error);
return error;
}
}
function SearchContact(prompt, sheet, row, lastCol) {
return new Promise(async (resolve, reject) => {
try {
let email = "";
email = prompt.match(/\S+@\S+\.\S+/);
let foundContact = await llm(
"https://api.perplexity.ai/chat/completions",
PERPLEXITY_70_MODEL,
PERPLEXITY_API_KEY,
SEARCH_SYSTEM_PROMPT,
SEARCH_QUERY_PROMPT + prompt,
MAX_TOKENS
);
let llmFormattedContact = await llm(
"https://api.openai.com/v1/chat/completions",
OPENAI_35_MODEL,
OPENAI_API_KEY,
FORMAT_SYSTEM_PROMPT,
foundContact,
MAX_TOKENS
);
let finalFormatedContact = formatContact(llmFormattedContact);
let linkedin = await googleSearch(prompt, "linkedin.com");
let twitter = await googleSearch(prompt, "twitter.com");
let employer_website = "";
if (finalFormatedContact["employer"] !== "" && finalFormatedContact["employer_website"] == "") {
employer_website = await googleSearch(finalFormatedContact["employer"] + " website", "");
}
sheet.getRange(row, lastCol + 1).setValue(finalFormatedContact["full_name"]);
if (email !== "") {
sheet.getRange(row, lastCol + 2).setValue(email);
} else {
sheet.getRange(row, lastCol + 2).setValue(finalFormatedContact["email"]);
}
if (linkedin !== "") {
sheet.getRange(row, lastCol + 3).setValue(linkedin);
} else {
sheet.getRange(row, lastCol + 3).setValue(finalFormatedContact["linkedin"]);
}
if (twitter !== "") {
sheet.getRange(row, lastCol + 4).setValue(twitter);
} else {
sheet.getRange(row, lastCol + 4).setValue(finalFormatedContact["twitter"]);
}
sheet.getRange(row, lastCol + 5).setValue(finalFormatedContact["employer"]);
sheet.getRange(row, lastCol + 6).setValue(finalFormatedContact["job_title"]);
if (employer_website !== "") {
sheet.getRange(row, lastCol + 7).setValue(employer_website);
} else {
sheet.getRange(row, lastCol + 7).setValue(finalFormatedContact["employer_website"]);
}
if (finalFormatedContact["bio"] === "") {
sheet.getRange(row, lastCol + 8).setValue(foundContact);
} else {
sheet.getRange(row, lastCol + 8).setValue(finalFormatedContact["bio"]);
}
sheet.getRange(row, lastCol + 9).setValue(foundContact);
resolve();
} catch (error) {
reject(error);
}
});
}
async function SearchContacts() {
// Get selected cell range with prompts
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const range = sheet.getActiveRange(); // get the selected range
const values = range.getValues();
const firstRow = range.getRow(); // get first row number
const lastCol = range.getLastColumn(); // get last column number
// Run model on each prompt
let promises = [];
for (let i = 0; i < values.length; i++) {
const row = firstRow + i; // row number
let prompt = values[i][0]; // get the prompt from the first column of the selected row
// If the prompt is not empty
if (prompt !== "") {
promises.push(SearchContact(prompt, sheet, row, lastCol));
}
}
Promise.all(promises)
return 1;
}