Skip to content

Commit c1552eb

Browse files
authored
chore: move wikipedia tool to create-llama (#498)
1 parent 131e63a commit c1552eb

File tree

7 files changed

+87
-10
lines changed

7 files changed

+87
-10
lines changed

.changeset/thin-buses-hunt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
chore: move wikipedia tool to create-llama

.github/workflows/e2e.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ jobs:
6969
DATASOURCE: ${{ matrix.datasources }}
7070
working-directory: .
7171

72-
- uses: actions/upload-artifact@v3
72+
- uses: actions/upload-artifact@v4
7373
if: always()
7474
with:
75-
name: playwright-report-python
75+
name: playwright-report-python-${{ matrix.os }}-${{ matrix.frameworks }}-${{ matrix.datasources }}
7676
path: ./playwright-report/
77+
overwrite: true
7778
retention-days: 30
7879

7980
e2e-typescript:
@@ -136,9 +137,10 @@ jobs:
136137
DATASOURCE: ${{ matrix.datasources }}
137138
working-directory: .
138139

139-
- uses: actions/upload-artifact@v3
140+
- uses: actions/upload-artifact@v4
140141
if: always()
141142
with:
142-
name: playwright-report-typescript
143+
name: playwright-report-typescript-${{ matrix.os }}-${{ matrix.frameworks }}-${{ matrix.datasources }}-node${{ matrix.node-version }}
143144
path: ./playwright-report/
145+
overwrite: true
144146
retention-days: 30

helpers/tools.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,16 @@ export const writeToolsConfig = async (
325325
yaml.stringify(configContent),
326326
);
327327
} else {
328+
// For Typescript, we treat llamahub tools as local tools
329+
const tsConfigContent = {
330+
local: {
331+
...configContent.local,
332+
...configContent.llamahub,
333+
},
334+
};
328335
await fs.writeFile(
329336
path.join(configPath, "tools.json"),
330-
JSON.stringify(configContent, null, 2),
337+
JSON.stringify(tsConfigContent, null, 2),
331338
);
332339
}
333340
};

templates/components/engines/typescript/agent/tools/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { BaseToolWithCall } from "llamaindex";
2-
import { ToolsFactory } from "llamaindex/tools/ToolsFactory";
32
import fs from "node:fs/promises";
43
import path from "node:path";
54
import { CodeGeneratorTool, CodeGeneratorToolParams } from "./code-generator";
@@ -18,6 +17,7 @@ import { ImgGeneratorTool, ImgGeneratorToolParams } from "./img-gen";
1817
import { InterpreterTool, InterpreterToolParams } from "./interpreter";
1918
import { OpenAPIActionTool } from "./openapi-action";
2019
import { WeatherTool, WeatherToolParams } from "./weather";
20+
import { WikipediaTool, WikipediaToolParams } from "./wikipedia";
2121

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

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

3533
const toolFactory: Record<string, ToolCreator> = {
34+
"wikipedia.WikipediaToolSpec": async (config: unknown) => {
35+
return [new WikipediaTool(config as WikipediaToolParams)];
36+
},
3637
weather: async (config: unknown) => {
3738
return [new WeatherTool(config as WeatherToolParams)];
3839
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { JSONSchemaType } from "ajv";
2+
import type { BaseTool, ToolMetadata } from "llamaindex";
3+
import { default as wiki } from "wikipedia";
4+
5+
type WikipediaParameter = {
6+
query: string;
7+
lang?: string;
8+
};
9+
10+
export type WikipediaToolParams = {
11+
metadata?: ToolMetadata<JSONSchemaType<WikipediaParameter>>;
12+
};
13+
14+
const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<WikipediaParameter>> = {
15+
name: "wikipedia_tool",
16+
description: "A tool that uses a query engine to search Wikipedia.",
17+
parameters: {
18+
type: "object",
19+
properties: {
20+
query: {
21+
type: "string",
22+
description: "The query to search for",
23+
},
24+
lang: {
25+
type: "string",
26+
description: "The language to search in",
27+
nullable: true,
28+
},
29+
},
30+
required: ["query"],
31+
},
32+
};
33+
34+
export class WikipediaTool implements BaseTool<WikipediaParameter> {
35+
private readonly DEFAULT_LANG = "en";
36+
metadata: ToolMetadata<JSONSchemaType<WikipediaParameter>>;
37+
38+
constructor(params?: WikipediaToolParams) {
39+
this.metadata = params?.metadata || DEFAULT_META_DATA;
40+
}
41+
42+
async loadData(
43+
page: string,
44+
lang: string = this.DEFAULT_LANG,
45+
): Promise<string> {
46+
wiki.setLang(lang);
47+
const pageResult = await wiki.page(page, { autoSuggest: false });
48+
const content = await pageResult.content();
49+
return content;
50+
}
51+
52+
async call({
53+
query,
54+
lang = this.DEFAULT_LANG,
55+
}: WikipediaParameter): Promise<string> {
56+
const searchResult = await wiki.search(query);
57+
if (searchResult.results.length === 0) return "No search results.";
58+
return await this.loadData(searchResult.results[0].title, lang);
59+
}
60+
}

templates/types/streaming/express/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"@apidevtools/swagger-parser": "^10.1.0",
3030
"formdata-node": "^6.0.3",
3131
"marked": "^14.1.2",
32-
"papaparse": "^5.4.1"
32+
"papaparse": "^5.4.1",
33+
"wikipedia": "^2.1.2"
3334
},
3435
"devDependencies": {
3536
"@types/cors": "^2.8.16",

templates/types/streaming/nextjs/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"tailwind-merge": "^2.6.0",
3737
"tiktoken": "^1.0.15",
3838
"uuid": "^9.0.1",
39-
"marked": "^14.1.2"
39+
"marked": "^14.1.2",
40+
"wikipedia": "^2.1.2"
4041
},
4142
"devDependencies": {
4243
"@types/node": "^20.10.3",

0 commit comments

Comments
 (0)