AI-agents that automatically generate and use Langchain Tools and ChatGPT plugins
535
Stars
36
Forks
18
Watchers
4
Issues
Toolkit 提供了一种生成和使用 AI 插件的便捷方式。只需描述插件的功能,即可自动生成 🦜 🔗 LangChain 插件代码。
你也可以访问 toolkit.club 在线体验托管版本。
本项目由 Pal 团队开发。Pal 致力于让创建 AI 助手变得简单,通过接入多种工具,帮助你的客户和团队提高工作效率,并让 AI 深度了解你的产品。
你可以引入此包,并根据代码生成工具。
yarn
yarn add @heypal/toolkit-ai
pnpm
pnpm add @heypal/toolkit-ai
import Toolkit from '@heypal/toolkit-ai';
// 可选:在此处设置 OpenAI API key
// 否则底层模型将使用环境变量 OPENAI_API_KEY
const toolkit = new Toolkit({
openAIApiKey: '',
});
(async () => {
const tool = await toolkit.generateTool({
name: 'Temperature Converter',
description:
'Converts a temperature from Farenheit, Celsius, or Kelvin to any other unit.',
});
console.log(tool.langChainCode);
})();
我们还制作了一个简易库,你可以通过它来测试你生成的工具: hey-pal/langchain-tools-demo
输出结果将是一个 Tool 对象,其中包含 LangchainCode 的实现。以下是 LangChain 工具生成的示例输出:
LangChain 工具代码输出示例
import { Tool } from 'langchain/agents';
import Ajv from 'ajv';
// 以下是工具被调用时实际运行的代码
function call({ value, fromUnit, toUnit }) {
let convertedValue;
if (fromUnit === "Fahrenheit") {
if (toUnit === "Celsius") {
convertedValue = ((value - 32) * 5) / 9;
} else if (toUnit === "Kelvin") {
convertedValue = ((value - 32) * 5) / 9 + 273.15;
} else {
convertedValue = value;
}
} else if (fromUnit === "Celsius") {
if (toUnit === "Fahrenheit") {
convertedValue = (value * 9) / 5 + 32;
} else if (toUnit === "Kelvin") {
convertedValue = value + 273.15;
} else {
convertedValue = value;
}
} else if (fromUnit === "Kelvin") {
if (toUnit === "Fahrenheit") {
convertedValue = ((value - 273.15) * 9) / 5 + 32;
} else if (toUnit === "Celsius") {
convertedValue = value - 273.15;
} else {
convertedValue = value;
}
}
return { convertedValue };
}
// 这是一个对应 Langchain 工具定义的类
// https://js.langchain.com/docs/modules/agents/tools/
// 它根据 schema 验证输入和输出,然后调用工具代码
class TemperatureConverter extends Tool {
name = 'temperature-converter';
description = `Converts a temperature from Fahrenheit, Celsius, or Kelvin to any other unit. The action input should adhere to this JSON schema:
{{"type":"object","properties":{{"value":{{"type":"number","description":"The temperature value to be converted."}},"fromUnit":{{"type":"string","enum":["Fahrenheit","Celsius","Kelvin"],"description":"The unit of the input temperature value."}},"toUnit":{{"type":"string","enum":["Fahrenheit","Celsius","Kelvin"],"description":"The unit to which the temperature value should be converted."}}}},"required":["value","fromUnit","toUnit"]}}`;
ajv = new Ajv();
inputSchema = {
"type": "object",
"properties": {
"value": {
"type": "number",
"description": "The temperature value to be converted."
},
"fromUnit": {
"type": "string",
"enum": [
"Fahrenheit",
"Celsius",
"Kelvin"
],
"description": "The unit of the input temperature value."
},
"toUnit": {
"type": "string",
"enum": [
"Fahrenheit",
"Celsius",
"Kelvin"
],
"description": "The unit to which the temperature value should be converted."
}
},
"required": [
"value",
"fromUnit",
"toUnit"
]
};
outputSchema = {
"type": "object",
"properties": {
"convertedValue": {
"type": "number",
"description": "The converted temperature value in the desired unit."
}
},
"required": [
"convertedValue"
]
};
validate(data, schema) {
if (schema) {
const validateSchema = this.ajv.compile(schema);
if (!validateSchema(data)) {
throw new Error(this.ajv.errorsText(validateSchema.errors));
}
}
}
async _call(arg) {
let output;
try {
const input = JSON.parse(arg);
this.validate(input, this.inputSchema);
output = await call(input);
try {
this
Toolkit AI 是一个基于 TypeScript 构建的开发工具,旨在通过自然语言描述自动生成 LangChain 工具和 ChatGPT 插件。它通过引入智能代理机制,简化了 AI 扩展功能的创建过程,让开发者无需手动编写繁琐代码即可快速构建 AI 助手的功能插件。
利用自然语言描述自动生成可直接运行的 LangChain 工具代码。
提供命令行界面(CLI)支持,实现高效的工具开发与交互体验。
集成自评估代理功能,能够根据反馈自动优化和持续改进生成的工具逻辑。
支持通过 npm 包轻松集成至现有项目中,降低工具开发的准入门槛。
该项目适用于需要快速扩展 AI 助手能力的开发者和团队,特别适合在构建智能应用时,希望通过自动化方式快速适配多种外部工具接口的使用场景。