-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
# atlas schema apply \ | ||
# -u "postgres://postgres:password@localhost/nyheter?sslmode=disable" \ | ||
# --to file://schema.sql \ | ||
# --dev-url "docker://postgres/15/test" \ | ||
|
||
atlas schema apply \ | ||
-u "postgres://postgres:password@ai:9001/postgres?sslmode=disable" \ | ||
--to file://schema.sql \ | ||
--dev-url "docker://postgres/15/test" | ||
--dev-url "docker+postgres://ankane/pgvector/dev" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
import { db } from './utils/db'; | ||
import { openai } from './utils/openai'; | ||
|
||
async function main() { | ||
// get all articles that does not have embeddings | ||
// get the article's english article | ||
// generate embeddings | ||
// store the embeddings on the articles table | ||
const articles = await db | ||
.selectFrom('articleTranslations as at') | ||
.innerJoin('articles as a', 'a.id', 'at.articleId') | ||
.select(['a.id as articleId', 'at.title', 'at.body']) | ||
.where('a.embedding', 'is', null) | ||
.where('at.language', '=', 'en') | ||
.orderBy('a.id', 'desc') | ||
.execute(); | ||
|
||
for (const article of articles) { | ||
console.log(`Generate embedding for ${article.title}`); | ||
|
||
const text = article.title + ' ' + article.body; | ||
|
||
const res = await openai.embeddings.create({ | ||
input: text, | ||
model: 'text-embedding-ada-002', | ||
}); | ||
|
||
if (res.data.length === 0) { | ||
console.log('no embeddings received from openai'); | ||
continue; | ||
} | ||
|
||
const { embedding } = res.data[0]; | ||
|
||
await db | ||
.updateTable('articles') | ||
.set({ | ||
embedding: `[${embedding.toString()}]`, | ||
}) | ||
.where('id', '=', article.articleId) | ||
.execute(); | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters