Skip to content

Commit 3af5617

Browse files
fix: query tool
1 parent 724d0ef commit 3af5617

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

.changeset/olive-readers-itch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"llamaindex": patch
3+
"@llamaindex/examples": patch
4+
---
5+
6+
fix undefined values in querytool

examples/agents/agent/query-tool.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { openai } from "@llamaindex/openai";
1+
import { OpenAIEmbedding, openai } from "@llamaindex/openai";
22
import {
33
agent,
44
agentStreamEvent,
55
agentToolCallResultEvent,
66
} from "@llamaindex/workflow";
7-
import { Document, VectorStoreIndex } from "llamaindex";
7+
import { Document, Settings, VectorStoreIndex } from "llamaindex";
88

99
async function main() {
10+
Settings.embedModel = new OpenAIEmbedding();
11+
Settings.llm = openai({ model: "gpt-4o" });
12+
1013
const index = await VectorStoreIndex.fromDocuments([
1114
new Document({
1215
text: "Cats have a specialized collarbone that allows them to always land on their feet when they fall.",
@@ -20,7 +23,6 @@ async function main() {
2023
]);
2124

2225
const myAgent = agent({
23-
llm: openai({ model: "gpt-4o" }),
2426
tools: [
2527
index.queryTool({
2628
options: { similarityTopK: 2 },

examples/pnpm-workspace.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/llamaindex/src/tools/QueryEngineTool.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ export class QueryEngineTool implements BaseTool<QueryEngineParam> {
5454
return { content: response.message.content } as unknown as JSONValue;
5555
}
5656

57+
// Use JSON.parse(JSON.stringify()) to remove undefined values from sourceNodes
58+
// since undefined is not a valid JSONValue
5759
return {
5860
content: response.message.content,
59-
sourceNodes: response.sourceNodes,
61+
sourceNodes: JSON.parse(JSON.stringify(response.sourceNodes ?? [])),
6062
} as unknown as JSONValue;
6163
}
6264
}

0 commit comments

Comments
 (0)