-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kb): Generating embedding chunks and vectors
- Loading branch information
1 parent
dd3de86
commit bb05f2a
Showing
8 changed files
with
1,045 additions
and
582 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import axios from 'axios'; | ||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; | ||
import { KnowledgeDocumentChunk } from 'src/types/knowledge'; | ||
|
||
const DEFAULT_EMBEDDING_API_URL = | ||
'https://curated.aleph.cloud/vm/ee1b2a8e5bd645447739d8b234ef495c9a2b4d0b98317d510a3ccf822808ebe5/embedding'; | ||
|
||
export const generateChunks = async ( | ||
title: string, | ||
content: string, | ||
chunkSize = 500, | ||
overlapSize = 100, | ||
): Promise<KnowledgeDocumentChunk[]> => { | ||
const splitter = new RecursiveCharacterTextSplitter({ | ||
chunkSize: chunkSize, | ||
chunkOverlap: overlapSize, | ||
separators: ['\n\n---\n\n', '\n\n', '\n', ' '], | ||
}); | ||
|
||
// Split into a list of LangChain documents | ||
const documents = await splitter.createDocuments( | ||
[content], | ||
// TODO: include metadata | ||
[], | ||
{ | ||
chunkHeader: `DOCUMENT TITLE: ${title}\n\n---\n\n`, | ||
appendChunkOverlapHeader: true, | ||
}, | ||
); | ||
return await Promise.all( | ||
documents.map( | ||
async (d): Promise<KnowledgeDocumentChunk> => ({ | ||
content: d.pageContent, | ||
vector: await embed(d.pageContent), | ||
}), | ||
), | ||
); | ||
}; | ||
|
||
async function embed(content: string): Promise<number[]> { | ||
const tries = 3; | ||
let timeout = 1000; | ||
|
||
const errors = []; | ||
for (let i = 0; i < tries; i++) { | ||
try { | ||
const response = await axios.post<{ embedding: number[] }>(DEFAULT_EMBEDDING_API_URL, { | ||
content, | ||
}); | ||
|
||
return response.data.embedding; | ||
} catch (error) { | ||
errors.push(error); | ||
console.error(`Error embedding text: ${error}`); | ||
await new Promise((resolve) => setTimeout(resolve, timeout)); | ||
timeout *= 2; | ||
} | ||
} | ||
return []; | ||
} |