Skip to content

Commit 4353d42

Browse files
committed
feat(tools): add structured Wolfram tool
1 parent d0be2e6 commit 4353d42

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

api/app/langchain/tools/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const OpenAICreateImage = require('./DALL-E');
55
const StructuredSD = require('./structured/StableDiffusion');
66
const StableDiffusionAPI = require('./StableDiffusion');
77
const WolframAlphaAPI = require('./Wolfram');
8+
const StructuredWolfram = require('./structured/Wolfram');
89
const SelfReflectionTool = require('./SelfReflection');
910
const availableTools = require('./manifest.json');
1011

@@ -14,8 +15,9 @@ module.exports = {
1415
HttpRequestTool,
1516
AIPluginTool,
1617
OpenAICreateImage,
17-
StructuredSD,
1818
StableDiffusionAPI,
19+
StructuredSD,
1920
WolframAlphaAPI,
21+
StructuredWolfram,
2022
SelfReflectionTool
2123
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-useless-escape */
2+
const axios = require('axios');
3+
const { StructuredTool } = require('langchain/tools');
4+
const { z } = require('zod');
5+
6+
class WolframAlphaAPI extends StructuredTool {
7+
constructor(fields) {
8+
super();
9+
this.name = 'wolfram';
10+
this.apiKey = fields.WOLFRAM_APP_ID || this.getAppId();
11+
this.description = `WolframAlpha offers computation, math, curated knowledge, and real-time data. It handles natural language queries and performs complex calculations.
12+
Guidelines include:
13+
- Use English for queries and inform users if information isn't from Wolfram.
14+
- Use "6*10^14" for exponent notation and single-line strings for input.
15+
- Use Markdown for formulas and simplify queries to keywords.
16+
- Use single-letter variable names and named physical constants.
17+
- Include a space between compound units and consider equations without units when solving.
18+
- Make separate calls for each property and choose relevant 'Assumptions' if results aren't relevant.
19+
- The tool also performs data analysis, plotting, and information retrieval.`;
20+
this.schema = z.object({
21+
nl_query: z.string().describe("Natural language query to WolframAlpha following the guidelines"),
22+
});
23+
}
24+
25+
async fetchRawText(url) {
26+
try {
27+
const response = await axios.get(url, { responseType: 'text' });
28+
return response.data;
29+
} catch (error) {
30+
console.error(`Error fetching raw text: ${error}`);
31+
throw error;
32+
}
33+
}
34+
35+
getAppId() {
36+
const appId = process.env.WOLFRAM_APP_ID || '';
37+
if (!appId) {
38+
throw new Error('Missing WOLFRAM_APP_ID environment variable.');
39+
}
40+
return appId;
41+
}
42+
43+
createWolframAlphaURL(query) {
44+
// Clean up query
45+
const formattedQuery = query.replaceAll(/`/g, '').replaceAll(/\n/g, ' ');
46+
const baseURL = 'https://www.wolframalpha.com/api/v1/llm-api';
47+
const encodedQuery = encodeURIComponent(formattedQuery);
48+
const appId = this.apiKey || this.getAppId();
49+
const url = `${baseURL}?input=${encodedQuery}&appid=${appId}`;
50+
return url;
51+
}
52+
53+
async _call(data) {
54+
try {
55+
const { nl_query } = data;
56+
const url = this.createWolframAlphaURL(nl_query);
57+
const response = await this.fetchRawText(url);
58+
return response;
59+
} catch (error) {
60+
if (error.response && error.response.data) {
61+
console.log('Error data:', error.response.data);
62+
return error.response.data;
63+
} else {
64+
console.log(`Error querying Wolfram Alpha`, error.message);
65+
// throw error;
66+
return 'There was an error querying Wolfram Alpha.';
67+
}
68+
}
69+
}
70+
}
71+
72+
module.exports = WolframAlphaAPI;

api/app/langchain/tools/util/handleTools.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
AIPluginTool,
1414
GoogleSearchAPI,
1515
WolframAlphaAPI,
16+
StructuredWolfram,
1617
HttpRequestTool,
1718
OpenAICreateImage,
1819
StableDiffusionAPI,
@@ -76,7 +77,7 @@ const loadTools = async ({ user, model, functions = null, tools = [], options =
7677
const toolConstructors = {
7778
calculator: Calculator,
7879
google: GoogleSearchAPI,
79-
wolfram: WolframAlphaAPI,
80+
wolfram: functions ? StructuredWolfram : WolframAlphaAPI,
8081
'dall-e': OpenAICreateImage,
8182
'stable-diffusion': functions ? StructuredSD : StableDiffusionAPI
8283
};

0 commit comments

Comments
 (0)