
    @i                         S r SSKrSSKJrJr  SSKJrJr  SSKJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJr  SSKJr  \R0                  " \5      rS\S\4S	 jrg)
z#API routes for FastAPI application.    N)	APIRouterHTTPException)DictAny)QueryRequestQueryResponseFeedbackRequestFeedbackResponseTrainRequestTrainResponseTrainingHistoryResponseIterationStartRequestIterationStartResponseIterationNextRequestIterationNextResponseIterationFinalizeRequestIterationFinalizeResponse)
WebServiceservicereturnc                 l  ^  [        SS/S9nUR                  S[        S9S[        S[        4U 4S jj5       nUR                  S	[        S9S[
        S[        4U 4S
 jj5       nUR                  S[        S9S[        S[        4U 4S jj5       nUR                  S[        S9S[        4U 4S jj5       nUR                  S[        S9S[        S[        4U 4S jj5       nUR                  S[        S9S[        S[        4U 4S jj5       nUR                  S[        S9S[        S[        4U 4S jj5       nU$ )zuCreate API router with all endpoints.

Args:
    service: WebService instance
    
Returns:
    Configured APIRouter
z/apiapi)prefixtagsz/query)response_modelrequestr   c                    >#     TR                  U R                  U R                  S9n[        S0 UD6$ ! [         a/  n[
        R                  SU 3SS9  [        S[        U5      S9eSnAff = f7f)	zProcess a natural language query and return patterns.

Args:
    request: Query request with natural language query string and optional iteration rounds
    
Returns:
    Query response with patterns, rules, and explanation
)iteration_roundszError in query endpoint: Texc_info  status_codedetailN )		run_queryqueryr   r   	Exceptionloggererrorr   strr   resulter   s      =   /home/ubuntu/codebase/yexijia/保研/iCoLoc/src/web/router.pyr'   create_router.<locals>.query    ss     	@&&w}}wG_G_&`F *6** 	@LL4QC84LHCA??	@s$   A0.4 A0
A-*A((A--A0z	/feedbackc                 .  >#     U R                   S;  a
  [        SSS9eTR                  U R                  U R                  U R                   S9n[        S0 UD6$ ! [         a/  n[        R                  SU 3SS9  [        S	[        U5      S9eS
nAff = f7f)zSave user feedback for a pattern.

Args:
    request: Feedback request with pattern and feedback type
    
Returns:
    Feedback response with status and counts
)positivenegativei  z)Feedback must be 'positive' or 'negative'r"   )
pattern_idpatternfeedbackzError in feedback endpoint: Tr   r!   Nr%   )
r6   r   add_feedbackr4   r5   r
   r(   r)   r*   r+   r,   s      r/   r6   create_router.<locals>.feedback1   s     	@'??# #F 
 ))"-- )) * F
 $-f-- 	@LL7s;dLKCA??	@s)   BAA B
B#*BBBz/trainc                 X  >#     TR                  U R                  =(       d    SU R                  =(       d    SU R                  =(       d    SU R                  =(       d    SS9n[        S0 UD6$ ! [         a/  n[        R                  SU 3SS9  [        S	[        U5      S
9eSnAff = f7f)zTrigger preference model training.

Args:
    request: Training request with hyperparameters
    
Returns:
    Training response with loss history

   gMbP?    g333333?)epochslearning_rate
batch_sizemarginzError in train endpoint: Tr   r!   r"   Nr%   )run_trainingr<   r=   r>   r?   r   r(   r)   r*   r   r+   r,   s      r/   traincreate_router.<locals>.trainL   s     
	@))~~+%33<u"--3~~,	 * F !*6** 	@LL4QC84LHCA??	@s)   B*A(A. -B*.
B'8*B""B''B*z/train/historyc                     >#     TR                  5       n [        S0 U D6$ ! [         a/  n[        R	                  SU 3SS9  [        S[        U5      S9eSnAff = f7f)zXGet training history.

Returns:
    Training history response with all training records
z$Error in training history endpoint: Tr   r!   r"   Nr%   )get_training_historyr   r(   r)   r*   r   r+   )historyr.   r   s     r/   rD   +create_router.<locals>.get_training_historyb   sb     	@224G*5W55 	@LL?sCdLSCA??	@s$   A  A
A*AAAz/iteration/startc                    >#     TR                  U R                  U R                  5      n[        S0 UD6$ ! [         a/  n[
        R                  SU 3SS9  [        S[        U5      S9eSnAff = f7f)zStart an interactive iteration session (Stage4).

Args:
    request: Iteration start request with query and rounds
    
Returns:
    Iteration start response with session_id and first round results
z#Error in iteration start endpoint: Tr   r!   r"   Nr%   )	start_iterationr'   r   r   r(   r)   r*   r   r+   r,   s      r/   rH   &create_router.<locals>.start_iterationq   sp     	@,,W]]G<T<TUF)3F33 	@LL>qcBTLRCA??	@%   A206 A2
A/ *A**A//A2z/iteration/nextc                    >#     TR                  U R                  U R                  5      n[        S0 UD6$ ! [         a/  n[
        R                  SU 3SS9  [        S[        U5      S9eSnAff = f7f)zProcess feedback, train model, and return next iteration round (Stage4).

Args:
    request: Iteration next request with session_id and feedback
    
Returns:
    Iteration next response with next round results
z"Error in iteration next endpoint: Tr   r!   r"   Nr%   )	next_iteration
session_idr6   r   r(   r)   r*   r   r+   r,   s      r/   rL   %create_router.<locals>.next_iteration   sr     	@++G,>,>@P@PQF(2622 	@LL=aSADLQCA??	@rJ   z/iteration/finalizec                    >#     TR                  U R                  5      n[        S0 UD6$ ! [         a/  n[        R                  SU 3SS9  [        S[        U5      S9eSnAff = f7f)zGet final results after all iterations (Stage4).

Args:
    request: Iteration finalize request with session_id
    
Returns:
    Iteration finalize response with final results
z&Error in iteration finalize endpoint: Tr   r!   r"   Nr%   )finalize_iterationrM   r   r(   r)   r*   r   r+   r,   s      r/   rP   )create_router.<locals>.finalize_iteration   sk     	@//0B0BCF,6v66 	@LLA!EPTLUCA??	@s$   A'%+ A'
A$*AA$$A')r   postr   r   r
   r	   r   r   getr   r   r   r   r   r   r   )	r   routerr'   r6   rA   rD   rH   rL   rP   s	   `        r/   create_routerrU      s    fE73F[[-[8@\ @m @ 9@  [[-=[>@ @4D @ ?@4 [[-[8@\ @m @ 9@* ZZ 1HZI@(? @ J@ [[#4J[K@'< @AW @ L@  [["3H[I@&: @?T @ J@  [[&7P[Q@*B @G` @ R@  M    )__doc__loggingfastapir   r   typingr   r   web.schemasr   r   r	   r
   r   r   r   r   r   r   r   r   r   web.servicesr   	getLogger__name__r)   rU   r%   rV   r/   <module>r_      sP    )  ,     $			8	$O: O) OrV   