Skip to content
Snippets Groups Projects
Unverified Commit c1552ebb authored by Thuc Pham's avatar Thuc Pham Committed by GitHub
Browse files

chore: move wikipedia tool to create-llama (#498)

parent 131e63ae
No related branches found
Tags v0.3.23
No related merge requests found
---
"create-llama": patch
---
chore: move wikipedia tool to create-llama
......@@ -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:
......@@ -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
......@@ -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),
);
}
};
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";
......@@ -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[]>;
......@@ -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)];
},
......
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);
}
}
......@@ -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",
......
......@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment