Building a Smarter AGI: An Experiment with BabyAGI and LangChain

Building a Smarter AGI: An Experiment with BabyAGI and LangChain

Have you ever dreamed of building your own Artificial General Intelligence (AGI)? While AGI may still be a long way off, the development of BabyAGI, a simple AGI designed to perform specific tasks, has sparked a new wave of interest in the field. In this article, we’ll show you how we used BabyAGI and LangChain to create a smarter AGI, capable of performing a variety of tasks with increased accuracy.

The Code

To build our smarter AGI, we used the BabyAGI framework, which is built on top of the LangChain and other tools. LangChain is a python package that simplifies the creation of execution chains for AGIs. Our code builds on an earlier notebook, “baby_agi.ipynb”, but we replaced the execution chain with an agent that has access to tools for providing reliable information.

Our code requires the installation and importation of several modules, including LangChain, OpenAI, tiktoken, faiss-cpu, google-api-python-client, os, json, collections, typing, and pydantic. Once these modules are installed, the code connects to the vector store, initializes an empty vectorstore using FAISS, and defines the three LLM chains used in BabyAGI.

The three LLM chains are:

  • Task creation chain to select new tasks to add to the list
  • Task prioritization chain to re-prioritize tasks
  • Execution chain to execute the tasks

In this notebook, the execution chain is an agent instead of LLM.

Our code defines the execute_task function, which takes in a vectorstore, an execution chain, an objective, a task description, and a maximum number of search results (k). The function returns either the top search result if the task starts with “Search:” or the result of executing the task with the execution chain.

The code also defines a GoogleSearchAPIWrapper class and a function called search_internet that uses the Google Custom Search API to search the internet for information based on a given query. The search_internet function takes in a query as input, creates an instance of the GoogleSearchAPIWrapper class with an API key and CSE ID, performs the search using the wrapper, and then extracts the titles and URLs of the top search results. The results are returned as a list of dictionaries where each dictionary contains the title and URL of a single search result.

Our code also defines a code_prompt and an action_prompt using the PromptTemplate class from LangChain. These prompts are used to define two LLM chains: code_chain and action_chain, respectively. Each chain contains an OpenAI language model (LLM) with a temperature of 0, which allows for deterministic responses.

The code defines a search_tool named “Search” that utilizes the search_internet function to search the internet based on a given query. This tool is useful when you need to search the internet for information. The other two tools defined are “Code” and “Action”, which use the and methods, respectively.

All these tools are added to a list called tools. These tools are then used to create a zero-shot agent prompt using the ZeroShotAgent.create_prompt method. The resulting prompt includes the available tools, objective, task, context, and an agent scratchpad. The prefix variable provides context about the AGI performing one specific task based on the provided objective and previously completed tasks. The suffix variable contains a question to be answered by the AGI, along with its scratchpad.

Our code defines the BabyAGI class, which is a controller model for the BabyAGI agent. The BabyAGI controller composes several chains that are defined elsewhere in the code to perform an infinite loop of task execution, prioritization, and creation until a maximum number of iterations is reached (if defined).

The BabyAGI class has several methods that handle different aspects of its functioning. For example, the add_task method adds a new task to the deque list of tasks that need to be executed. The print_task_list, print_next_task, and print_task_result methods print out various aspects of the controller’s task list and current state.

The main function of the BabyAGI class is performed by the _call method, which executes the following steps:

  1. Pulls the first task from the list of tasks.
  2. Executes the task using the execute_task function that utilizes a vectorstore and an execution_chain.
  3. Stores the result of the task in Pinecone.
  4. Creates new tasks and reprioritizes the task list based on the previously executed task.

The get_next_task function takes a task_creation_chain instance and extracts new tasks from it based on incomplete ones and returns them as a list of dictionaries. The prioritize_tasks function takes a task_prioritization_chain instance and reprioritizes the existing task list by assigning task ids and returning the prioritized list as a list of dictionaries.

Finally, the code creates an instance of the BabyAGI class using an OpenAI language model (LLM), a vector store, a verbose flag, and a maximum iteration count (if defined). It then calls the BabyAGI instance with an objective dictionary containing the agent’s objective to kick off the agent’s functioning. The agent will continue running indefinitely until the iteration count is reached if defined.

Overall, this code sets up a framework for creating an agent that can execute tasks based on priorities and reprioritize them based on results using various chains and other tools. The resulting code is flexible and can be adapted to suit specific objectives and requirements.


In this article, we have introduced BabyAGI, and demonstrated how it can be enhanced with LangChain to make a more capable and reliable AGI. We have described how we have implemented the BabyAGI model with a tool-based approach, using a vector store and LLM chains to create an agent that can execute tasks based on priorities and reprioritise them based on results. We have also shown how we can add new tools to the agent, such as a tool for searching the internet for information.

Our implementation of BabyAGI with LangChain and tools shows promise for developing a more capable and robust AGI. It provides a flexible and adaptable framework that can be used to solve a wide range of problems. Further development of this approach could lead to significant advancements in the field of AGI, enabling machines to perform complex tasks and achieve new levels of intelligence. We hope that our work will inspire others to explore this exciting area of research and development.

See the output here: How a Task-Driven Autonomous Agent Utilises GPT-4 and LangChain for Diverse Applications





Leave a Reply

Your email address will not be published. Required fields are marked *