Harnessing the Power of AI for Self-Improvement: How to Build Your Own AutoGPT style Bot

Sriram Parthasarathy
10 min readApr 12, 2023

A Step-by-Step Guide to Creating an simple AutoGPT style Bot

As conversational AI gains traction, an increasing number of enterprises are looking to adopt chatbots to engage with customers, clients, and staff. AutoGPT-style bots, driven by artificial intelligence, facilitate natural language exchanges with users and execute tasks on their behalf. Their growing popularity stems from their ability to streamline operations by automating tasks that would typically require human input.

In this article, we will demonstrate how to construct a straightforward AutoGPT-style bot capable of interactively proposing specific tasks to accomplish a particular objective. By considering the user’s intent on a subject and the question they wish to address, our bot will employ the ChatGPT API to generate tasks and obtain detailed information about each task. Moreover, it can delve deeper into each task to provide additional insights using the ChatGPT API.

The advantage of this approach is, the same bot can be used to answer different kinds of question in different domain!

Using ChatGPT API for creating tasks

Chat GPT API is an AI-powered REST API that uses natural language processing and machine learning to generate human-like responses to user queries. Custom applications can be built with ChatGPT API by using the API’s natural language processing and machine learning capabilities to generate responses to user queries. Developers can integrate the ChatGPT API into their applications by using the API’s SDKs and RESTful API, which provide a variety of functionalities such as text generation, summarization, translation, and more.

To utilize the ChatGPT API, create an account on the ChatGPT website and obtain an API key. With the key in hand, you can start making requests to the API endpoints.

To engage with the ChatGPT API, send a text prompt and await its response. Prompts can range from simple questions to complex sentences, and the API will produce relevant and meaningful responses.

The ChatGPT API’s output is a textual response that can be utilized to generate conversational interfaces. This response can be employed to solicit further input from the user, offer pertinent information or suggestions, or execute other actions based on user input.

Read more about how to use ChatGPT API to ask contextual questions here. That would be a good read about the API as in this article I won’t go through the basics.

AutoGPT prompt Design and Use cases addressed

The self-help chatbot employs a goal-driven strategy to support users across various fields, steering them towards accomplishing their specific aims. Initially, the bot requests users to pinpoint their area of interest and explicitly state their goal. For example, the user could focus on social media with the intention of expanding their Twitter following, or fitness, aiming to lose 10 pounds within a month.

User expecting a detailed task list of what he has to do to achive his goals

There is a wide array of categories and goals that users may wish to pursue. Some examples include:

  • Fitness: Completing a 10K race in under an hour
  • Financial: Saving $10,000 for a house down payment
  • Social Media: Gaining 10,000 Instagram followers within six months
  • Learning: Achieving fluency in a new language within a year
  • Cooking: Mastering five gourmet recipes and hosting a dinner party for friends and family

Once the chatbot acquires the domain and objective, it produces a customized list of tasks catering to each unique goal. This versatile bot can also probe deeper into each task, supplying further information when required.

To preserve simplicity and reduce API expenses, the chatbot’s in-depth functionality is limited to a single drill down. The same approach can be extended for additional drill downs. Users are inquired if they want more details on each primary task, and the bot only delves deeper if the user confirms their interest. In subsequent sections, we will examine the mechanics of this chatbot and the method it utilizes to guide users towards realizing their goals in an engaging and user-friendly way.

How the bot was built

The application initiates by inviting the user to input a subject and a related question or objective. Based on the user’s response, the app employs the ChatGPT API to create a custom prompt and uses ChatGPT API to create a list of the top 5 tasks associated with the question or goal.

Subsequently, the app inquires if the user wishes to acquire more information about each of the 5 tasks identified. If the user agrees, the app consults the ChatGPT API once more to obtain in-depth details for the chosen task. This procedure can be repeated for multiple levels to construct an interactive AutoGPT-style bot. However, to maintain simplicity, this app delves only one level deep. The same approach can be used to extend to multiple levels.

The gathered information can be saved to a file, a database, or displayed on-screen, enabling the user to monitor their progress and objectives.

The next 5 sections will discuss how the bot was build.

  1. Initial setup to connect to ChatGPT
  2. Function call to connect ChatGPT with past context
  3. Prompt design to query tasks to accomplish the goals
  4. Send prompt and receive the task list from the API
  5. Asking the user if he likes details of any of the task and retrieve additional details and save it to a file

1. Initial setup

Lets import the OpenAI library and set the API Key you got from the OpenAI web site. We will be using the GPU 3.5 model.

import openai
# Get the key from the OpenAI web site
openai.api_key = " ..... "
model_id = "gpt-3.5-turbo"
openai.api_key = API_KEY

2. Function to call ChatGPT AI

Next Step is to write a simple function that will call ChatGPT API and get your questions answered. More details of how to do this in steps is explained in this article.

To facilitate a conversation with ChatGPT, it’s necessary to provide it with the context of past exchanges in addition to the current question. ChatGPT doesn’t retain previous conversation details by default, and thus, sending this information is crucial for ChatGPT to accurately recall and respond to the ongoing dialogue.

This function takes two parameters.

The new question you would like to ask and the past conversation. The new question and the past conversation details are packaged and sent to ChatGPT and we get a response. This response is returned by this function.

# We call this function and pass the new question and the last messages
# NewQuestion is the brand new question we want to answer
# lastmessage is the past conversatio that is passed along for context to have a conversation
# API does not rememeber the past conversation so we have to do this so it can use that context
def GetMessageMemory(NewQuestion,lastmessage):
# Append the new question to the last message
lastmessage.append({"role": "user", "content": NewQuestion})
# Make a call to ChatGPT API
msgcompletion = openai.ChatCompletion.create(
model=model_id,
messages=lastmessage
)
# Get the response from ChatGPT API
msgresponse = msgcompletion.choices[0].message.content
# You can print it if you like.
#print(msgresponse)
# We return the new answer back to the calling function
return msgresponse

We will be using this function to communicate with ChatGPT.

3. Prompt design to query tasks to accomplish the goals

The goal here is to setup a prompt that can be sent to ChatGPT which includes the topic we are querying and the actual question.The topic and the question are user input. Those two answers are used to create a new prompt that is actually sent to ChatGPT to create the task in the next step.

# Get Domain and the actual question to ask from the user
topic1 = input("Specify the domain or area or the topic you want ask the AI? Example : Social Media or Healthcare")
q1 = input("Describe the task or goal or question or problem? Example : Grow Social Media or Loose 10 pounds in a week")


# Create the prompt string to use from the above input
initialprompt = "Hey ChatGPT, can you create a task list for me related to " + topic1 + "? I need to " + q1 + ". Please provide me with the top 5 prioritized tasks to achieve this goal, starting with the most important. Thank you! Give the data in the format Task #1: Task #2:"

4. Send prompt and receive the task list from the API

In the previous step, we crafted a prompt to generate the top 5 tasks for achieving the goals using ChatGPT APIs. The GetMessageMemory function is employed to communicate with the ChatGPT API. More details of how to do this in steps is explained in this article.

messages = []
# Replace the question below with the question you'd like to ask about the clinical document
# Set the question to answer
cresponse = GetMessageMemory(initialprompt, messages)
messages.append({"role": "assistant", "content": cresponse})

API response times can vary, ranging from a few seconds to almost a minute, depending on the time of day the API is called.

Once the API response is obtained, there are several ways to store it. It can be displayed on-screen, saved to a file, or stored in a vector database like Pinecone. To simplify this example and reduce complexity, the response is saved locally into a file with the same name as the asked question.

# User the question as the file name to save the responses
file_name = q1

# Convert file name to a valid file name
file_name = "".join(x for x in file_name if x.isalnum() or x in [" ", "-", "_"]).rstrip()

# Add file extension
file_name += ".txt"

# Open the file to append
file = open(file_name, 'a')

# Add the response we got from ChatGPT API
file.write(cresponse + '-------------------------------\n \n ')

5. Requesting additional details for the task

At this stage, the first part of the self-help bot is complete, providing a task list based on the user’s query. The next step involves responding to the user for additional details on each task.

Instead of automatically querying the ChatGPT API for further details on each task, the user’s input is considered. The user is asked if they would like more information about a specific task. For example, if there are 5 steps and the user is only interested in details for steps 3 and 4, they can decline additional information for other steps and affirm their interest in steps 3 and 4.

# Loop 5 times for 5 tasks
for i in range(5):
# Ask a yes or no question and get the user's response
response = input("Do you want to get additional details for response #" + str(i+1) + " y/n?")

# Check if the response is "y"
if response == "y" or response == "yes":
# If the response is "yes", print "yes"
file.write('-------------------------------\n \n ADDITIONAL DETAILS FOR TASK #: ' + str(i+1)+ ' \n')
cresponse = GetMessageMemory("Expand #" + str(i+1), messages)
print("Answer : " + cresponse)
file.write(cresponse + '\n \n -------------------------------\n \n ')


# Close the file
file.close()

This is an example output (top 5 steps ) provided by the AutoGPT style bot for the topic: Fitness and for the goal “Loose 10 pounds by doing yoga”.

Establishing a consistent yoga routine is the most important task in achieving your goal of losing 10 pounds by doing yoga. This entails a deliberate and mindful approach to your yoga practice, with a focus on regularity, discipline, and structure. To establish a consistent routine, follow these tips:

1. Create a schedule: Plan a regular weekly schedule that allows enough time for yoga practice, taking into account your personal commitments, work schedule, and other activities. Ensure the schedule is realistic and achievable.

2. Find a suitable environment: Choose a space that is free from distractions, noise, and other disturbances. Consider using items such as yoga blocks, straps, and mats to help you focus and remain comfortable during your practice.

3. Determine your practice style: Select a yoga style that aligns with your fitness goals, interests, and current skill level. You can choose from several styles such as Hatha, Vinyasa, and Ashtanga yoga.

4. Take guidance from professionals: Consider taking classes or hiring a certified yoga instructor who can guide you through your practice, identify areas for improvement, and ensure that you are doing the poses correctly.

5. Modify your practice when necessary: Understand that you might face physical or mental challenges that affect your yoga routine. It's important to modify your practice when necessary to avoid burnout, injuries, or loss of motivation.

By establishing a regular yoga practice, you will build strength, flexibility, and endurance, all of which contribute to weight loss. More importantly, a consistent yoga routine will create a sense of discipline and structure that can help you stay motivated and on track towards achieving your weight loss goals.

-------------------------------

Conclusion

In conclusion, the implementation of an AutoGPT-style self-help chatbot offers users an interactive and engaging way to receive tailored guidance for achieving their goals. By integrating the ChatGPT API, this chatbot generates customized task lists and provides additional information upon user request. Although the example presented in this article maintains simplicity by limiting the depth of details to one level, it demonstrates the potential of such a bot in various domains, such as fitness or social media management. As conversational AI continues to advance, chatbots like this will play an increasingly vital role in assisting users in a user-friendly and efficient manner, ultimately helping them achieve their objectives more effectively.

BECOME a WRITER at MLearning.ai

--

--