diff options
| author | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2025-03-11 17:43:05 +0100 |
|---|---|---|
| committer | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2025-03-11 17:43:05 +0100 |
| commit | fa937182bc93aa2c6faadda80ea998cdfd479b4e (patch) | |
| tree | cba8e16edcccc6fd2932173484ac444cb79abea2 /src/client/views/nodes/chatbot/tools/WikipediaTool.ts | |
| parent | cf91c46cfec6e3e36b9184764016f9c1b5c997d4 (diff) | |
| parent | 04669ffeb163688c7aefd7b5face7998252abdca (diff) | |
Merge branch 'master' of https://github.com/brown-dash/Dash-Web into DocCreatorMenu-work
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/WikipediaTool.ts')
| -rw-r--r-- | src/client/views/nodes/chatbot/tools/WikipediaTool.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts new file mode 100644 index 000000000..ee815532a --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts @@ -0,0 +1,50 @@ +import { v4 as uuidv4 } from 'uuid'; +import { Networking } from '../../../../Network'; +import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } 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; + +const wikipediaToolInfo: ToolInfo<WikipediaToolParamsType> = { + name: 'wikipedia', + citationRules: 'No citation needed.', + parameterRules: wikipediaToolParams, + description: 'Returns a summary from searching an article title on Wikipedia.', +}; + +export class WikipediaTool extends BaseTool<WikipediaToolParamsType> { + private _addLinkedUrlDoc: (url: string, id: string) => void; + + constructor(addLinkedUrlDoc: (url: string, id: string) => void) { + super(wikipediaToolInfo); + 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.' }]; + } + } +} |
