File size: 1,155 Bytes
36102cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pydantic import BaseModel, Field
from typing import List, Optional


class Source(BaseModel):
    """A document reference or citation that supports the answer."""
    document_id: str
    content: str    
    page_number: Optional[int] = None
    section_title: Optional[str] = None


class QueryRequest(BaseModel):
    """Request model for the agent endpoint."""
    question: str = Field(
        ..., 
        min_length=1, 
        max_length=1000, 
        description="The natural language question to answer"
    )
    selected_text: Optional[str] = Field(
        None, 
        max_length=5000, 
        description="Specific text selected by the user from the book"
    )
    mode: Optional[str] = Field(
        None,
        pattern=r"^(selected_text|book|general)$",
        description="The mode to use when answering (selected_text, book, or general)"
    )


class QueryResponse(BaseModel):
    """Response model for the agent endpoint."""
    answer: str
    sources: List[Source]
    mode: str = Field(
        ...,
        pattern=r"^(selected_text|book|general)$",
        description="The mode used to generate this answer"
    )