Task: `/explore`, and `/recommend` endpoints
Now you'll implement the two most sophisticated endpoints that demonstrate advanced Weaviate capabilities: targeted vector search and retrieval-augmented generation (RAG).
Endpoint 4: /explore - Genre-Based Discovery
Purpose: Discovers movies by genre with optional year filtering, sorted by popularity.
Learning focus: Use vector search as a first-pass, then sort results by object properties.
Your Task
Implement genre exploration with ranking:
@app.get("/explore", response_model=ExplorerResponse)
def explore_movies(
genre: str = Query(..., description="Movie genre to explore"),
year_min: Optional[int] = Query(
None, description="Filter by release year - from this year"
),
year_max: Optional[int] = Query(
None, description="Filter by release year - to this year"
),
):
"""
Explore movies by genre(s) and optional year
- Returns most popular movies best matching the specified genre
- Can filter by year
- Sorted by popularity/rating
"""
Implementation Steps
- Build year filters using the same logic as the search endpoint
- Perform hybrid search targeting the "genres" vector specifically
- Sort results by popularity score in descending order
- Return organized results with the query parameters
Implementation Hints
- Use the appropriate target vector
- Build filters the same way as in
/search: handle min only, max only, both, or neither - You can use Python's
sorted()function, with thislambda x: x["popularity"]as key - Check that the sorting is done in descending order (most popular first)
Endpoint 5: /recommend - AI-Powered Recommendations
Purpose: Takes a viewing occasion (like "date night" or "family movie") and uses RAG to recommend suitable movies with explanations.
Learning focus: Combining retrieval with generative AI to create context-aware recommendations.
Your Task
Implement RAG-powered recommendations:
@app.get("/recommend", response_model=RecommendationResponse)
def recommend_movie(
occasion: str = Query(
..., description="Viewing occasion (e.g., 'date night', 'family movie')"
)
):
"""
Get movie recommendations based on viewing occasion
- Generates a query string from occasion
- Performs semantic search against movie descriptions
- Returns best match with reasoning
"""
Implementation Steps
- Convert the occasion to a search query using the provided
movie_occasion_to_query()helper function - Build the task prompt for the AI model using the provided template
- Perform RAG with semantic search using
near_text() - Configure the generative model at runtime
Implementation Hints
The prompt template full_task_prompt is already constructed for you.
For the Weaviate operation:
- Do you remember how to perform generative searches?
- Use grouped task
- Set
target_vector="default"for general semantic search - Configure with
claude-3-5-haiku-latestmodel
Testing Your Endpoints
Once you are finished, you can start the FastAPI server with:
python main.py
Then, you can navigate to http://localhost:8000/docs to explore the API documentation and test your endpoints interactively.
Time to see these advanced patterns in action! Let's explore the complete solutions for genre discovery and RAG-powered movie recommendations.