Hugging Face

Overview

Patronus integrates the Hugging Face LLM model to provide users with advanced predictions on flight prices. The selected model analyzes historical and current flight data to identify optimal booking windows for the lowest prices. This functionality is central to Patronus, as it empowers users to book flights at the most economical rates.

Model Selection and Objective

In Patronus, we are using the Hugging Face LLM model Mistral-7B-Instruct-v0.2. This model is fine-tuned to handle various data-driven prediction tasks and was selected for its ability to process structured inputs, generate text-based responses, and produce accurate predictive outputs. Its role in Patronus is to:

  • Analyze flight data to predict the lowest price for a given route.

  • Recommend booking timings that align with price drops, giving users actionable insights.

Integration with Appwrite

The model is accessed via Appwrite functions, which handle both the data preparation and the API requests to Hugging Face. This setup allows the model to be invoked securely within Appwrite’s cloud environment, enabling seamless AI interaction.

1. Data Handling with Appwrite

The flight query data collected from users is stored in the Appwrite database and includes the following:

  • fromLocation and toLocation: Departure and destination details.

  • departureDate and arrivalDate: Planned dates for the flight.

  • number of passengers: Total number of people traveling.

Upon user request, this data is retrieved from Appwrite and formatted for compatibility with the Hugging Face API.

2. API Integration with Hugging Face LLM

The Appwrite function initiates a request to the Hugging Face model API, sending the structured flight details as input. Here’s a breakdown of the request and response process:

  1. Prepare the Request Data:

    • The flight details (from Appwrite) are formatted and serialized to meet the input requirements for the Hugging Face model.

    const hf = new HfInference(process.env.HUGGING_FACE_API_KEY);
        const prompt = `Given the following details:
            {
                "fromLocation": "${document.fromLocation}",
                "toLocation": "${document.toLocation}",
                "departureDate": "${document.departureDate}",
                "arrivalDate": "${document.arrivalDate}",
                "passengers": "${document.passengers}"
            }
        Return a valid JSON object with fields: fromLocation, toLocation, departureDate, arrivalDate, takeOffTime, landingTime, passengers, ticketPrice, airline, bookingLink. Do not include any explanation or additional text.`;
  2. Invoke the Model:

    • The request is sent to the Hugging Face LLM API using the Hugging Face Inference API, authenticated with a secure API key stored in environment variables.

    const response = await hf.textGeneration({
                model: 'mistralai/Mistral-7B-Instruct-v0.2',
                inputs: prompt,
                parameters: { max_new_tokens: 200 }
            });
    
            if (!response || !response.generated_text) {
                log('AI Response is empty or invalid');
                return res.json({ 
                    ok: false, 
                    error: 'AI response was empty or invalid' 
                }, 500);
    }
  3. Parse and Handle the Response:

    • Once the API responds, the output includes:

      • Predicted Lowest Price and Optimal Booking Date/Time.

      • Booking Link from the provider, where available.

    The Appwrite function then parses this data and prepares it for email delivery to the user.

  4. Error Handling and Logging:

    • Error responses are logged, and the function includes mechanisms for retrying requests if the Hugging Face API fails.

Why Hugging Face for Patronus?

  • Accuracy in Price Predictions: Hugging Face models are pre-trained on large datasets, making them highly reliable for predictions based on historical and structured data.

  • Scalability: The API-based integration ensures Patronus can scale its AI capabilities alongside growing user demand.

  • Efficiency and Automation: By automating the prediction and notification process, the Hugging Face model saves users time and ensures they’re informed promptly.

Use Case in Patronus

  1. Flight Price Prediction: Users input their flight details, which are sent to the model via Appwrite. The model predicts the lowest price and ideal booking date.

  2. Automated Notifications: The response is integrated into an email notification (via Mailgun), sent directly to the user’s mailbox for convenience.


This Hugging Face LLM integration brings sophisticated data analysis and predictive capabilities to Patronus, enhancing its value for users looking to save on travel. Let me know if you'd like any further information on the AI model setup!

Last updated