mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 19:00:13 +01:00
feat(tools): add structured Wolfram tool
This commit is contained in:
parent
d0be2e6f4a
commit
4353d42035
3 changed files with 77 additions and 2 deletions
|
|
@ -5,6 +5,7 @@ const OpenAICreateImage = require('./DALL-E');
|
||||||
const StructuredSD = require('./structured/StableDiffusion');
|
const StructuredSD = require('./structured/StableDiffusion');
|
||||||
const StableDiffusionAPI = require('./StableDiffusion');
|
const StableDiffusionAPI = require('./StableDiffusion');
|
||||||
const WolframAlphaAPI = require('./Wolfram');
|
const WolframAlphaAPI = require('./Wolfram');
|
||||||
|
const StructuredWolfram = require('./structured/Wolfram');
|
||||||
const SelfReflectionTool = require('./SelfReflection');
|
const SelfReflectionTool = require('./SelfReflection');
|
||||||
const availableTools = require('./manifest.json');
|
const availableTools = require('./manifest.json');
|
||||||
|
|
||||||
|
|
@ -14,8 +15,9 @@ module.exports = {
|
||||||
HttpRequestTool,
|
HttpRequestTool,
|
||||||
AIPluginTool,
|
AIPluginTool,
|
||||||
OpenAICreateImage,
|
OpenAICreateImage,
|
||||||
StructuredSD,
|
|
||||||
StableDiffusionAPI,
|
StableDiffusionAPI,
|
||||||
|
StructuredSD,
|
||||||
WolframAlphaAPI,
|
WolframAlphaAPI,
|
||||||
|
StructuredWolfram,
|
||||||
SelfReflectionTool
|
SelfReflectionTool
|
||||||
}
|
}
|
||||||
|
|
|
||||||
72
api/app/langchain/tools/structured/Wolfram.js
Normal file
72
api/app/langchain/tools/structured/Wolfram.js
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
/* eslint-disable no-useless-escape */
|
||||||
|
const axios = require('axios');
|
||||||
|
const { StructuredTool } = require('langchain/tools');
|
||||||
|
const { z } = require('zod');
|
||||||
|
|
||||||
|
class WolframAlphaAPI extends StructuredTool {
|
||||||
|
constructor(fields) {
|
||||||
|
super();
|
||||||
|
this.name = 'wolfram';
|
||||||
|
this.apiKey = fields.WOLFRAM_APP_ID || this.getAppId();
|
||||||
|
this.description = `WolframAlpha offers computation, math, curated knowledge, and real-time data. It handles natural language queries and performs complex calculations.
|
||||||
|
Guidelines include:
|
||||||
|
- Use English for queries and inform users if information isn't from Wolfram.
|
||||||
|
- Use "6*10^14" for exponent notation and single-line strings for input.
|
||||||
|
- Use Markdown for formulas and simplify queries to keywords.
|
||||||
|
- Use single-letter variable names and named physical constants.
|
||||||
|
- Include a space between compound units and consider equations without units when solving.
|
||||||
|
- Make separate calls for each property and choose relevant 'Assumptions' if results aren't relevant.
|
||||||
|
- The tool also performs data analysis, plotting, and information retrieval.`;
|
||||||
|
this.schema = z.object({
|
||||||
|
nl_query: z.string().describe("Natural language query to WolframAlpha following the guidelines"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchRawText(url) {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(url, { responseType: 'text' });
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error fetching raw text: ${error}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getAppId() {
|
||||||
|
const appId = process.env.WOLFRAM_APP_ID || '';
|
||||||
|
if (!appId) {
|
||||||
|
throw new Error('Missing WOLFRAM_APP_ID environment variable.');
|
||||||
|
}
|
||||||
|
return appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
createWolframAlphaURL(query) {
|
||||||
|
// Clean up query
|
||||||
|
const formattedQuery = query.replaceAll(/`/g, '').replaceAll(/\n/g, ' ');
|
||||||
|
const baseURL = 'https://www.wolframalpha.com/api/v1/llm-api';
|
||||||
|
const encodedQuery = encodeURIComponent(formattedQuery);
|
||||||
|
const appId = this.apiKey || this.getAppId();
|
||||||
|
const url = `${baseURL}?input=${encodedQuery}&appid=${appId}`;
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
async _call(data) {
|
||||||
|
try {
|
||||||
|
const { nl_query } = data;
|
||||||
|
const url = this.createWolframAlphaURL(nl_query);
|
||||||
|
const response = await this.fetchRawText(url);
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
console.log('Error data:', error.response.data);
|
||||||
|
return error.response.data;
|
||||||
|
} else {
|
||||||
|
console.log(`Error querying Wolfram Alpha`, error.message);
|
||||||
|
// throw error;
|
||||||
|
return 'There was an error querying Wolfram Alpha.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WolframAlphaAPI;
|
||||||
|
|
@ -13,6 +13,7 @@ const {
|
||||||
AIPluginTool,
|
AIPluginTool,
|
||||||
GoogleSearchAPI,
|
GoogleSearchAPI,
|
||||||
WolframAlphaAPI,
|
WolframAlphaAPI,
|
||||||
|
StructuredWolfram,
|
||||||
HttpRequestTool,
|
HttpRequestTool,
|
||||||
OpenAICreateImage,
|
OpenAICreateImage,
|
||||||
StableDiffusionAPI,
|
StableDiffusionAPI,
|
||||||
|
|
@ -76,7 +77,7 @@ const loadTools = async ({ user, model, functions = null, tools = [], options =
|
||||||
const toolConstructors = {
|
const toolConstructors = {
|
||||||
calculator: Calculator,
|
calculator: Calculator,
|
||||||
google: GoogleSearchAPI,
|
google: GoogleSearchAPI,
|
||||||
wolfram: WolframAlphaAPI,
|
wolfram: functions ? StructuredWolfram : WolframAlphaAPI,
|
||||||
'dall-e': OpenAICreateImage,
|
'dall-e': OpenAICreateImage,
|
||||||
'stable-diffusion': functions ? StructuredSD : StableDiffusionAPI
|
'stable-diffusion': functions ? StructuredSD : StableDiffusionAPI
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue