LangChain Agent
A LangChain Agent that can handle your Instagram account
In this example we’re going to create an agent using LangChain and give it web tools made with Dendrite. The web tools will allow the agent to interact with Instagram through the web app, allowing unlimited customizability based on your agent’s needs.
The order we’re going to do this is:
- Installing dependencies
- Setup and imports
- Creating a class for Instagram tools using
Dendrite
- Defining our langchain tools for the agent using our
InstagramTools
class - Creating our agent using LangChain
- Running the agent in a chat loop between Human and Agent in the console
Installing Dependencies
We’re going to start with installing all necessary packages. First, let’s install Dendrite using Poetry:
Now let’s install the langchain packages we need:
And finally, the remaining packages:
Setup
Now, let’s create a python file called agent.py
. We’re going to start by importing all dependencies at the top. Don’t worry about their uses, it will become clear later on:
Instagram Tooling
Next, we’re going to start building our Instagram tools. We are going to create 3 tools for Instagram:
- Reading DMs
- Sending a DM
- Posting content
For these, we’re going to create a class called InstagramTools
. The class will manage the Dendrite browser and will contain the methods used for the tools metioned above. Let’s start with creating the class and initiating Dendrite.
In the code above, we initiate the Dendrite browser with an authenticated session on instagram.com. Read more about authentication here.
Reading and Sending DMs
For our DM related tools, we’re going to create a utility function that navigates to the specified DM conversation. Inside the InstagramTools
class, let’s create a goto_chat
method.
This method takes in a chat name, and navigates on the Instagram website just like a human would.
Now it’s time to build the methods to read and send messages. Inside InstagramTools
we’re creating send_message_in_chat
and get_messages_from_chat
, utilizing out goto_chat
method for navigation.
First we’re create the Pydantic models passed in to the extract
method for structured data output. Then, we utilize the fill
and press
methods to send messages, and the extract
method to extract messages.
Posting Content
Our last Instagram tool is for posting content. We will implement it using simple navigation and page interactions from the Dendrite SDK and using the upload_content
method. This tool takes in the caption and image path as an argument. Later, we will create a utility function that generates an image with Dall-E and saves it locally.
Creating our LangChain Agent
Now it’s time to pass our web tools to a LangChain agent. For this, we’re going to create a main function and initialize the InstagramTools class. We’ll also create a util function for generating a config object for out agent.
The next step is to define the tools for the agent using the @tool decorator from LangChain. We’re also creating a quick function for generating an image with Dall-E, which can be used to generate an image for our upload_post
tool.
Great, now it’s time to actually create the agent, configure it and pass our tools to it!
Now that we have our agent_executor, the last step is to run it in a loop where the human writes a message and the answer from the agent is streamed to the console.
Finally, simply call the main
function
Final Result
Et voilà! A fully functioning Instagram agent built with LangChain and Dendrite.