From 56e14ea06415e382eb32674e88b8421efbf7e13c Mon Sep 17 00:00:00 2001 From: thucpn <thucsh2@gmail.com> Date: Mon, 17 Mar 2025 09:29:42 +0700 Subject: [PATCH] wiki() factory --- examples/agent/wiki.ts | 2 +- examples/agentworkflow/blog-writer.ts | 2 +- examples/anthropic/agent.ts | 2 +- examples/vercel/llm.ts | 2 +- examples/wiki.ts | 62 --------------------------- packages/tools/src/tools/wiki.ts | 36 +++++++++------- 6 files changed, 24 insertions(+), 82 deletions(-) delete mode 100644 examples/wiki.ts diff --git a/examples/agent/wiki.ts b/examples/agent/wiki.ts index 745042686..f69594303 100644 --- a/examples/agent/wiki.ts +++ b/examples/agent/wiki.ts @@ -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, }); diff --git a/examples/agentworkflow/blog-writer.ts b/examples/agentworkflow/blog-writer.ts index f7a0bd411..154aceca7 100644 --- a/examples/agentworkflow/blog-writer.ts +++ b/examples/agentworkflow/blog-writer.ts @@ -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, }); diff --git a/examples/anthropic/agent.ts b/examples/anthropic/agent.ts index 6aaac73fb..349beb6f1 100644 --- a/examples/anthropic/agent.ts +++ b/examples/anthropic/agent.ts @@ -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, diff --git a/examples/vercel/llm.ts b/examples/vercel/llm.ts index 15fb4a198..850842cca 100644 --- a/examples/vercel/llm.ts +++ b/examples/vercel/llm.ts @@ -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({ diff --git a/examples/wiki.ts b/examples/wiki.ts deleted file mode 100644 index f522011ef..000000000 --- a/examples/wiki.ts +++ /dev/null @@ -1,62 +0,0 @@ -/** 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); - } -} diff --git a/packages/tools/src/tools/wiki.ts b/packages/tools/src/tools/wiki.ts index 741ec452e..b07b72838 100644 --- a/packages/tools/src/tools/wiki.ts +++ b/packages/tools/src/tools/wiki.ts @@ -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() }; + }, + }); +}; -- GitLab