-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add retriever tool example (#814)
- Loading branch information
1 parent
97e4ecd
commit e37fa5d
Showing
2 changed files
with
68 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
FunctionTool, | ||
MetadataMode, | ||
NodeWithScore, | ||
OpenAIAgent, | ||
SimpleDirectoryReader, | ||
VectorStoreIndex, | ||
} from "llamaindex"; | ||
|
||
async function main() { | ||
// Load the documents | ||
const documents = await new SimpleDirectoryReader().loadData({ | ||
directoryPath: "node_modules/llamaindex/examples", | ||
}); | ||
|
||
// Create a vector index from the documents | ||
const vectorIndex = await VectorStoreIndex.fromDocuments(documents); | ||
|
||
const retriever = vectorIndex.asRetriever({ similarityTopK: 3 }); | ||
|
||
const retrieverTool = FunctionTool.from( | ||
async ({ query }: { query: string }) => { | ||
const nodesWithScores = await retriever.retrieve({ | ||
query, | ||
}); | ||
return nodesWithScores | ||
.map((nodeWithScore: NodeWithScore) => | ||
nodeWithScore.node.getContent(MetadataMode.NONE), | ||
) | ||
.join("\n"); | ||
}, | ||
{ | ||
name: "get_abramov_info", | ||
description: "Get information about the Abramov documents", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
query: { | ||
type: "string", | ||
description: "The query about Abramov", | ||
}, | ||
}, | ||
required: ["query"], | ||
}, | ||
}, | ||
); | ||
|
||
// Create an OpenAIAgent with the function tools | ||
const agent = new OpenAIAgent({ | ||
tools: [retrieverTool], | ||
verbose: true, | ||
}); | ||
|
||
// Chat with the agent | ||
const response = await agent.chat({ | ||
message: "What was his first salary?", | ||
}); | ||
|
||
// Print the response | ||
console.log(response.response); | ||
} | ||
|
||
void main().then(() => { | ||
console.log("Done"); | ||
}); |