-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate-embeddings.ts
149 lines (136 loc) · 3.28 KB
/
generate-embeddings.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
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
import { Pool } from 'pg';
import axios from 'axios';
const DATABASE_URL = '';
const OPENAI_API_KEY = '';
const pool = new Pool({
connectionString: DATABASE_URL,
});
async function createCompaniesTable() {
const client = await pool.connect();
try {
await client.query(`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS companies (
id SERIAL PRIMARY KEY,
name TEXT,
slug TEXT,
website TEXT,
"smallLogoUrl" TEXT,
"oneLiner" TEXT,
"longDescription" TEXT,
"teamSize" INTEGER,
url TEXT,
batch TEXT,
tags TEXT[],
status TEXT,
industries TEXT[],
regions TEXT[],
locations TEXT[],
badges TEXT[],
embedding VECTOR(1536)
);
`);
console.log('Companies table created successfully');
} catch (error) {
console.error('Error creating companies table:', error);
} finally {
client.release();
}
}
async function scrapeCompanies(url: string) {
try {
const response = await axios.get(url);
const { companies, nextPage } = response.data;
for (const company of companies) {
const { longDescription } = company;
const embedding = await generateEmbedding(longDescription);
await storeCompany(company, embedding);
}
if (nextPage) {
await scrapeCompanies(nextPage);
}
} catch (error) {
console.error('Error scraping companies:', error);
}
}
async function generateEmbedding(text: string): Promise<number[]> {
try {
const response = await axios.post(
'https://api.openai.com/v1/embeddings',
{
input: text,
model: 'text-embedding-ada-002',
},
{
headers: {
Authorization: `Bearer ${OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
}
);
const { data } = response.data;
return data[0].embedding;
} catch (error) {
console.error('Error generating embedding:', error);
}
return [];
}
async function storeCompany(company: any, embedding: number[]) {
const {
name,
slug,
website,
smallLogoUrl,
oneLiner,
longDescription,
teamSize,
url,
batch,
tags,
status,
industries,
regions,
locations,
badges,
} = company;
const client = await pool.connect();
try {
await client.query(
`
INSERT INTO companies (
name, slug, website, "smallLogoUrl", "oneLiner", "longDescription", "teamSize", url, batch, tags, status, industries, regions, locations, badges, embedding
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
`,
[
name,
slug,
website,
smallLogoUrl,
oneLiner,
longDescription,
teamSize,
url,
batch,
tags,
status,
industries,
regions,
locations,
badges,
embedding,
]
);
console.log(`Company '${name}' stored successfully`);
} catch (error) {
console.error(`Error storing company '${name}':`, error);
} finally {
client.release();
}
}
async function runScript() {
await createCompaniesTable();
await scrapeCompanies('https://api.ycombinator.com/v0.1/companies?page=1');
await pool.end();
}
runScript();