LibreChat/api/app/clients/tools/structured/Wolfram.js
Danny Avila d3e7627046
refactor(plugins): Improve OpenAPI handling, Show Multiple Plugins, & Other Improvements (#845)
* feat(PluginsClient.js): add conversationId to options object in the constructor
feat(PluginsClient.js): add support for Code Interpreter plugin
feat(PluginsClient.js): add support for Code Interpreter plugin in the availableTools manifest
feat(CodeInterpreter.js): add CodeInterpreterTools module
feat(CodeInterpreter.js): add RunCommand class
feat(CodeInterpreter.js): add ReadFile class
feat(CodeInterpreter.js): add WriteFile class
feat(handleTools.js): add support for loading Code Interpreter plugin

* chore(api): update langchain dependency to version 0.0.123

* fix(CodeInterpreter.js): add support for extracting environment from code
fix(WriteFile.js): add support for extracting environment from data
fix(extractionChain.js): add utility functions for creating extraction chain from Zod schema
fix(handleTools.js): refactor getOpenAIKey function to handle user-provided API key
fix(handleTools.js): pass model and openAIApiKey to CodeInterpreter constructor

* fix(tools): rename CodeInterpreterTools to E2BTools
fix(tools): rename code_interpreter pluginKey to e2b_code_interpreter

* chore(PluginsClient.js): comment out unused import and function findMessageContent
feat(PluginsClient.js): add support for CodeSherpa plugin
feat(PluginsClient.js): add CodeSherpaTools to available tools
feat(PluginsClient.js): update manifest.json to include CodeSherpa plugin
feat(CodeSherpaTools.js): create RunCode and RunCommand classes for CodeSherpa plugin

feat(E2BTools.js): Add E2BTools module for extracting environment from code and running commands, reading and writing files
fix(codesherpa.js): Remove codesherpa module as it is no longer needed

feat(handleTools.js): add support for CodeSherpaTools in loadTools function
feat(loadToolSuite.js): create loadToolSuite utility function to load a suite of tools

* feat(PluginsClient.js): add support for CodeSherpa v2 plugin
feat(PluginsClient.js): add CodeSherpa v1 plugin to available tools
feat(PluginsClient.js): add CodeSherpa v2 plugin to available tools
feat(PluginsClient.js): update manifest.json for CodeSherpa v1 plugin
feat(PluginsClient.js): update manifest.json for CodeSherpa v2 plugin
feat(CodeSherpa.js): implement CodeSherpa plugin for interactive code and shell command execution
feat(CodeSherpaTools.js): implement RunCode and RunCommand plugins for CodeSherpa v1
feat(CodeSherpaTools.js): update RunCode and RunCommand plugins for CodeSherpa v2

fix(handleTools.js): add CodeSherpa import statement
fix(handleTools.js): change pluginKey from 'codesherpa' to 'codesherpa_tools'
fix(handleTools.js): remove model and openAIApiKey from options object in e2b_code_interpreter tool
fix(handleTools.js): remove openAIApiKey from options object in codesherpa_tools tool
fix(loadToolSuite.js): remove model and openAIApiKey parameters from loadToolSuite function

* feat(initializeFunctionsAgent.js): add prefix to agentArgs in initializeFunctionsAgent function

The prefix is added to the agentArgs in the initializeFunctionsAgent function. This prefix is used to provide instructions to the agent when it receives any instructions from a webpage, plugin, or other tool. The agent will notify the user immediately and ask them if they wish to carry out or ignore the instructions.

* feat(PluginsClient.js): add ChatTool to the list of tools if it meets the conditions
feat(tools/index.js): import and export ChatTool
feat(ChatTool.js): create ChatTool class with necessary properties and methods

* fix(initializeFunctionsAgent.js): update PREFIX message to include sharing all output from the tool
fix(E2BTools.js): update descriptions for RunCommand, ReadFile, and WriteFile plugins to provide more clarity and context

* chore: rebuild package-lock after rebase

* chore: remove deleted file from rebase

* wip: refactor plugin message handling to mirror chat.openai.com, handle incoming stream for plugin use

* wip: new plugin handling

* wip: show multiple plugins handling

* feat(plugins): save new plugins array

* chore: bump langchain

* feat(experimental): support streaming in between plugins

* refactor(PluginsClient): factor out helper methods to avoid bloating the class, refactor(gptPlugins): use agent action for mapping the name of action

* fix(handleTools): fix tests by adding condition to return original toolFunctions map

* refactor(MessageContent): Allow the last index to be last in case it has text (may change with streaming)

* feat(Plugins): add handleParsingErrors, useful when LLM does not invoke function params

* chore: edit out experimental codesherpa integration

* refactor(OpenAPIPlugin): rework tool to be 'function-first', as the spec functions are explicitly passed to agent model

* refactor(initializeFunctionsAgent): improve error handling and system message

* refactor(CodeSherpa, Wolfram): optimize token usage by delegating bulk of instructions to system message

* style(Plugins): match official style with input/outputs

* chore: remove unnecessary console logs used for testing

* fix(abortMiddleware): render markdown when message is aborted

* feat(plugins): add BrowserOp

* refactor(OpenAPIPlugin): improve prompt handling

* fix(useGenerations): hide edit button when message is submitting/streaming

* refactor(loadSpecs): optimize OpenAPI spec loading by only loading requested specs instead of all of them

* fix(loadSpecs): will retain original behavior when no tools are passed to the function

* fix(MessageContent): ensure cursor only shows up for last message and last display index
fix(Message): show legacy plugin and pass isLast to Content

* chore: remove console.logs

* docs: update docs based on breaking changes and new features
refactor(structured/SD): use description_for_model for detailed prompting

* docs(azure): make plugins section more clear

* refactor(structured/SD): change default payload to SD-WebUI to prefer realism and config for SDXL

* refactor(structured/SD): further improve system message prompt

* docs: update breaking changes after rebase

* refactor(MessageContent): factor out EditMessage, types, Container to separate files, rename Content -> Markdown

* fix(CodeInterpreter): linting errors

* chore: reduce browser console logs from message streams

* chore: re-enable debug logs for plugins/langchain to help with user troubleshooting

* chore(manifest.json): add [Experimental] tag to CodeInterpreter plugins, which are not intended as the end-all be-all implementation of this feature for Librechat
2023-08-28 12:03:08 -04:00

92 lines
5 KiB
JavaScript

/* 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_for_model = `// Access dynamic computation and curated data from WolframAlpha and Wolfram Cloud.
// General guidelines:
// - Use only getWolframAlphaResults or getWolframCloudResults endpoints.
// - Prefer getWolframAlphaResults unless Wolfram Language code should be evaluated.
// - Use getWolframAlphaResults for natural-language queries in English; translate non-English queries before sending, then respond in the original language.
// - Use getWolframCloudResults for problems solvable with Wolfram Language code.
// - Suggest only Wolfram Language for external computation.
// - Inform users if information is not from Wolfram endpoints.
// - Display image URLs with Image Markdown syntax: ![caption](https://imageURL/.../MSPStoreType=image/png&s=18). You must prefix the caption brackets with "!".
// - ALWAYS use this exponent notation: \`6*10^14\`, NEVER \`6e14\`.
// - ALWAYS use {{"input": query}} structure for queries to Wolfram endpoints; \`query\` must ONLY be a single-line string.
// - ALWAYS use proper Markdown formatting for all math, scientific, and chemical formulas, symbols, etc.: '$$\n[expression]\n$$' for standalone cases and '\( [expression] \)' when inline.
// - Format inline Wolfram Language code with Markdown code formatting.
// - Never mention your knowledge cutoff date; Wolfram may return more recent data. getWolframAlphaResults guidelines:
// - Understands natural language queries about entities in chemistry, physics, geography, history, art, astronomy, and more.
// - Performs mathematical calculations, date and unit conversions, formula solving, etc.
// - Convert inputs to simplified keyword queries whenever possible (e.g. convert "how many people live in France" to "France population").
// - Use ONLY single-letter variable names, with or without integer subscript (e.g., n, n1, n_1).
// - Use named physical constants (e.g., 'speed of light') without numerical substitution.
// - Include a space between compound units (e.g., "Ω m" for "ohm*meter").
// - To solve for a variable in an equation with units, consider solving a corresponding equation without units; exclude counting units (e.g., books), include genuine units (e.g., kg).
// - If data for multiple properties is needed, make separate calls for each property.
// - If a Wolfram Alpha result is not relevant to the query:
// -- If Wolfram provides multiple 'Assumptions' for a query, choose the more relevant one(s) without explaining the initial result. If you are unsure, ask the user to choose.
// -- Re-send the exact same 'input' with NO modifications, and add the 'assumption' parameter, formatted as a list, with the relevant values.
// -- ONLY simplify or rephrase the initial query if a more relevant 'Assumption' or other input suggestions are not provided.
// -- Do not explain each step unless user input is needed. Proceed directly to making a better API call based on the available assumptions.`;
this.description = `WolframAlpha offers computation, math, curated knowledge, and real-time data. It handles natural language queries and performs complex calculations.
Follow the guidelines to get the best results.`;
this.schema = z.object({
input: 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 { input } = data;
const url = this.createWolframAlphaURL(input);
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;