Implement FastAPI endpoint with fitness input validation and AI response generation #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a complete FastAPI application that processes and validates fitness-related user inputs with a two-step AI validation and response generation pipeline.
Overview
The implementation addresses the requirement for a FastAPI endpoint that:
Key Features
Structured Input Model
The endpoint accepts comprehensive user data:
{weight: 76.8, height: 180, age: 30})Two-Step Processing Pipeline
Step 1: Fitness Validation
Uses OpenAI's structured output feature with GPT-4o to determine if the input is fitness-related:
Step 2: Conditional Processing
Example Usage
Success Response (200):
{ "response": "Based on your goals of losing weight and building muscle...", "fitness_score": 0.95 }Error Response (400):
{ "detail": { "error": "Input is not fitness-related", "details": "The provided input does not appear to be related to fitness, health, or wellness. Confidence score: 0.25" } }Files Added
Testing
All tests pass successfully:
/,/health,/process-fitness-input)Setup Instructions
pip install -r requirements.txtcp .env.example .envand add your keypython main.pyoruvicorn main:app --reloadhttp://localhost:8000/docsThe implementation provides a production-ready FastAPI application with comprehensive error handling, type safety via Pydantic models, and automatic OpenAPI documentation generation.
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.