How I Made a Bot to Plan My Trips Using Julep and Chainlit
Published: 2024-05-06 14:53:42

As part of my generative AI series, this post will walk you through how I created an AI-powered travel agent using Julep and Chainlit. This bot can help you plan your next trip by suggesting destinations and activities and even generating an entire travel itinerary.

As a travel enthusiast, I've always wanted a personalized travel assistant that could help me discover new places and create custom itineraries. With the power of generative AI, I was able to bring this idea to life in just a few steps.

## What You're Gonna Use/Tech Stack

To build this trip planning bot, I used:

- Julep: An open-source framework for creating AI agents
- Chainlit: A tool for easily adding chat interfaces to your AI projects

## But, What's Julep?

Julep is an incredibly helpful framework that provides all the tools you need to create AI agents, manage user sessions, and store conversation memory. It allowed me to focus on designing my travel agent's capabilities without getting bogged down in the infrastructure.

Don't forget to ⭐ the [Julep repo](https://github.com/julep-ai/infuse/) to show your support!

## Step 1: Set Up Julep and Chainlit

First, install Julep and Chainlit in your Python environment:

```bash
pip install chainlit
pip install julep
```

Then, initialize the Julep client with your API key:

```python
from julep import AsyncClient

api_key = "<YOUR_API_KEY>"

client = AsyncClient(api_key=api_key)

```

## Step 2: Create the Travel Agent

Next, use the Julep API to create your AI travel agent. Give it a name, description, and a set of instructions that define its capabilities:

```python
    agent = await client.agents.create(
        name="Travel Agent",
        about="You are a travel agent with several your of experience. You are knowledgeable about different travel destinations and can provide recommendations based on user preferences. You are also able to suggest flights, hotels, and other travel accommodations for users. You are friendly, helpful, and eager to assist users with their travel needs.",
        model="gpt-4-turbo",
        instructions=[
            "Ask the user where they would like to travel to.",
            'Ask the user what their budget is',
            'Ask the user what vibe they are looking for in a trip',
            'Ask the user what kind of activities they enjoy',
            "Depending on the user's responses, suggest travel destinations as a numbered list",
            "Suggest activities, nearby attractions, landmarks, and restaurants in the destination as a numbered list",
            "Ask if they are planning to visit multiple destinations",
            "Ask for how many days the user is planning to stay",
            "Prepare an itinerary for the user",
            "Give useful phrases in the local language",
    ])
```

## Step 3: Set Up User & Session

Create a user and a session to facilitate the conversation between the user and the travel agent:

```python
user = await client.users.create(name="Philip", about="Traveler")
session = await client.sessions.create(agent_id=agent.id, user_id=user.id, situation="You are greeting a user that's planning to go on a trip.")

```

## Step 4: Set Up Chainlit Interface

Then, use Chainlit's decorators to define the chat interface:

```python
@cl.on_chat_start
async def start():
    session_id = await setup_session()
    cl.user_session.set("session_id", session_id)

    response = await client.sessions.chat(session_id=session_id, messages=[{"content": "Greet the user", "role": "system"}], recall=True, remember=True, max_tokens=1000)
    await cl.Message(content=response.response[0][0].content).send()

@cl.on_message
async def main(message: cl.Message):
    session_id = cl.user_session.get("session_id")

    response = await client.sessions.chat(session_id=session_id, messages=[{"content": message.content, "role": "user"}], recall=True, remember=True, max_tokens=1000)
    await cl.Message(content=response.response[0][0].content).send()

```

## Getting it to Plan

And that's it! Run your Chainlit app and start chatting with your new AI travel agent. It will ask about your preferences, suggest destinations and activities, and generate a personalized itinerary for your dream trip.


![Success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yvk0jwtb70qfo8xgofs.jpg)

## Use It to Plan Your Own Trip!

Want to try it out yourself? The full code for this project is available on my GitHub repo: [https://github.com/philipbalbas/travel-agent](https://github.com/philipbalbas/travel-agent)

Feel free to clone it, customize the agent's capabilities, and start planning your next adventure!

And once again, don't forget to ⭐ [Julep](https://github.com/julep-ai/julep/) for making projects like this possible.


![star!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2c5uqovxo6rnjpioy5c4.gif)

Happy travels! 🌎✈️