- 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:Setup
Now, let’s create a python file calledagent.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:
agent.py
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
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.
agent.py
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 theInstagramTools
class, let’s create a goto_chat
method.
agent.py
InstagramTools
we’re creating send_message_in_chat
and get_messages_from_chat
, utilizing out goto_chat
method for navigation.
agent.py
agent.py
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 theupload_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.
agent.py
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.agent.py
upload_post
tool.
agent.py
agent.py
agent.py
main
function
agent.py
Final Result
Et voilà! A fully functioning Instagram agent built with LangChain and Dendrite.agent.py