Skip to content

chore: move wikipedia tool to create-llama #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-buses-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

chore: move wikipedia tool to create-llama
10 changes: 6 additions & 4 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ jobs:
DATASOURCE: ${{ matrix.datasources }}
working-directory: .

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report-python
name: playwright-report-python-${{ matrix.os }}-${{ matrix.frameworks }}-${{ matrix.datasources }}
path: ./playwright-report/
overwrite: true
retention-days: 30

e2e-typescript:
Expand Down Expand Up @@ -136,9 +137,10 @@ jobs:
DATASOURCE: ${{ matrix.datasources }}
working-directory: .

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report-typescript
name: playwright-report-typescript-${{ matrix.os }}-${{ matrix.frameworks }}-${{ matrix.datasources }}-node${{ matrix.node-version }}
path: ./playwright-report/
overwrite: true
retention-days: 30
9 changes: 8 additions & 1 deletion helpers/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,16 @@ export const writeToolsConfig = async (
yaml.stringify(configContent),
);
} else {
// For Typescript, we treat llamahub tools as local tools
const tsConfigContent = {
local: {
...configContent.local,
...configContent.llamahub,
},
};
await fs.writeFile(
path.join(configPath, "tools.json"),
JSON.stringify(configContent, null, 2),
JSON.stringify(tsConfigContent, null, 2),
);
}
};
7 changes: 4 additions & 3 deletions templates/components/engines/typescript/agent/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseToolWithCall } from "llamaindex";
import { ToolsFactory } from "llamaindex/tools/ToolsFactory";
import fs from "node:fs/promises";
import path from "node:path";
import { CodeGeneratorTool, CodeGeneratorToolParams } from "./code-generator";
Expand All @@ -18,6 +17,7 @@ import { ImgGeneratorTool, ImgGeneratorToolParams } from "./img-gen";
import { InterpreterTool, InterpreterToolParams } from "./interpreter";
import { OpenAPIActionTool } from "./openapi-action";
import { WeatherTool, WeatherToolParams } from "./weather";
import { WikipediaTool, WikipediaToolParams } from "./wikipedia";

type ToolCreator = (config: unknown) => Promise<BaseToolWithCall[]>;

Expand All @@ -27,12 +27,13 @@ export async function createTools(toolConfig: {
}): Promise<BaseToolWithCall[]> {
// add local tools from the 'tools' folder (if configured)
const tools = await createLocalTools(toolConfig.local);
// add tools from LlamaIndexTS (if configured)
tools.push(...(await ToolsFactory.createTools(toolConfig.llamahub)));
return tools;
}

const toolFactory: Record<string, ToolCreator> = {
"wikipedia.WikipediaToolSpec": async (config: unknown) => {
return [new WikipediaTool(config as WikipediaToolParams)];
},
weather: async (config: unknown) => {
return [new WeatherTool(config as WeatherToolParams)];
},
Expand Down
60 changes: 60 additions & 0 deletions templates/components/engines/typescript/agent/tools/wikipedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { JSONSchemaType } from "ajv";
import type { BaseTool, ToolMetadata } from "llamaindex";
import { default as wiki } from "wikipedia";

type WikipediaParameter = {
query: string;
lang?: string;
};

export type WikipediaToolParams = {
metadata?: ToolMetadata<JSONSchemaType<WikipediaParameter>>;
};

const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<WikipediaParameter>> = {
name: "wikipedia_tool",
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);
}
}
3 changes: 2 additions & 1 deletion templates/types/streaming/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"@apidevtools/swagger-parser": "^10.1.0",
"formdata-node": "^6.0.3",
"marked": "^14.1.2",
"papaparse": "^5.4.1"
"papaparse": "^5.4.1",
"wikipedia": "^2.1.2"
},
"devDependencies": {
"@types/cors": "^2.8.16",
Expand Down
3 changes: 2 additions & 1 deletion templates/types/streaming/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"tailwind-merge": "^2.6.0",
"tiktoken": "^1.0.15",
"uuid": "^9.0.1",
"marked": "^14.1.2"
"marked": "^14.1.2",
"wikipedia": "^2.1.2"
},
"devDependencies": {
"@types/node": "^20.10.3",
Expand Down