Build and Deploy a Chat Agent
Overview
The MeshAgent Chatbot lets you add a conversational (chat/text based) agent to any MeshAgent Room with just a few lines of code!
In this quickstart we’ll build the agent in three phases:
- Basic Chatbot: A simple agent with a system prompt (rules)
- Chatbot with Built-in MeshAgent Tools: Extend our chat agent by adding MeshAgent’s prebuilt tools to interact with the user and write documents to the room.
- ChatBot with Built-in MeshAgent Tools and Custom Tools: Add our own tools to the agent for use-case specific tasks.
You’ll learn how to:
- Build a chat based agent with MeshAgent
- Connect the agent to a MeshAgent Room for live testing.
- Deploy the agent as a MeshAgent Service.
- Generate a shareable link so others can start chatting right away.
Getting Started
Install MeshAgent
Note: uv is the faster drop-in replacement for pip; feel free to use pip install meshagent[all] if you don’t have uv.
Connect to MeshAgent Project
Note: You can create a MeshAgent API Key through the MeshAgent Studio UI or by running meshagent api-key create and passing in the key name and description
Chat with the built-in ChatBot (zero code)
Let’s bring a ready-made chatbot into a room and talk to it:
Running this command will:
- Create and open a room
- Call the chatbot into the room
Now you can type a question to the chatbot and watch it reply.
Phase 1: Building a Simple Chatbot
Now that we’ve seen the default chatbot in action, let’s create one from scratch.
Create a main.py file and paste the starter code for the basic ChatBot. To start, we can give the agent one rule / system instruction. We’ll build up this code over the course of this example.
Running the Simple Chatbot in Test Mode:
Time to try it out!
To run the agent you will need two tabs open in your terminal. Be sure you have saved / exported your environment variables to your environment, installed the MeshAgent libraries, and authenticated to MeshAgent (see above steps).
In the first tab run:
In the second tab run the call agent command to bring our agent into the room:
As an example:
Now we can navigate to the MeshAgent Studio and we’ll see our agent, “mychatagent” show up under the messaging tab! We can chat back and forth with the agent, asking it questions and making sure that it responds with a fun fact like we told it to.
Phase 2: Adding Built-in MeshAgent Tools to our Chatbot
Next, let’s give our agent the ability to interact with the user, convert documents to markdown, and write documents to the room. We can do this by adding toolkits to our agent. Toolkits represent a group of tools that are used for a particular purpose.
We’ll also add a few imports and update the chat agent’s rules so it knows how to interact with the available tools more efficiently.
We can test the agent using the same commands as we did with the simple chat agent!
Phase 3: Adding Custom Tools to our Chatbot
Now let’s add a custom tool to our Chatbot! We’ll create a TaskTracker toolkit that allows the agent to write and read tasks to the Room database.
To do this we will update the chat agent initialization to create a table for the tasks when the room starts up. We will create a tasks toolkit with two custom tools to WriteTask and GetTasks from the database.
This is a simple example of adding tasks, to create a more useful task writer we’d want to add date information and other metadata to better track and filter tasks. This example is mainly to demonstrate writing to room storage and adding custom tools to our chat agent.
We can test the agent using the same commands as we did with the simple chat agent! And now when we go to the room we can ask the agent to add a task to our task database.
Once we’re satisfied with how the agent is performing we can deploy and share it.
Note: Building an agent will likely take multiple rounds of iterating through writing different versions of the system prompt and crafting the best tools for the agent.
Deploying and Running the Agent as a MeshAgent Service
Prerequisites:
To deploy the agent you will need to have docker setup and a container registry with your cloud provider (e.g. GCP, Azure, AWS).
For this example we are using Azure Container Registry. These steps assume you have created a container registry in Azure, have a service principal setup, and have the appropriate permissions to access and push images to the container registry.
- For more information on setting up an Azure Container Registry see the documentation here: https://learn.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal?tabs=azure-cli
- For more information on creating a service principal in azure see the documentation here: https://learn.microsoft.com/en-us/azure/container-registry/container-registry-auth-kubernetes.
You will need to create a new image pull secret so that MeshAgent can pull and run your container, the image pull secret requires the service principal ID and password. To save the image pull secret navigate to the MeshAgent Studio, click the Secrets tab, then click New Image Pull Secret, give a name to your secret and fill in the required information.
Once you have the registry setup proceed with the following steps.
Step 1: Build and Push Your Container
- Create and activate a dedicated Buildx builder We recommend using zstd images to speed up image pulls. To enable building zstd images, run the following commands:
- Log in to Azure, connect to your ACR instance, then build and push the docker container. To build this container, use docker buildx to make a linux/amd64 image and push it to your registry.
Step 2: Create a Service in MeshAgent for each of your Agents
- Navigate to the Services tab and click the button to create a New Service and fill in the required information about the agent.
- Add the MESHAGENT_PORT as an environment variable, this needs to match the port that you register the service with.
Step 3: Try your agent!
Navigate to the Sessions tab in the MeshAgent Studio and either join an existing room or create a new room by starting a session. You will then see the Chatbot Agent available in the room for you to interact with!
Generate a Link and Share Your Agent
- Navigate to your Room, click the hamburger menu icon in the top left corner, next click “Share”
- Select the Room and Agent you’d like to share and click “Generate Link”
- Proudly share your Agent!