diff options
| author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-12-19 11:45:00 -0500 |
|---|---|---|
| committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-12-19 11:45:00 -0500 |
| commit | f915013d2ccfaeb7f04bf8bfea57e6d7d1f66b81 (patch) | |
| tree | 69cde0426434a687a096ebe987145628afd145b0 /src/client/views/nodes/chatbot/tools/DictionaryTool.ts | |
| parent | 9b4c554cca11f5c3105085b54646e684dd235f1d (diff) | |
image generation works better
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/DictionaryTool.ts')
| -rw-r--r-- | src/client/views/nodes/chatbot/tools/DictionaryTool.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/client/views/nodes/chatbot/tools/DictionaryTool.ts b/src/client/views/nodes/chatbot/tools/DictionaryTool.ts new file mode 100644 index 000000000..fa554e7b3 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/DictionaryTool.ts @@ -0,0 +1,64 @@ +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; +import { BaseTool } from './BaseTool'; + +// Define the tool's parameters +const dictionaryToolParams = [ + { + name: 'word', + type: 'string', + description: 'The word to look up in the dictionary.', + required: true, + }, +] as const; + +type DictionaryToolParamsType = typeof dictionaryToolParams; + +// Define the tool's metadata and rules +const dictionaryToolInfo: ToolInfo<DictionaryToolParamsType> = { + name: 'dictionary', + citationRules: 'No citation needed.', + parameterRules: dictionaryToolParams, + description: 'Fetches the definition of a given word using an open dictionary API.', +}; + +export class DictionaryTool extends BaseTool<DictionaryToolParamsType> { + constructor() { + super(dictionaryToolInfo); + } + + async execute(args: ParametersType<DictionaryToolParamsType>): Promise<Observation[]> { + const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${args.word}`; + + try { + const response = await fetch(url); + const data = await response.json(); + + // Handle cases where the word is not found + if (data.title === 'No Definitions Found') { + return [ + { + type: 'text', + text: `Sorry, I couldn't find a definition for the word "${args.word}".`, + }, + ]; + } + + // Extract the first definition + const definition = data[0]?.meanings[0]?.definitions[0]?.definition; + return [ + { + type: 'text', + text: `The definition of "${args.word}" is: ${definition}`, + }, + ]; + } catch (error) { + return [ + { + type: 'text', + text: `An error occurred while fetching the definition: ${error}`, + }, + ]; + } + } +} |
