├── package.json ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "langchain-autogpt-nodejs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "type": "module", 13 | "dependencies": { 14 | "dotenv": "^16.0.3", 15 | "hnswlib-node": "^1.4.2", 16 | "langchain": "^0.0.66" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoGPT with Langchain in Node.js 2 | 3 | This repository contains a Node.js boilerplate for getting started with AutoGPT using Langchain. The project demonstrates how to set up and use the experimental AutoGPT implementation in a Node.js environment. It provides a foundation for building applications that leverage the power of generative language models and external APIs to create dynamic and interactive content. 4 | 5 | ## Prerequisites 6 | 7 | - Node.js version 18 or higher 8 | - OpenAI API key (obtain from https://platform.openai.com/account/api-keys) 9 | - SERP API key (obtain from https://serpapi.com/) 10 | 11 | ## Getting Started 12 | 13 | 1. Clone the repository: 14 | 15 | git clone https://github.com/your-username/autogpt-langchain-nodejs.git 16 | cd autogpt-langchain-nodejs 17 | 18 | 19 | 2. Install dependencies: 20 | 21 | npm install langchain hnswlib dotenv 22 | 23 | 24 | 3. Create a `.env` file in the root directory and add your API keys: 25 | 26 | OPENAI_API_KEY=your_openai_api_key 27 | SERPAPI_API_KEY=your_serp_api_key 28 | 29 | 30 | 4. Run the `index.js` script to see AutoGPT in action: 31 | 32 | node index.js 33 | 34 | 35 | ## How It Works 36 | 37 | The `index.js` script performs the following steps: 38 | 39 | - Imports necessary modules from Langchain, including AutoGPT, ReadFileTool, WriteFileTool, SerpAPI, NodeFileStore, HNSWLib, OpenAIEmbeddings, and ChatOpenAI. 40 | - Configures dotenv to manage environment variables. 41 | - Instantiates NodeFileStore, tools for file operations and SERP API, and HNSWLib with OpenAI embeddings. 42 | - Creates an AutoGPT instance with the ChatOpenAI model, tools, and memory. 43 | - Demonstrates how to use AutoGPT to perform tasks and generate content. 44 | 45 | ## Customization 46 | 47 | This boilerplate provides a starting point for your own projects. You can customize the code to create a wide range of applications, such as generating dynamic web content, creating interactive chatbots, and more. Feel free to experiment and explore the capabilities of AutoGPT and Langchain! 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Step 1: Obtain an OpenAI API key from https://platform.openai.com/account/api-keys 2 | // Step 2: Obtain a SERP API key from https://serpapi.com/ 3 | // Step 3: Run npm init -y to create a package.json file 4 | // Run touch index.js .env to create an index.js file and a .env file 5 | // Step 4: Ensure Node.js version is at least 18 by running node -v 6 | // If the version is below 18, upgrade to the latest Node.js version 7 | // Step 5: Install langchain and HNSWLib with npm install langchain hnswlib 8 | // Step 6: Import necessary modules 9 | 10 | import { AutoGPT } from "langchain/experimental/autogpt"; // Import the experimental AutoGPT implementation 11 | import { ReadFileTool, WriteFileTool, SerpAPI } from "langchain/tools"; // Import various tools for file operations and SERP API 12 | import { NodeFileStore } from "langchain/stores/file/node"; // Import NodeFileStore for file storage in Node.js 13 | import { HNSWLib } from "langchain/vectorstores/hnswlib"; // Import HNSWLib for efficient similarity search 14 | import { OpenAIEmbeddings } from "langchain/embeddings/openai"; // Import pre-trained OpenAI embeddings 15 | import { ChatOpenAI } from "langchain/chat_models/openai"; // Import the ChatOpenAI class 16 | import dotenv from "dotenv"; // Import dotenv for managing environment variables 17 | 18 | // Step 7: Configure dotenv 19 | dotenv.config(); 20 | 21 | // Step 8: Instantiate NodeFileStore 22 | const store = new NodeFileStore(); 23 | 24 | // Step 9: Create tools for file operations and SERP API 25 | const tools = [ 26 | new ReadFileTool({ store }), 27 | new WriteFileTool({ store }), 28 | new SerpAPI(process.env.SERPAPI_API_KEY, { 29 | engine: "google", 30 | hl: "en", 31 | gl: "us", 32 | }), 33 | ]; 34 | 35 | // Step 10: Set up HNSWLib with OpenAI embeddings 36 | const vectorStore = new HNSWLib(new OpenAIEmbeddings(), { 37 | space: "cosine", 38 | numDimensions: 1536, 39 | }); 40 | 41 | // Step 11: Create AutoGPT instance with ChatOpenAI model, tools, and memory 42 | const autogpt = AutoGPT.fromLLMAndTools( 43 | new ChatOpenAI({ temperature: 0 }), 44 | tools, 45 | { 46 | memory: vectorStore.asRetriever(), 47 | aiName: "Developer Digest Assistant", 48 | aiRole: "Assistant", 49 | } 50 | ); 51 | 52 | // Step 12: Use AutoGPT to create an index.html file with a working stock chart of Apple's stock price 53 | await autogpt.run([ 54 | "Create an index.html file with a working stock chart of Apple's stock price using Chart.js and with stock data from google finance", 55 | ]); 56 | --------------------------------------------------------------------------------