📦 fix: npm warnings; chore: bump deprecated packages (#4707)

* chore: bump langchain deps to address vulnerability warnings

* chore: bump community package and install textsplitters package

* fix: update expected result in tokenSplit tests for accuracy

* chore: remove CodeSherpa tools

* chore: remove E2B tools and loadToolSuite

* chore: remove CodeBrew tool and update related references

* chore: remove HumanTool and ChatTool, update tool references

* chore: remove Zapier tool from manifest.json and update SerpAPI

* chore: remove basic tools

* chore: update import path for RecursiveCharacterTextSplitter

* chore: update import path for DynamicStructuredTool

* chore: remove extractionChain.js and update tool filtering logic

* chore: npm audit fix

* chore: bump google packages

* chore: update DALL-E tool to DALL-E-3 and adjust authentication logic

* ci: update message classes

* chore: elliptic npm audit fix

* chore: update CallbackManager import and remove deprecated tool handling logic

* chore: imports order

* chore: remove unused code

---------

Co-authored-by: Max Sanna <max@maxsanna.com>
This commit is contained in:
Danny Avila 2024-11-12 18:51:32 -05:00 committed by GitHub
parent d012da0065
commit 95201908e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 1446 additions and 3109 deletions

View file

@ -1,23 +0,0 @@
const { StructuredTool } = require('langchain/tools');
const { z } = require('zod');
// proof of concept
class ChatTool extends StructuredTool {
constructor({ onAgentAction }) {
super();
this.handleAction = onAgentAction;
this.name = 'talk_to_user';
this.description =
'Use this to chat with the user between your use of other tools/plugins/APIs. You should explain your motive and thought process in a conversational manner, while also analyzing the output of tools/plugins, almost as a self-reflection step to communicate if you\'ve arrived at the correct answer or used the tools/plugins effectively.';
this.schema = z.object({
message: z.string().describe('Message to the user.'),
// next_step: z.string().optional().describe('The next step to take.'),
});
}
async _call({ message }) {
return `Message to user: ${message}`;
}
}
module.exports = ChatTool;

View file

@ -1,165 +0,0 @@
const { StructuredTool } = require('langchain/tools');
const axios = require('axios');
const { z } = require('zod');
const headers = {
'Content-Type': 'application/json',
};
function getServerURL() {
const url = process.env.CODESHERPA_SERVER_URL || '';
if (!url) {
throw new Error('Missing CODESHERPA_SERVER_URL environment variable.');
}
return url;
}
class RunCode extends StructuredTool {
constructor() {
super();
this.name = 'RunCode';
this.description =
'Use this plugin to run code with the following parameters\ncode: your code\nlanguage: either Python, Rust, or C++.';
this.headers = headers;
this.schema = z.object({
code: z.string().describe('The code to be executed in the REPL-like environment.'),
language: z.string().describe('The programming language of the code to be executed.'),
});
}
async _call({ code, language = 'python' }) {
// logger.debug('<--------------- Running Code --------------->', { code, language });
const response = await axios({
url: `${this.url}/repl`,
method: 'post',
headers: this.headers,
data: { code, language },
});
// logger.debug('<--------------- Sucessfully ran Code --------------->', response.data);
return response.data.result;
}
}
class RunCommand extends StructuredTool {
constructor() {
super();
this.name = 'RunCommand';
this.description =
'Runs the provided terminal command and returns the output or error message.';
this.headers = headers;
this.schema = z.object({
command: z.string().describe('The terminal command to be executed.'),
});
}
async _call({ command }) {
const response = await axios({
url: `${this.url}/command`,
method: 'post',
headers: this.headers,
data: {
command,
},
});
return response.data.result;
}
}
class CodeSherpa extends StructuredTool {
constructor(fields) {
super();
this.name = 'CodeSherpa';
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
// this.description = `A plugin for interactive code execution, and shell command execution.
// Run code: provide "code" and "language"
// - Execute Python code interactively for general programming, tasks, data analysis, visualizations, and more.
// - Pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl. If you need to install additional packages, use the \`pip install\` command.
// - When a user asks for visualization, save the plot to \`static/images/\` directory, and embed it in the response using \`http://localhost:3333/static/images/\` URL.
// - Always save all media files created to \`static/images/\` directory, and embed them in responses using \`http://localhost:3333/static/images/\` URL.
// Run command: provide "command" only
// - Run terminal commands and interact with the filesystem, run scripts, and more.
// - Install python packages using \`pip install\` command.
// - Always embed media files created or uploaded using \`http://localhost:3333/static/images/\` URL in responses.
// - Access user-uploaded files in \`static/uploads/\` directory using \`http://localhost:3333/static/uploads/\` URL.`;
this.description = `This plugin allows interactive code and shell command execution.
To run code, supply "code" and "language". Python has pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl. Additional ones can be installed via pip.
To run commands, provide "command" only. This allows interaction with the filesystem, script execution, and package installation using pip. Created or uploaded media files are embedded in responses using a specific URL.`;
this.schema = z.object({
code: z
.string()
.optional()
.describe(
`The code to be executed in the REPL-like environment. You must save all media files created to \`${this.url}/static/images/\` and embed them in responses with markdown`,
),
language: z
.string()
.optional()
.describe(
'The programming language of the code to be executed, you must also include code.',
),
command: z
.string()
.optional()
.describe(
'The terminal command to be executed. Only provide this if you want to run a command instead of code.',
),
});
this.RunCode = new RunCode({ url: this.url });
this.RunCommand = new RunCommand({ url: this.url });
this.runCode = this.RunCode._call.bind(this);
this.runCommand = this.RunCommand._call.bind(this);
}
async _call({ code, language, command }) {
if (code?.length > 0) {
return await this.runCode({ code, language });
} else if (command) {
return await this.runCommand({ command });
} else {
return 'Invalid parameters provided.';
}
}
}
/* TODO: support file upload */
// class UploadFile extends StructuredTool {
// constructor(fields) {
// super();
// this.name = 'UploadFile';
// this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
// this.description = 'Endpoint to upload a file.';
// this.headers = headers;
// this.schema = z.object({
// file: z.string().describe('The file to be uploaded.'),
// });
// }
// async _call(data) {
// const formData = new FormData();
// formData.append('file', fs.createReadStream(data.file));
// const response = await axios({
// url: `${this.url}/upload`,
// method: 'post',
// headers: {
// ...this.headers,
// 'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
// },
// data: formData,
// });
// return response.data;
// }
// }
// module.exports = [
// RunCode,
// RunCommand,
// // UploadFile
// ];
module.exports = CodeSherpa;

View file

@ -1,121 +0,0 @@
const { StructuredTool } = require('langchain/tools');
const axios = require('axios');
const { z } = require('zod');
function getServerURL() {
const url = process.env.CODESHERPA_SERVER_URL || '';
if (!url) {
throw new Error('Missing CODESHERPA_SERVER_URL environment variable.');
}
return url;
}
const headers = {
'Content-Type': 'application/json',
};
class RunCode extends StructuredTool {
constructor(fields) {
super();
this.name = 'RunCode';
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
this.description_for_model = `// A plugin for interactive code execution
// Guidelines:
// Always provide code and language as such: {{"code": "print('Hello World!')", "language": "python"}}
// Execute Python code interactively for general programming, tasks, data analysis, visualizations, and more.
// Pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl.If you need to install additional packages, use the \`pip install\` command.
// When a user asks for visualization, save the plot to \`static/images/\` directory, and embed it in the response using \`${this.url}/static/images/\` URL.
// Always save alls media files created to \`static/images/\` directory, and embed them in responses using \`${this.url}/static/images/\` URL.
// Always embed media files created or uploaded using \`${this.url}/static/images/\` URL in responses.
// Access user-uploaded files in\`static/uploads/\` directory using \`${this.url}/static/uploads/\` URL.
// Remember to save any plots/images created, so you can embed it in the response, to \`static/images/\` directory, and embed them as instructed before.`;
this.description =
'This plugin allows interactive code execution. Follow the guidelines to get the best results.';
this.headers = headers;
this.schema = z.object({
code: z.string().optional().describe('The code to be executed in the REPL-like environment.'),
language: z
.string()
.optional()
.describe('The programming language of the code to be executed.'),
});
}
async _call({ code, language = 'python' }) {
// logger.debug('<--------------- Running Code --------------->', { code, language });
const response = await axios({
url: `${this.url}/repl`,
method: 'post',
headers: this.headers,
data: { code, language },
});
// logger.debug('<--------------- Sucessfully ran Code --------------->', response.data);
return response.data.result;
}
}
class RunCommand extends StructuredTool {
constructor(fields) {
super();
this.name = 'RunCommand';
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
this.description_for_model = `// Run terminal commands and interact with the filesystem, run scripts, and more.
// Guidelines:
// Always provide command as such: {{"command": "ls -l"}}
// Install python packages using \`pip install\` command.
// Always embed media files created or uploaded using \`${this.url}/static/images/\` URL in responses.
// Access user-uploaded files in\`static/uploads/\` directory using \`${this.url}/static/uploads/\` URL.`;
this.description =
'A plugin for interactive shell command execution. Follow the guidelines to get the best results.';
this.headers = headers;
this.schema = z.object({
command: z.string().describe('The terminal command to be executed.'),
});
}
async _call(data) {
const response = await axios({
url: `${this.url}/command`,
method: 'post',
headers: this.headers,
data,
});
return response.data.result;
}
}
/* TODO: support file upload */
// class UploadFile extends StructuredTool {
// constructor(fields) {
// super();
// this.name = 'UploadFile';
// this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
// this.description = 'Endpoint to upload a file.';
// this.headers = headers;
// this.schema = z.object({
// file: z.string().describe('The file to be uploaded.'),
// });
// }
// async _call(data) {
// const formData = new FormData();
// formData.append('file', fs.createReadStream(data.file));
// const response = await axios({
// url: `${this.url}/upload`,
// method: 'post',
// headers: {
// ...this.headers,
// 'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
// },
// data: formData,
// });
// return response.data;
// }
// }
module.exports = [
RunCode,
RunCommand,
// UploadFile
];

View file

@ -1,155 +0,0 @@
const { z } = require('zod');
const axios = require('axios');
const { StructuredTool } = require('langchain/tools');
const { PromptTemplate } = require('langchain/prompts');
// const { ChatOpenAI } = require('langchain/chat_models/openai');
const { createExtractionChainFromZod } = require('./extractionChain');
const { logger } = require('~/config');
const envs = ['Nodejs', 'Go', 'Bash', 'Rust', 'Python3', 'PHP', 'Java', 'Perl', 'DotNET'];
const env = z.enum(envs);
const template = `Extract the correct environment for the following code.
It must be one of these values: ${envs.join(', ')}.
Code:
{input}
`;
const prompt = PromptTemplate.fromTemplate(template);
// const schema = {
// type: 'object',
// properties: {
// env: { type: 'string' },
// },
// required: ['env'],
// };
const zodSchema = z.object({
env: z.string(),
});
async function extractEnvFromCode(code, model) {
// const chatModel = new ChatOpenAI({ openAIApiKey, modelName: 'gpt-4-0613', temperature: 0 });
const chain = createExtractionChainFromZod(zodSchema, model, { prompt, verbose: true });
const result = await chain.run(code);
logger.debug('<--------------- extractEnvFromCode --------------->');
logger.debug(result);
return result.env;
}
function getServerURL() {
const url = process.env.E2B_SERVER_URL || '';
if (!url) {
throw new Error('Missing E2B_SERVER_URL environment variable.');
}
return url;
}
const headers = {
'Content-Type': 'application/json',
'openai-conversation-id': 'some-uuid',
};
class RunCommand extends StructuredTool {
constructor(fields) {
super();
this.name = 'RunCommand';
this.url = fields.E2B_SERVER_URL || getServerURL();
this.description =
'This plugin allows interactive code execution by allowing terminal commands to be ran in the requested environment. To be used in tandem with WriteFile and ReadFile for Code interpretation and execution.';
this.headers = headers;
this.headers['openai-conversation-id'] = fields.conversationId;
this.schema = z.object({
command: z.string().describe('Terminal command to run, appropriate to the environment'),
workDir: z.string().describe('Working directory to run the command in'),
env: env.describe('Environment to run the command in'),
});
}
async _call(data) {
logger.debug(`<--------------- Running ${data} --------------->`);
const response = await axios({
url: `${this.url}/commands`,
method: 'post',
headers: this.headers,
data,
});
return JSON.stringify(response.data);
}
}
class ReadFile extends StructuredTool {
constructor(fields) {
super();
this.name = 'ReadFile';
this.url = fields.E2B_SERVER_URL || getServerURL();
this.description =
'This plugin allows reading a file from requested environment. To be used in tandem with WriteFile and RunCommand for Code interpretation and execution.';
this.headers = headers;
this.headers['openai-conversation-id'] = fields.conversationId;
this.schema = z.object({
path: z.string().describe('Path of the file to read'),
env: env.describe('Environment to read the file from'),
});
}
async _call(data) {
logger.debug(`<--------------- Reading ${data} --------------->`);
const response = await axios.get(`${this.url}/files`, { params: data, headers: this.headers });
return response.data;
}
}
class WriteFile extends StructuredTool {
constructor(fields) {
super();
this.name = 'WriteFile';
this.url = fields.E2B_SERVER_URL || getServerURL();
this.model = fields.model;
this.description =
'This plugin allows interactive code execution by first writing to a file in the requested environment. To be used in tandem with ReadFile and RunCommand for Code interpretation and execution.';
this.headers = headers;
this.headers['openai-conversation-id'] = fields.conversationId;
this.schema = z.object({
path: z.string().describe('Path to write the file to'),
content: z.string().describe('Content to write in the file. Usually code.'),
env: env.describe('Environment to write the file to'),
});
}
async _call(data) {
let { env, path, content } = data;
logger.debug(`<--------------- environment ${env} typeof ${typeof env}--------------->`);
if (env && !envs.includes(env)) {
logger.debug(`<--------------- Invalid environment ${env} --------------->`);
env = await extractEnvFromCode(content, this.model);
} else if (!env) {
logger.debug('<--------------- Undefined environment --------------->');
env = await extractEnvFromCode(content, this.model);
}
const payload = {
params: {
path,
env,
},
data: {
content,
},
};
logger.debug('Writing to file', JSON.stringify(payload));
await axios({
url: `${this.url}/files`,
method: 'put',
headers: this.headers,
...payload,
});
return `Successfully written to ${path} in ${env}`;
}
}
module.exports = [RunCommand, ReadFile, WriteFile];

View file

@ -1,52 +0,0 @@
const { zodToJsonSchema } = require('zod-to-json-schema');
const { PromptTemplate } = require('langchain/prompts');
const { JsonKeyOutputFunctionsParser } = require('langchain/output_parsers');
const { LLMChain } = require('langchain/chains');
function getExtractionFunctions(schema) {
return [
{
name: 'information_extraction',
description: 'Extracts the relevant information from the passage.',
parameters: {
type: 'object',
properties: {
info: {
type: 'array',
items: {
type: schema.type,
properties: schema.properties,
required: schema.required,
},
},
},
required: ['info'],
},
},
];
}
const _EXTRACTION_TEMPLATE = `Extract and save the relevant entities mentioned in the following passage together with their properties.
Passage:
{input}
`;
function createExtractionChain(schema, llm, options = {}) {
const { prompt = PromptTemplate.fromTemplate(_EXTRACTION_TEMPLATE), ...rest } = options;
const functions = getExtractionFunctions(schema);
const outputParser = new JsonKeyOutputFunctionsParser({ attrName: 'info' });
return new LLMChain({
llm,
prompt,
llmKwargs: { functions },
outputParser,
tags: ['openai_functions', 'extraction'],
...rest,
});
}
function createExtractionChainFromZod(schema, llm) {
return createExtractionChain(zodToJsonSchema(schema), llm);
}
module.exports = {
createExtractionChain,
createExtractionChainFromZod,
};