fix: Add metadatafilters to context chat engine (Typescript) (#196)

This commit is contained in:
Marcus Schiesser
2024-07-31 08:55:06 +02:00
committed by GitHub
parent df51361ca1
commit f43399cc18
4 changed files with 39 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
Add metadatafilters to context chat engine (Typescript)
@@ -1,4 +1,9 @@
import { ContextChatEngine, Settings } from "llamaindex";
import {
ContextChatEngine,
MetadataFilter,
MetadataFilters,
Settings,
} from "llamaindex";
import { getDataSource } from "./index";
export async function createChatEngine(documentIds?: string[]) {
@@ -10,12 +15,36 @@ export async function createChatEngine(documentIds?: string[]) {
}
const retriever = index.asRetriever({
similarityTopK: process.env.TOP_K ? parseInt(process.env.TOP_K) : 3,
filters: generateFilters(documentIds || []),
});
return new ContextChatEngine({
chatModel: Settings.llm,
retriever,
// disable as a custom system prompt disables the generated context
// systemPrompt: process.env.SYSTEM_PROMPT,
systemPrompt: process.env.SYSTEM_PROMPT,
});
}
function generateFilters(documentIds: string[]): MetadataFilters {
// public documents don't have the "private" field or it's set to "false"
const publicDocumentsFilter: MetadataFilter = {
key: "private",
value: ["true"],
operator: "nin",
};
// if no documentIds are provided, only retrieve information from public documents
if (!documentIds.length) return { filters: [publicDocumentsFilter] };
const privateDocumentsFilter: MetadataFilter = {
key: "doc_id",
value: documentIds,
operator: "in",
};
// if documentIds are provided, retrieve information from public and private documents
return {
filters: [publicDocumentsFilter, privateDocumentsFilter],
condition: "or",
};
}
@@ -20,7 +20,7 @@
"dotenv": "^16.3.1",
"duck-duck-scrape": "^2.2.5",
"express": "^4.18.2",
"llamaindex": "0.5.8",
"llamaindex": "0.5.12",
"pdf2json": "3.0.5",
"ajv": "^8.12.0",
"@e2b/code-interpreter": "^0.0.5",
@@ -24,7 +24,7 @@
"duck-duck-scrape": "^2.2.5",
"formdata-node": "^6.0.3",
"got": "^14.4.1",
"llamaindex": "0.5.8",
"llamaindex": "0.5.12",
"lucide-react": "^0.294.0",
"next": "^14.2.4",
"react": "^18.2.0",