DEV Community

Cover image for Building a Chatbot For Travel Booking Assistant with OpenAI Integration
Jacky
Jacky

Posted on

Building a Chatbot For Travel Booking Assistant with OpenAI Integration

Building a Retrieval-Augmented Generation (RAG) system for a chat application that recommends flights and hotels involves integrating information retrieval (from your database) with natural language processing (NLP) for user interaction. In this context, a RAG system uses retrieval-based methods to fetch relevant information (like flights and hotels) based on a user's query, then generates responses by combining that data with a language model (e.g., OpenAI GPT).

Here’s how to build such a system:

1. Overview of Architecture

The RAG system will have two key parts:

  • 1. Retrieval Component: This fetches relevant flight and hotel data from your database using a query processor. This could involve SQL or a more complex search engine setup (e.g., Elasticsearch for faster querying).
  • 2. Generation Component: After retrieving the relevant data, a language model generates a human-like response using that data to reply to the user in the chat.

2. System Workflow

Here’s the step-by-step process of how the system will work:

  • 1. User Query: The user types a natural language query in the chat, like “I want to fly to Singapore from Hanoi, Vietnam.”
  • 2. NLP Parsing: Use an NLP model to extract key entities from the user query (departure city, destination city, dates).
  • 3. Database Search: Based on the parsed query, run a SQL query or Elasticsearch search to retrieve relevant flight and hotel options.
  • 4. Response Generation: Once you have the flight or hotel data, pass it to the language model (like GPT-3) to generate a conversational response that lists options for the user.
  • 5. User Interaction: The user selects a flight, and based on that choice, you run a new search for hotels. The system retrieves the hotels and generates another response with the best options.
  • 6. Book or Recommend: Once the user chooses a hotel or flight, you can proceed to the booking flow (through the chat interface).

3. Components of the RAG System

3.1 Retrieval Component: Query Matching

Example Use Case: Parsing User Query for Flights

When a user types a message like: "I want to fly to Singapore from Hanoi next week",you want to extract key pieces of information like:

  • Departure city: Hanoi
  • Destination city: Singapore
  • Date or time: next week

3.2. Using OpenAI to Parse Keywords

You can create a prompt that instructs OpenAI to extract the desired information. Here’s an example approach:

Prompt Design for Flight Information Extraction

import openai

# Function to extract key details using OpenAI
def extract_flight_info(user_query):
    prompt = f"""
    The user has provided the following query: "{user_query}"

    Extract the following information from the user's request:
    1. Departure city
    2. Destination city
    3. Travel date or time (if mentioned)

    Provide the results in the following format:
    - Departure City: [Extracted departure city]
    - Destination City: [Extracted destination city]
    - Date/Time: [Extracted date or time]
    If any information is missing, return "Unknown" for that field.
    """

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content": prompt}]
    )
    return response['choices'][0]['message']['content']

# Example query
user_query = "I want to fly to Singapore from Hanoi next week"
extracted_info = extract_flight_info(user_query)
print(extracted_info)

Enter fullscreen mode Exit fullscreen mode

Expected Output

When you run the above code with the user query "I want to fly to Singapore from Hanoi next week", the output might look something like this:

- Departure City: Hanoi
- Destination City: Singapore
- Date/Time: next week
Enter fullscreen mode Exit fullscreen mode

This way, OpenAI can help break down natural language input into structured components that your application can then use to perform further actions, like database searches.

3.3. Further Enhancements for Parsing

To handle more complex scenarios (e.g., when the user provides partial information), you can adjust the prompt to better fit various cases:

Handling Flexible Queries

You might need to parse different types of queries, such as:

  • "Find me a hotel in Singapore this weekend"
  • "What flights are available from New York to London tomorrow?"

Here’s an example prompt that adapts to both flights and hotels:


def extract_travel_info(user_query):
    prompt = f"""
    The user has provided the following query: "{user_query}"

    Extract the following information from the user's request:
    1. Type of request (flight or hotel)
    2. Departure city (for flights) or city (for hotels)
    3. Destination city (for flights) or "N/A" (for hotels)
    4. Travel date or time (if mentioned)

    Provide the results in the following format:
    - Request Type: [flight or hotel]
    - Departure City: [Extracted departure city]
    - Destination City: [Extracted destination city] or "N/A"
    - Date/Time: [Extracted date or time]
    If any information is missing, return "Unknown" for that field.
    """

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content": prompt}]
    )
    return response['choices'][0]['message']['content']

# Example for a hotel request
user_query = "Find me a hotel in Singapore this weekend"
extracted_info = extract_travel_info(user_query)
print(extracted_info)

# Example for a flight request
user_query = "I want to fly from New York to London tomorrow"
extracted_info = extract_travel_info(user_query)
print(extracted_info)

Enter fullscreen mode Exit fullscreen mode

Expected Output for Hotel Request

For the query "Find me a hotel in Singapore this weekend", the output might be:


- Request Type: hotel
- Departure City: N/A
- Destination City: Singapore
- Date/Time: this weekend

Enter fullscreen mode Exit fullscreen mode

Expected Output for Flight Request

For the query "I want to fly from New York to London tomorrow", the output might be:


- Request Type: flight
- Departure City: New York
- Destination City: London
- Date/Time: tomorrow
Enter fullscreen mode Exit fullscreen mode

3.4. Next Steps: Using Extracted Data for Database Query

Once you have the extracted information (like departure and destination cities, dates, and request type), you can pass these into your database query to retrieve relevant flights or hotels.

For example, if the extracted data shows the request is for a flight:

  • Departure City: New York
  • Destination City: London
  • Date: tomorrow

You can construct a SQL or Elasticsearch query to search your flights table for relevant results.


SELECT * FROM flights
WHERE departure_city = 'New York'
AND arrival_city = 'London'
AND departure_date = '2024-10-22';
Enter fullscreen mode Exit fullscreen mode

3.5. Handling Ambiguities and Errors

In some cases, the user's query may be ambiguous or lack sufficient detail. You can modify the prompt to ask follow-up questions or handle missing information:

  • If the user doesn’t specify a date, the model can prompt: "Could you please provide the date of your travel?"
  • If the departure or destination city is unclear, it can ask: "Where would you like to fly from?"

This approach ensures a smooth interaction and helps fill in any gaps in the query.

Generation Component: Response Building

After retrieving the relevant data, integrate a language model (like GPT) to generate responses using the retrieved data.

  • Generate Chat Responses:

    • You can use OpenAI's GPT model to craft natural-sounding responses by passing the retrieved data (e.g., flight options or hotel recommendations) into the prompt. For example:
    
    prompt = f"""
    The user wants to fly from {from_city} to {to_city}. Here are some flight options:
    
    1. {flight1_details}
    2. {flight2_details}
    
    Which one would you like to choose? Once selected, we will suggest some hotels in {to_city}.
    """
    
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content": prompt}]
    )
    

    This will generate a conversational reply using the flight or hotel data you pass in.

4. Additional Features

  • Personalization: Tailor recommendations based on user history (preferred airlines, hotel types, etc.).
  • Filter Options: Allow users to filter results (price, ratings, amenities).
  • Feedback Loop: Collect user feedback to improve search relevance over time.

5. Scaling and Production Considerations

  • Real-time Processing: Ensure low-latency in retrieving data and generating responses, possibly using a message broker like RabbitMQ.
  • Caching: Use caching for frequently requested flights and hotels to optimize performance.
  • Error Handling: Gracefully handle cases where no results match the user's query by suggesting alternatives

By integrating retrieval from your flight and hotel databases with a language model for generation, this RAG system will offer users a seamless, conversational experience for booking travel options directly through a chat interface.

Top comments (0)