1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import { v4 as uuidv4 } from 'uuid';
import { Networking } from '../../../../Network';
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType } from '../types/tool_types';
const wikipediaToolParams = [
{
name: 'title',
type: 'string',
description: 'The title of the Wikipedia article to search',
required: true,
},
] as const;
type WikipediaToolParamsType = typeof wikipediaToolParams;
export class WikipediaTool extends BaseTool<WikipediaToolParamsType> {
private _addLinkedUrlDoc: (url: string, id: string) => void;
constructor(addLinkedUrlDoc: (url: string, id: string) => void) {
super(
'wikipedia',
'Search Wikipedia and return a summary',
wikipediaToolParams,
'Provide simply the title you want to search on Wikipedia and nothing more. If re-using this tool, try a different title for different information.',
'Returns a summary from searching an article title on Wikipedia'
);
this._addLinkedUrlDoc = addLinkedUrlDoc;
}
async execute(args: ParametersType<WikipediaToolParamsType>): Promise<Observation[]> {
try {
const { text } = await Networking.PostToServer('/getWikipediaSummary', { title: args.title });
const id = uuidv4();
const url = `https://en.wikipedia.org/wiki/${args.title.replace(/ /g, '_')}`;
this._addLinkedUrlDoc(url, id);
return [
{
type: 'text',
text: `<chunk chunk_id="${id}" chunk_type="url"> ${text} </chunk>`,
},
];
} catch (error) {
console.log(error);
return [{ type: 'text', text: 'An error occurred while fetching the article.' }];
}
}
}
|