Skip to content
Snippets Groups Projects
Commit 56e14ea0 authored by thucpn's avatar thucpn
Browse files

wiki() factory

parent b0e24afd
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ async function main() {
const llm = new OpenAI({ model: "gpt-4-turbo" });
const workflow = agent({
tools: [wiki],
tools: [wiki()],
llm,
verbose: false,
});
......
......@@ -46,7 +46,7 @@ async function main() {
description:
"Responsible for gathering relevant information from the internet",
systemPrompt: `You are a research agent. Your role is to gather information from the internet using the provided tools and then transfer this information to the report agent for content creation.`,
tools: [wiki],
tools: [wiki()],
canHandoffTo: [reportAgent],
llm,
});
......
......@@ -13,7 +13,7 @@ const workflow = agent({
}),
execute: ({ location }) => `The weather in ${location} is sunny`,
}),
wiki,
wiki(),
],
llm: anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
......
......@@ -33,7 +33,7 @@ async function main() {
console.log("\n=== Test 3: Using LLMAgent with WikipediaTool ===");
const agent = new LLMAgent({
llm: vercelLLM,
tools: [wiki],
tools: [wiki()],
});
const { message } = await agent.chat({
......
/** Example of a tool that uses Wikipedia */
import type { JSONSchemaType } from "ajv";
import type { BaseTool, ToolMetadata } from "llamaindex";
import { default as wiki } from "wikipedia";
type WikipediaParameter = {
query: string;
lang?: string;
};
type WikipediaToolParams = {
metadata?: ToolMetadata<JSONSchemaType<WikipediaParameter>>;
};
const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<WikipediaParameter>> = {
name: "wikipedia_search",
description: "A tool that uses a query engine to search Wikipedia.",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The query to search for",
},
lang: {
type: "string",
description: "The language to search in",
nullable: true,
},
},
required: ["query"],
},
};
export class WikipediaTool implements BaseTool<WikipediaParameter> {
private readonly DEFAULT_LANG = "en";
metadata: ToolMetadata<JSONSchemaType<WikipediaParameter>>;
constructor(params?: WikipediaToolParams) {
this.metadata = params?.metadata || DEFAULT_META_DATA;
}
async loadData(
page: string,
lang: string = this.DEFAULT_LANG,
): Promise<string> {
wiki.setLang(lang);
const pageResult = await wiki.page(page, { autoSuggest: false });
const content = await pageResult.content();
return content;
}
async call({
query,
lang = this.DEFAULT_LANG,
}: WikipediaParameter): Promise<string> {
const searchResult = await wiki.search(query);
if (searchResult.results.length === 0) return "No search results.";
return await this.loadData(searchResult.results[0].title, lang);
}
}
......@@ -7,19 +7,23 @@ export type WikiToolOutput = {
content: string;
};
export const wiki = tool({
name: "wikipedia",
description: "Use this function to search Wikipedia",
parameters: z.object({
query: z.string().describe("The query to search for"),
lang: z.string().describe("The language to search in").default("en"),
}),
execute: async ({ query, lang }): Promise<WikiToolOutput> => {
wikipedia.setLang(lang);
const searchResult = await wikipedia.search(query);
const pageTitle = searchResult.results[0].title;
if (!pageTitle) return { title: "No search results.", content: "" };
const pageResult = await wikipedia.page(pageTitle, { autoSuggest: false });
return { title: pageTitle, content: await pageResult.content() };
},
});
export const wiki = () => {
return tool({
name: "wikipedia",
description: "Use this function to search Wikipedia",
parameters: z.object({
query: z.string().describe("The query to search for"),
lang: z.string().describe("The language to search in").default("en"),
}),
execute: async ({ query, lang }): Promise<WikiToolOutput> => {
wikipedia.setLang(lang);
const searchResult = await wikipedia.search(query);
const pageTitle = searchResult.results[0].title;
if (!pageTitle) return { title: "No search results.", content: "" };
const pageResult = await wikipedia.page(pageTitle, {
autoSuggest: false,
});
return { title: pageTitle, content: await pageResult.content() };
},
});
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment