-
Notifications
You must be signed in to change notification settings - Fork 0
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
Olasunkanmi Oyinlola
authored and
Olasunkanmi Oyinlola
committed
Jan 28, 2024
1 parent
00b47a8
commit e5b7003
Showing
5 changed files
with
75 additions
and
7 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
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,10 +1,35 @@ | ||
export const dbQuery = { | ||
CREATE_VECTOR: "CREATE EXTENSION IF NOT EXISTS vector;", | ||
CREATE_TABLE: ` | ||
CREATE TABLE IF NOT EXISTS documents ( | ||
id bigserial PRIMARY KEY, | ||
content text, | ||
embedding vector(1536) | ||
); | ||
`, | ||
CREATE TABLE IF NOT EXISTS documents ( | ||
id bigserial PRIMARY KEY, | ||
content text, | ||
embedding vector(1536) | ||
); | ||
`, | ||
CREATE_MATCH_DOCUMENTS_TABLES: ` | ||
create or replace function match_documents ( | ||
query_embedding vector(1536), | ||
match_threshold float, | ||
match_count int | ||
) | ||
returns table ( | ||
id bigint, | ||
content text, | ||
similarity float | ||
) | ||
language sql stable | ||
as $$ | ||
select | ||
documents.id, | ||
documents.content, | ||
1 - (documents.embedding <=> query_embedding) as similarity | ||
from documents | ||
where documents.embedding <=> query_embedding < 1 - match_threshold | ||
order by documents.embedding <=> query_embedding | ||
limit match_count; | ||
$$; | ||
`, | ||
CREATE_INDEX: `CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops) | ||
WITH (lists = 100);`, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export class DocumentService {} |
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 +1,33 @@ | ||
export class Embed {} | ||
export class EmbeddingService { | ||
generateEmbeddings() {} | ||
|
||
/** | ||
* Calculates the cosine similarity between two vectors. | ||
* @param vecA - The first vector. | ||
* @param vecB - The second vector. | ||
* @returns The cosine similarity between the two vectors. | ||
* @throws Error if the lengths of the vectors are not equal. | ||
*/ | ||
cosineSimilarity(vecA: number[], vecB: number[]) { | ||
let dotProduct = 0; | ||
let magnitudeA = 0; | ||
let magnitudeB = 0; | ||
if (vecA.length !== vecB.length) { | ||
throw Error("Both vectors must be of the same length"); | ||
} | ||
for (let i = 0; i < vecA.length; i++) { | ||
dotProduct += vecA[i] * vecB[i]; | ||
magnitudeA += Math.pow(vecA[i], 2); | ||
magnitudeB += Math.pow(vecB[i], 2); | ||
} | ||
|
||
magnitudeA = Math.sqrt(magnitudeA); | ||
magnitudeB = Math.sqrt(magnitudeB); | ||
|
||
if (magnitudeA !== 0 && magnitudeB !== 0) { | ||
return dotProduct / (magnitudeA * magnitudeB); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |