File size: 1,575 Bytes
8ba64a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from app.config import get_settings
from app.schemas.requests import ExtractionRequest, FollowSchemaRequest
from app.schemas.schema_tools import validate_json_schema
from app.utils.logger import setup_logger

logger = setup_logger(__name__)
settings = get_settings()


def validate_extract_request(request: ExtractionRequest):
    """Validate the request to extract attributes."""
    request.max_attempts = max(request.max_attempts, 1)
    request.max_attempts = min(request.max_attempts, 5)

    # Limit the number of images to 10
    if len(request.img_urls) > 10:
        logger.warning(
            f"Number of images exceeds 10: {len(request.img_urls)}. Limiting to 10."
        )
        request.img_urls = request.img_urls[:10]

    for url in request.img_urls:
        if not url.startswith("http"):
            raise ValueError(f"Invalid URL: {url}")

    # validate_json_schema(request.data_schema)

    if request.ai_model.lower() not in settings.SUPPORTED_MODELS:
        raise ValueError(
            f"Invalid ai_model: {request.ai_model}, only support {settings.SUPPORTED_MODELS}"
        )


def validate_follow_request(request: FollowSchemaRequest):
    """Validate the request to follow a schema."""
    request.max_attempts = max(request.max_attempts, 1)
    request.max_attempts = min(request.max_attempts, 5)

    validate_json_schema(request.data_schema)

    if request.ai_model.lower() not in settings.SUPPORTED_MODELS:
        raise ValueError(
            f"Invalid ai_model: {request.ai_model}, only support {settings.SUPPORTED_MODELS}"
        )