-
Notifications
You must be signed in to change notification settings - Fork 961
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
6 changed files
with
138 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Major ref: https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/qdrant | ||
import dotenv from "dotenv"; | ||
import { Document } from "langchain/document"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { CharacterTextSplitter } from "langchain/text_splitter"; | ||
import { QdrantVectorStore } from "langchain/vectorstores/qdrant"; | ||
import { QdrantClient } from '@qdrant/js-client-rest'; | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
dotenv.config({ path: `.env.local` }); | ||
|
||
const fileNames = fs.readdirSync("companions"); | ||
const splitter = new CharacterTextSplitter({ | ||
separator: " ", | ||
chunkSize: 200, | ||
chunkOverlap: 50, | ||
}); | ||
|
||
const langchainDocs = await Promise.all( | ||
fileNames.map(async (fileName) => { | ||
if (fileName.endsWith(".txt")) { | ||
const filePath = path.join("companions", fileName); | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
// get the last section in the doc for background info | ||
const lastSection = fileContent.split("###ENDSEEDCHAT###").slice(-1)[0]; | ||
const splitDocs = await splitter.createDocuments([lastSection]); | ||
return splitDocs.map((doc) => { | ||
return new Document({ | ||
metadata: { fileName }, | ||
pageContent: doc.pageContent, | ||
}); | ||
}); | ||
} | ||
}) | ||
); | ||
|
||
|
||
const qdrantClient = new QdrantClient({ url: process.env.QDRANT_URL, apiKey: process.env?.QDRANT_API_KEY }); | ||
|
||
await QdrantVectorStore.fromDocuments( | ||
langchainDocs.flat().filter((doc) => doc !== undefined), | ||
new OpenAIEmbeddings({ openAIApiKey: process.env.OPENAI_API_KEY }), | ||
{ | ||
client: qdrantClient, | ||
collectionName: process.env.QDRANT_COLLECTION_NAME, | ||
} | ||
); |