In the previous posts, we discussed the fundamentals of AI agents and how you can start developing your own with open-source tools. Now, let’s dive into hands-on coding and build a simple conversational AI agent using Rasa, a popular open-source framework for natural language processing and dialogue management. This guide includes example code, step-by-step instructions, and tips to get your first AI agent up and running.
1. Setting Up Your Environment
First, let’s make sure your development environment is ready. You’ll need Python and a few libraries, so follow these steps to set up:
- Install Python: If you don’t have Python installed, you can download it from python.org.
- Install Rasa: Use
pip
to install Rasa. Open your terminal and run:bashKopiera kodpip install rasa
Once installed, create a new directory for your project:
bashKopiera kodmkdir my_ai_agent
cd my_ai_agent
2. Initialize Your Rasa Project
Initialize your Rasa project by running the following command:
bashKopiera kodrasa init
This command will create essential files and folders, including a domain.yml
file for defining intents and responses, and a nlu.yml
file for training data. When prompted, select the option to train a sample model and interact with it to see how Rasa’s default bot works.
3. Understanding the Basic Project Structure
The core files in your Rasa project are:
nlu.yml
: Contains the training data for Natural Language Understanding (NLU), including example phrases for intents.domain.yml
: Defines your intents, entities, responses, and custom actions.stories.yml
: Describes example conversations to train the dialogue model.config.yml
: Specifies the NLU pipeline and policies for handling conversations.
4. Define Intents and Entities in nlu.yml
Let’s customize the bot to handle a simple task: responding to a greeting and providing information about the weather. Open data/nlu.yml
and replace the default intents with your custom ones:
yamlKopiera kodversion: "2.0"
nlu:
- intent: greet
examples: |
- hello
- hi
- hey
- good morning
- good evening
- intent: ask_weather
examples: |
- what's the weather like?
- how's the weather?
- is it going to rain today?
- do I need an umbrella?
5. Define Responses in domain.yml
In domain.yml
, define how the bot should respond to each intent. Update it with responses for greet
and ask_weather
:
yamlKopiera kodversion: "2.0"
intents:
- greet
- ask_weather
responses:
utter_greet:
- text: "Hello! How can I assist you today?"
utter_ask_weather:
- text: "I'm not connected to a live weather service, but I can tell you about the expected weather in general."
6. Create Stories in stories.yml
Stories are example conversations that help the AI agent learn how to respond based on user input. Open data/stories.yml
and add the following:
yamlKopiera kodversion: "2.0"
stories:
- story: greet user
steps:
- intent: greet
- action: utter_greet
- story: weather query
steps:
- intent: ask_weather
- action: utter_ask_weather
7. Configure the Pipeline in config.yml
The config.yml
file specifies which machine learning models and components Rasa will use. The default configuration is generally good for beginners, but here’s a basic setup:
yamlKopiera kodversion: "2.0"
pipeline:
- name: WhitespaceTokenizer
- name: RegexFeaturizer
- name: LexicalSyntacticFeaturizer
- name: CountVectorsFeaturizer
- name: DIETClassifier
epochs: 100
- name: EntitySynonymMapper
- name: ResponseSelector
policies:
- name: MemoizationPolicy
- name: TEDPolicy
max_history: 5
epochs: 100
- name: RulePolicy
8. Train Your AI Agent
Now that you’ve defined intents, responses, and stories, it’s time to train your model. Run the following command in your terminal:
bashKopiera kodrasa train
This will create a trained model file in the models
directory, which Rasa will use to understand and respond to user inputs.
9. Test Your AI Agent
To test your AI agent, start the Rasa shell:
bashKopiera kodrasa shell
Type in messages like “hello” or “what’s the weather like?” to interact with your bot and see how it responds. Adjust your nlu.yml
or stories.yml
if necessary to improve responses.
10. Add Custom Actions for Extended Functionality
For more advanced functionality, such as retrieving live data, you can define custom actions. Let’s set up a simple example where the bot gives a fixed response for the weather. First, add a custom action to domain.yml
:
yamlKopiera kodactions:
- action_weather
Next, create a new file called actions.py
in your project’s root directory and add the following code:
pythonKopiera kod# actions.py
from rasa_sdk import Action
from rasa_sdk.executor import CollectingDispatcher
class ActionWeather(Action):
def name(self):
return "action_weather"
def run(self, dispatcher, tracker, domain):
# This is a placeholder for actual weather data retrieval
weather = "It looks sunny today with a high of 22°C."
dispatcher.utter_message(text=weather)
return []
Update your endpoints.yml
file to enable action server:
yamlKopiera kodaction_endpoint:
url: "http://localhost:5055/webhook"
Start the action server with:
bashKopiera kodrasa run actions
And now, when you type “what’s the weather like?” the bot should respond with the message from ActionWeather
.
11. Deploy Your AI Agent
Once you’re happy with your AI agent, you can deploy it to a platform or cloud environment for broader access. Consider options like Docker for packaging or cloud providers like AWS or Google Cloud for hosting.
Summary
In this guide, you’ve learned how to set up a simple AI agent using Rasa, define intents and responses, create custom actions, and deploy your model. This hands-on project is just the beginning—now you can start expanding your agent to handle more complex interactions, connect it to live APIs, or even integrate it into messaging platforms like Slack or Facebook Messenger.
Building your own AI agents is a rewarding experience that allows you to learn more about machine learning, natural language processing, and practical AI application. With this guide and the foundational skills you’ve gained, you’re well on your way to developing more advanced, personalized AI agents.