|
| 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; |
0 commit comments