Skip to content

Commit 1f9fd98

Browse files
committed
feat: add filter for query in ts templates
1 parent 455ab68 commit 1f9fd98

File tree

7 files changed

+46
-10
lines changed

7 files changed

+46
-10
lines changed

templates/components/engines/typescript/agent/chat.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { BaseToolWithCall, OpenAIAgent, QueryEngineTool } from "llamaindex";
1+
import {
2+
BaseToolWithCall,
3+
MetadataFilters,
4+
OpenAIAgent,
5+
QueryEngineTool,
6+
} from "llamaindex";
27
import fs from "node:fs/promises";
38
import path from "node:path";
49
import { getDataSource } from "./index";
@@ -14,7 +19,7 @@ export async function createChatEngine(documentIds?: string[]) {
1419
tools.push(
1520
new QueryEngineTool({
1621
queryEngine: index.asQueryEngine({
17-
preFilters: undefined, // TODO: Add filters once LITS supports it (getQueryFilters)
22+
preFilters: generateFilters(documentIds || []),
1823
}),
1924
metadata: {
2025
name: "data_query_engine",
@@ -41,3 +46,32 @@ export async function createChatEngine(documentIds?: string[]) {
4146
systemPrompt: process.env.SYSTEM_PROMPT,
4247
});
4348
}
49+
50+
function generateFilters(documentIds: string[]): MetadataFilters | undefined {
51+
if (!documentIds.length) {
52+
return {
53+
filters: [
54+
{
55+
key: "private",
56+
value: ["true"],
57+
operator: "nin",
58+
},
59+
],
60+
};
61+
}
62+
return {
63+
filters: [
64+
{
65+
key: "private",
66+
value: "true",
67+
operator: "!=",
68+
},
69+
{
70+
key: "doc_id",
71+
value: documentIds,
72+
operator: "in",
73+
},
74+
],
75+
condition: "or",
76+
};
77+
}

templates/components/llamaindex/typescript/documents/pipeline.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export async function runPipeline(documents: Document[], filename: string) {
1818
for (const document of documents) {
1919
document.metadata = {
2020
...document.metadata,
21+
doc_id: document.id_,
2122
file_name: filename,
2223
private: "true", // to separate from other public documents
2324
};

templates/components/llamaindex/typescript/streaming/annotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export function retrieveDocumentIds(annotations?: JSONValue[]): string[] {
3535
) {
3636
const files = data.files as DocumentFile[];
3737
for (const file of files) {
38-
if (Array.isArray(file.content)) {
38+
if (Array.isArray(file.content.value)) {
3939
// it's an array, so it's an array of doc IDs
40-
for (const id of file.content) {
40+
for (const id of file.content.value) {
4141
ids.push(id);
4242
}
4343
}

templates/components/llamaindex/typescript/streaming/events.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function createCallbackManager(stream: StreamData) {
7777
const callbackManager = new CallbackManager();
7878

7979
callbackManager.on("retrieve-end", async (data) => {
80-
const { nodes, query } = data.detail.payload;
80+
const { nodes, query } = data.detail;
8181
await appendSourceData(stream, nodes);
8282
appendEventData(stream, `Retrieving context for query: '${query}'`);
8383
appendEventData(
@@ -87,7 +87,7 @@ export function createCallbackManager(stream: StreamData) {
8787
});
8888

8989
callbackManager.on("llm-tool-call", (event) => {
90-
const { name, input } = event.detail.payload.toolCall;
90+
const { name, input } = event.detail.toolCall;
9191
const inputString = Object.entries(input)
9292
.map(([key, value]) => `${key}: ${value}`)
9393
.join(", ");
@@ -98,7 +98,7 @@ export function createCallbackManager(stream: StreamData) {
9898
});
9999

100100
callbackManager.on("llm-tool-result", (event) => {
101-
const { toolCall, toolResult } = event.detail.payload;
101+
const { toolCall, toolResult } = event.detail;
102102
appendToolData(stream, toolCall, toolResult);
103103
});
104104

templates/types/streaming/express/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"dotenv": "^16.3.1",
2121
"duck-duck-scrape": "^2.2.5",
2222
"express": "^4.18.2",
23-
"llamaindex": "0.4.14",
23+
"llamaindex": "0.5.5",
2424
"pdf2json": "3.0.5",
2525
"ajv": "^8.12.0",
2626
"@e2b/code-interpreter": "^0.0.5",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** @type {import('next').NextConfig} */
22
import fs from "fs";
3+
import withLlamaIndex from "llamaindex/next";
34
import webpack from "./webpack.config.mjs";
45

56
const nextConfig = JSON.parse(fs.readFileSync("./next.config.json", "utf-8"));
67
nextConfig.webpack = webpack;
78

8-
export default nextConfig;
9+
export default withLlamaIndex(nextConfig);

templates/types/streaming/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"duck-duck-scrape": "^2.2.5",
2525
"formdata-node": "^6.0.3",
2626
"got": "^14.4.1",
27-
"llamaindex": "0.4.14",
27+
"llamaindex": "0.5.5",
2828
"lucide-react": "^0.294.0",
2929
"next": "^14.2.4",
3030
"react": "^18.2.0",

0 commit comments

Comments
 (0)