Building an AutoGPT assistant in Threads using LangChain and Replit
This past week at Threads was our first AI hackathon. We prototyped an AutoGPT AI assistant that can use Threads to collaborate with humans.
Our goal was to create infrastructure that allows us to experiment with AI agents quickly within Threads. We used LangChain and Replit to create this rapid prototyping environment.
In this tutorial, I'm gonna walk you through how we built it. If you're in a hurry, find the final code here.
Some background on Agents
An agent is an LLM-backed program that has access to tools, and that can plan to use them in order to achieve a goal. You can read more about them here.
LangChain is an open-source library that makes it really easy to chain together calls to LLMs in order to create complex programs, including agents.
Agents become really powerful when you give them long term memory. Early experiments have shown astonishing results, and open-source implementations like AutoGPT and BabyAGI have gone viral.
First, the demo
In this demo, we have an agent running in Replit that we give the following prompt:
I am working on a project with Rami and Chris. Please find out the current status of our research project, and write a thread status report in channel_id=34470847918
The agent will message Rami and Chris to get a status update from each, then publish a thread that synthesizes the updates it gathered.
Before we get started
The first step is to get a Threads API Key:
- If you haven't already, sign up and create an org
- Go to https://threads.com/integrations, and under Developer API, click "Generate API Key"
The easiest way to follow along with this demo is to use Replit:
- Fork the demo, or create a new repl
- Create a new secret called
THREADS_API_KEY
, and paste the Threads API Key you just created
Getting started: The ThreadsToolkit
Our goal is to create an agent that knows how to use Threads. We have an early public API, and the first step is to teach the agent how to use this API.
LangChain has a great abstraction for this: Toolkits. A Toolkit is a collection of Tools, where each tool is a function that the LLM can invoke, along with a description of when to use this tool.
For example, in order to search the web, you can give the agent a Search Tool that can call a search API with the following description:
Useful for when you need to answer questions about current events. Input should be a search query.
In our case, each Threads API endpoint is a different tool that the LLM can use, and inspired by the Jira Toolkit, I wrote a ThreadsToolkit
.
First we start with ThreadsAPI
, a wrapper around 3 sample API endpoints: postThread
, postChatMessage
, and postComment
.
To explain what each endpoint does to the LLM, we write a brief description that will be injected into the agent prompt:
We then define a generic ThreadsTool
class, which encapsulates an API endpoint, and a description like one we wrote above.
Finally we define ThreadsToolkit
, which creates a ThreadTool
for each endpoint:
Humans as Tools
Threads is a collaborative environment, and the agent should be able to communicate with others in the org in order to collaborate with them. To do that, inspired by the LangChain HumanInputRun tool, we created a Tool representing each person in the org that the agent can reach out to for help. The tool will message that user, and wait for their response.
Putting it all together
We pass in the tools from the ThreadsToolkit
and HumanToolkit
, along with a memory vector store, and we're ready to run our agent!
The agent will be able to understand any task that involves members of the org, and be able to use the Threads API to send and receive messages, and post threads.
And that's it!
This demo is nowhere near production-ready, but we were able to get to a reasonable prototype in less than an hour, and in a few hundred lines of code. Not bad!
Here's the final Repl: