### Install Backend Dependencies Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Installs all required Python packages listed in the requirements.txt file for the backend. ```bash pip install -r requirements.txt ``` -------------------------------- ### Start Frontend Development Server Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Launches the React development server. The application will be accessible at http://localhost:3000. ```bash npm start ``` -------------------------------- ### Install Frontend Dependencies Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Installs all required Node.js packages for the React frontend using npm. ```bash npm install ``` -------------------------------- ### Start Backend Server Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Launches the FastAPI backend application. The API will be accessible at http://localhost:8001. ```bash python main.py ``` -------------------------------- ### Configure Backend Environment Variables Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Copies the example environment file and instructs to fill in necessary API keys for Qdrant and OpenAI. Ensure these are kept confidential. ```bash cp .env.example .env # Edit .env and add QDRANT_URL, QDRANT_API_KEY, OPENAI_API_KEY ``` -------------------------------- ### Navigate to Backend Directory Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Change the current directory to the backend folder. This is the first step before installing backend dependencies. ```bash cd backend ``` -------------------------------- ### GET /api/health Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Verifies the backend server configuration, including connections to Qdrant and OpenAI. ```APIDOC ## GET /api/health ### Description Verifies that the backend server is properly configured with Qdrant and OpenAI connections. It returns the current configuration status and collection information. ### Method GET ### Endpoint /api/health ### Response #### Success Response (200) - **status** (string) - The health status of the service. - **qdrant_configured** (boolean) - Indicates if Qdrant is connected. - **openai_configured** (boolean) - Indicates if OpenAI is connected. - **hotels_loaded** (integer) - Number of hotels loaded in the collection. - **collection_name** (string) - The name of the Qdrant collection. #### Response Example { "status": "healthy", "qdrant_configured": true, "openai_configured": true, "hotels_loaded": 0, "collection_name": "hotels_structured_rag" } ``` -------------------------------- ### Navigate to Frontend Directory Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Change the current directory to the frontend folder. This is necessary before installing frontend dependencies. ```bash cd frontend ``` -------------------------------- ### Environment Configuration for API Keys Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Specifies the required environment variables for connecting to Qdrant and OpenAI services. Users should copy the example file and fill in their specific API credentials. ```bash # .env configuration file QDRANT_URL=https://your-cluster.cloud.qdrant.io:6333 QDRANT_API_KEY=your-qdrant-api-key OPENAI_API_KEY=sk-your-openai-api-key ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://github.com/loaychlih/structured-rag-hotel-search/blob/main/README.md Sets up an isolated Python environment for backend dependencies. Activate it using the appropriate command for your operating system. ```bash python -m venv venv # Windows: venv\Scripts\activate # Mac/Linux: source venv/bin/activate ``` -------------------------------- ### Check Server Health Status Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Use this endpoint to verify that the backend server is running and properly configured with Qdrant and OpenAI connections. It returns the current configuration status. ```bash # Check server health status curl -X GET "http://localhost:8001/api/health" ``` ```json # Expected response when configured: { "status": "healthy", "qdrant_configured": true, "openai_configured": true, "hotels_loaded": 0, "collection_name": "hotels_structured_rag" } ``` ```json # Expected response when not configured: { "status": "not_configured", "qdrant_configured": false, "openai_configured": false, "hotels_loaded": 0, "collection_name": "hotels_structured_rag" } ``` -------------------------------- ### Create Qdrant Filters from Structured Query Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Generates Qdrant filter objects from a structured query dictionary. 'Region' is a mandatory condition, while others are preferred. ```python from main import create_filters_from_structure from qdrant_client.models import Filter structured_query = { "region": "Europe", "country": "Italy", "type": "Villa", "atmosphere": "Romantic", "activities": ["Wine tasting", "Cooking"], "services": ["Pool", "Restaurant"], "best_months": ["May", "June", "September"] } filters = create_filters_from_structure(structured_query) # The filter object can be used in Qdrant searches: # - Must conditions: region = "Europe" # - Should conditions: country, type, atmosphere, activities (MatchAny), services (MatchAny), best_months (MatchAny) # Use in Qdrant search search_results = qdrant_client.search( collection_name="hotels_structured_rag", query_vector=("description", embedding), query_filter=filters, limit=10 ) ``` -------------------------------- ### Perform Structured RAG Search Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Executes a complete search pipeline including query parsing, filter creation, embedding generation, and dual-vector search. Falls back to traditional RAG if structured parsing fails. ```python from main import structured_rag_search # Perform structured search query = "Family-friendly hotel in Spain with pool and kids activities" hotels, search_type, query_analysis = structured_rag_search(query, top_k=5) # Expected output: # search_type: "structured" # query_analysis: { # "region": "Europe", # "country": "Spain", # "type": "Hotel", # "atmosphere": "Family-friendly", # "activities": [], # "services": ["Pool"], # "best_months": [] # } # hotels: List of hotel dictionaries with combined scores # Each hotel contains: # { # 'id': 42, # 'name': 'Costa Family Resort', # 'description': 'Perfect for families with children...', # 'region': 'Europe', # 'country': 'Spain', # 'type': 'Resort', # 'atmosphere': 'Family-friendly', # 'activities': ['Swimming', 'Beach'], # 'services': ['Pool', 'Restaurant', 'Kids Club'], # 'best_months': ['June', 'July', 'August'], # 'score': 0.8456, # Combined score (60% desc + 40% struct) # 'description_score': 0.8234, # 'structure_score': 0.8789 # } ``` -------------------------------- ### Search Hotels via Chat Endpoint Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Submit natural language queries to this endpoint to receive structured hotel recommendations. It performs a structured RAG search and falls back to traditional RAG if parsing fails. ```bash # Search for hotels with natural language curl -X POST "http://localhost:8001/api/chat" \ -H "Content-Type: application/json" \ -d '{"message": "I want a luxury resort in Thailand with a spa and pool"}' ``` ```json # Expected response: { "response": "Based on your specific requirements, I found these hotels:\n\n1. **Grand Resort Thailand** in Thailand (Asia)\n - Type: Resort\n - Atmosphere: Luxury\n - Activities: Swimming, Relaxation\n - Services: Spa, Pool, Restaurant\n - Match Score: 0.87", "hotels": [ { "id": 1, "name": "Grand Resort Thailand", "description": "A luxurious beachfront resort with world-class amenities", "region": "Asia", "country": "Thailand", "type": "Resort", "atmosphere": "Luxury", "activities": ["Swimming", "Relaxation", "Beach"], "services": ["Spa", "Pool", "Restaurant", "WiFi"], "best_months": ["November", "December", "January", "February"], "score": 0.8732, "description_score": 0.8654, "structure_score": 0.8850 } ], "search_type": "structured", "query_analysis": { "region": "Asia", "country": "Thailand", "type": "Resort", "atmosphere": "Luxury", "activities": ["Swimming"], "services": ["Spa", "Pool"], "best_months": [] } } ``` -------------------------------- ### POST /api/chat Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Processes natural language queries to return ranked hotel recommendations based on structured RAG search. ```APIDOC ## POST /api/chat ### Description The main chat endpoint accepts natural language queries about hotels and returns structured search results with hotel recommendations. It performs structured RAG search with automatic fallback to traditional RAG when structured parsing fails. ### Method POST ### Endpoint /api/chat ### Request Body - **message** (string) - Required - The natural language query describing the desired hotel. ### Request Example { "message": "I want a luxury resort in Thailand with a spa and pool" } ### Response #### Success Response (200) - **response** (string) - A natural language summary of the findings. - **hotels** (array) - A list of recommended hotel objects. - **search_type** (string) - The type of search performed (e.g., structured). - **query_analysis** (object) - The parsed criteria extracted from the query. #### Response Example { "response": "Based on your specific requirements, I found these hotels...", "hotels": [ { "id": 1, "name": "Grand Resort Thailand", "description": "A luxurious beachfront resort", "region": "Asia", "country": "Thailand", "type": "Resort", "atmosphere": "Luxury", "activities": ["Swimming"], "services": ["Spa", "Pool"], "score": 0.87 } ], "search_type": "structured", "query_analysis": { "region": "Asia", "country": "Thailand", "type": "Resort", "atmosphere": "Luxury", "activities": ["Swimming"], "services": ["Spa", "Pool"] } } ``` -------------------------------- ### Use Embeddings for Qdrant Search Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Performs a search in Qdrant using a pre-computed query embedding. Ensure your Qdrant URL and API key are correctly configured. ```python from qdrant_client import QdrantClient client = QdrantClient(url="your-qdrant-url", api_key="your-api-key") results = client.search( collection_name="hotels_structured_rag", query_vector=("description", query_embedding), limit=5 ) ``` -------------------------------- ### Frontend React Chat Interface - Sending Messages Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Handles sending user messages to the backend API and processing the response. It expects JSON data containing search results, search type, and query analysis. ```typescript // Sending a chat message to the backend const handleSendMessage = async (message: string) => { const response = await fetch('http://localhost:8001/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message }), }); if (response.ok) { const data = await response.json(); // data.hotels - array of matching hotels // data.search_type - "structured" or "traditional" // data.query_analysis - parsed query structure // data.response - natural language summary } }; ``` -------------------------------- ### Parse Natural Language Query to Structured Format Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt This Python function uses OpenAI's GPT-4o to convert natural language hotel search queries into structured criteria, including region, country, type, atmosphere, activities, and services. It validates the output against a JSON schema. ```python from main import parse_query_to_structured # Parse a natural language query into structured format query = "Find me a romantic boutique hotel in Paris with a restaurant and wine cellar" structured = parse_query_to_structured(query) # Expected output: # { # "region": "Europe", # "country": "France", # "type": "Boutique", # "atmosphere": "Romantic", # "activities": [], # "services": ["Restaurant", "Wine cellar"], # "best_months": [] # } # Another example with activities query = "Adventure lodge in Africa for safari and hiking" structured = parse_query_to_structured(query) # Expected output: # { # "region": "Africa", # "country": null, # "type": "Lodge", # "atmosphere": "Adventure", # "activities": ["Safari", "Hiking"], # "services": [], # "best_months": [] # } ``` -------------------------------- ### Generate Text Embeddings for Semantic Search Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt This Python function generates vector embeddings for text using OpenAI's text-embedding-3-small model. These embeddings are crucial for performing semantic searches within the Qdrant vector database. ```python from main import get_embedding # Generate embedding for hotel description search query_embedding = get_embedding("luxury beachfront resort with spa") # Returns: List[float] with 1536 dimensions ``` -------------------------------- ### Frontend React Chat Interface - Health Check Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Checks the health status of the application backend. It retrieves information about the service status and configuration of Qdrant and OpenAI. ```typescript // Checking health status on app start const checkHealthStatus = async () => { const response = await fetch('http://localhost:8001/api/health'); const data = await response.json(); // data.status - "healthy" or "not_configured" // data.qdrant_configured - boolean // data.openai_configured - boolean }; ``` -------------------------------- ### Generate Embedding for Structured Data Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Generates a vector embedding for structured hotel data. This embedding can be used for similarity searches in vector databases. ```python structure_text = "Region: Asia. Country: Thailand. Type: Resort. Atmosphere: Luxury. Services: Spa, Pool" structure_embedding = get_embedding(structure_text) # Returns: List[float] with 1536 dimensions ``` -------------------------------- ### Frontend React Chat Interface - Interfaces Source: https://context7.com/loaychlih/structured-rag-hotel-search/llms.txt Defines TypeScript interfaces for hotel data and chat messages used in the React frontend. These structures help ensure type safety for data exchanged between the frontend and backend. ```typescript // TypeScript interfaces for the frontend interface Hotel { id: number; name: string; description: string; region?: string; country?: string; type?: string; atmosphere?: string; activities?: string[]; services?: string[]; best_months?: string[]; score: number; description_score: number; structure_score: number; } interface ChatMessage { id: string; type: 'user' | 'bot'; content: string; hotels?: Hotel[]; searchType?: string; queryAnalysis?: any; timestamp: Date; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.