-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
wolframalpha.ts
37 lines (29 loc) · 932 Bytes
/
wolframalpha.ts
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
import { Tool, type ToolParams } from "@langchain/core/tools";
/**
* @example
* ```typescript
* const tool = new WolframAlphaTool({
* appid: "YOUR_APP_ID",
* });
* const res = await tool.invoke("What is 2 * 2?");
* ```
*/
export class WolframAlphaTool extends Tool {
appid: string;
name = "wolfram_alpha";
description = `A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query.`;
constructor(fields: ToolParams & { appid: string }) {
super(fields);
this.appid = fields.appid;
}
static lc_name() {
return "WolframAlphaTool";
}
async _call(query: string): Promise<string> {
const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${
this.appid
}&input=${encodeURIComponent(query)}`;
const res = await fetch(url);
return res.text();
}
}