Skip to content

Conversation

Copy link

Copilot AI commented Oct 21, 2025

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:

  1. Validates whether user input is fitness-related using AI
  2. Generates personalized responses based on user information if validation passes
  3. Returns appropriate error responses for non-fitness inputs

Key Features

Structured Input Model

The endpoint accepts comprehensive user data:

  • user_name: User's name
  • user_goals: List of fitness goals (e.g., ["lose weight", "build muscle"])
  • user_body_info: Dictionary of body metrics (e.g., {weight: 76.8, height: 180, age: 30})
  • user_input: Text input to validate and process

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:

completion = client.beta.chat.completions.parse(
    model="gpt-4o-2024-08-06",
    messages=[...],
    response_format=FitnessValidationResponse,  # Structured output with boolean + score
)

Step 2: Conditional Processing

  • If validation fails (score < 0.5): Returns HTTP 400 with error details
  • If validation succeeds: Generates personalized AI response using all user information

Example Usage

curl -X POST "http://localhost:8000/process-fitness-input" \
  -H "Content-Type: application/json" \
  -d '{
    "user_name": "John Doe",
    "user_goals": ["lose weight", "build muscle"],
    "user_body_info": {"weight": 76.8, "height": 180},
    "user_input": "I want to start a workout routine"
  }'

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

  • main.py: FastAPI application with validation and response generation logic
  • models.py: Pydantic models for type-safe request/response handling
  • requirements.txt: Project dependencies (FastAPI, OpenAI, Pydantic, etc.)
  • test_app.py: Automated tests for validation
  • example_usage.py: Usage examples and demonstrations
  • IMPLEMENTATION.md: Detailed implementation documentation
  • .env.example: Environment variable template for OpenAI API key
  • .gitignore: Python project gitignore configuration

Testing

All tests pass successfully:

  • ✅ Pydantic models instantiate correctly
  • ✅ FastAPI app structure is valid
  • ✅ All expected routes exist (/, /health, /process-fitness-input)
  • ✅ Function signatures are correct
  • ✅ Server starts and responds to requests

Setup Instructions

  1. Install dependencies: pip install -r requirements.txt
  2. Configure OpenAI API key: cp .env.example .env and add your key
  3. Start the server: python main.py or uvicorn main:app --reload
  4. Access API documentation at http://localhost:8000/docs

The implementation provides a production-ready FastAPI application with comprehensive error handling, type safety via Pydantic models, and automatic OpenAPI documentation generation.

Original prompt

fast api endpointi var

  • user adı
  • user hedefleri ama liste şeklinde
  • user vücut bilgileri (dict olarak, örneğin {weight: 76.8,..}
  • user input'u
  1. adım: user input'u kullanılarak, modele bu input'un fitness ile ilgili olup olmadığı sorulacak. Bu noktada içinde yalnızca boolean tarzı bi özellik olan veya score tarzı

  2. adım: değere göre kontrol yapılacak
    başarısızsa, hatalı response
    başarılı ise kullanıcı bilgileri prompta dahil edilerek AI'a istek gönderilecek. AI'dan gelen döndürülecek


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 21, 2025 14:23
…neration

Co-authored-by: lightningcell <117159961+lightningcell@users.noreply.github.com>
Co-authored-by: lightningcell <117159961+lightningcell@users.noreply.github.com>
Copilot AI changed the title [WIP] Add endpoint for user input validation and handling Implement FastAPI endpoint with fitness input validation and AI response generation Oct 21, 2025
Copilot AI requested a review from lightningcell October 21, 2025 14:28
Copilot finished work on behalf of lightningcell October 21, 2025 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants