-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify agents selection for typescript
- Loading branch information
1 parent
ed03236
commit 3c8a09f
Showing
5 changed files
with
64 additions
and
58 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
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 |
---|---|---|
@@ -1,26 +1,44 @@ | ||
import config from "@/config/tools.json"; | ||
import { OpenAI, OpenAIAgent, QueryEngineTool, ToolFactory } from "llamaindex"; | ||
import { | ||
BaseTool, | ||
OpenAI, | ||
OpenAIAgent, | ||
QueryEngineTool, | ||
ToolFactory, | ||
} from "llamaindex"; | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { STORAGE_CACHE_DIR } from "./constants.mjs"; | ||
import { getDataSource } from "./index"; | ||
|
||
export async function createChatEngine(llm: OpenAI) { | ||
let tools: BaseTool[] = []; | ||
|
||
// Add a query engine tool if we have a data source | ||
// Delete this code if you don't have a data source | ||
const index = await getDataSource(llm); | ||
const queryEngine = index.asQueryEngine(); | ||
const queryEngineTool = new QueryEngineTool({ | ||
queryEngine: queryEngine, | ||
metadata: { | ||
name: "data_query_engine", | ||
description: `A query engine for documents in storage folder: ${STORAGE_CACHE_DIR}`, | ||
}, | ||
}); | ||
if (index) { | ||
tools.push( | ||
new QueryEngineTool({ | ||
queryEngine: index.asQueryEngine(), | ||
metadata: { | ||
name: "data_query_engine", | ||
description: `A query engine for documents in storage folder: ${STORAGE_CACHE_DIR}`, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
const externalTools = await ToolFactory.createTools(config); | ||
try { | ||
// add tools from config file if it exists | ||
const config = JSON.parse( | ||
await fs.readFile(path.join("config", "tools.json"), "utf8"), | ||
); | ||
tools = tools.concat(await ToolFactory.createTools(config)); | ||
} catch {} | ||
|
||
const agent = new OpenAIAgent({ | ||
tools: [queryEngineTool, ...externalTools], | ||
verbose: true, | ||
return new OpenAIAgent({ | ||
tools, | ||
llm, | ||
verbose: true, | ||
}); | ||
|
||
return agent; | ||
} |
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