
    @i                         d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ ddlmZ  ej0                  e      Zdedefd	Zy)
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                     t        ddg      }|j                  dt              dt        dt        f fd       }|j                  d	t              dt
        dt        f fd
       }|j                  dt              dt        dt        f fd       }|j                  dt              dt        f fd       }|j                  dt              dt        dt        f fd       }|j                  dt              dt        dt        f fd       }|j                  dt              dt        dt        f fd       }|S )zCreate API router with all endpoints.
    
    Args:
        service: WebService instance
        
    Returns:
        Configured APIRouter
    z/apiapi)prefixtagsz/query)response_modelrequestr   c                    K   	 j                  | j                  | j                        }t        di |S # t        $ r5}t
        j                  d| d       t        dt        |            d}~ww xY ww)	a  Process 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      A   /home/ubuntu/codebase/yexijia/保研/colocation_mvp/web/router.pyr'   zcreate_router.<locals>.query    st     	@&&w}}wG_G_&`F *6** 	@LL4QC84LHCA??	@s%   A817 A8	A5 0A00A55A8z	/feedbackc                 @  K   	 | j                   dvrt        dd      j                  | j                  | j                  | j                         }t        di |S # t        $ r5}t        j                  d| d       t        d	t        |            d
}~ww xY ww)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%   )
r5   r   add_feedbackr3   r4   r
   r(   r)   r*   r+   r,   s      r/   r5   zcreate_router.<locals>.feedback1   s     	@'??# #F 
 ))"-- )) * F
 $-f-- 	@LL7s;dLKCA??	@)   BAA B	B&0BBBz/trainc                 @  K   	 j                  | j                  xs d| j                  xs d| j                  xs d| j                  xs d      }t        di |S # t        $ r5}t        j                  d| d       t        d	t        |      
      d}~ww xY ww)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/   trainzcreate_router.<locals>.trainL   s     
	@))~~+%33<u"--3~~,	 * F !*6** 	@LL4QC84LHCA??	@r7   z/train/historyc                     K   	 j                         } t        di | S # t        $ r5}t        j	                  d| d       t        dt        |            d}~ww xY ww)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/   rB   z+create_router.<locals>.get_training_historyb   sc     	@224G*5W55 	@LL?sCdLSCA??	@s$   A!  A!	A0AAA!z/iteration/startc                    K   	 j                  | j                  | j                        }t        di |S # t        $ r5}t
        j                  d| d       t        dt        |            d}~ww xY ww)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/   rE   z&create_router.<locals>.start_iterationq   sq     	@,,W]]G<T<TUF)3F33 	@LL>qcBTLRCA??	@$   A706 A7	A40A//A44A7z/iteration/nextc                    K   	 j                  | j                  | j                        }t        di |S # t        $ r5}t
        j                  d| d       t        dt        |            d}~ww xY ww)a  Process 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_idr5   r   r(   r)   r*   r   r+   r,   s      r/   rH   z%create_router.<locals>.next_iteration   ss     	@++G,>,>@P@PQF(2622 	@LL=aSADLQCA??	@rF   z/iteration/finalizec                    K   	 j                  | j                        }t        di |S # t        $ r5}t        j                  d| d       t        dt        |            d}~ww xY ww)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_iterationrI   r   r(   r)   r*   r   r+   r,   s      r/   rK   z)create_router.<locals>.finalize_iteration   sl     	@//0B0BCF,6v66 	@LLA!EPTLUCA??	@s$   A,%+ A,	A)0A$$A))A,)r   postr   r   r
   r	   r   r   getr   r   r   r   r   r   r   )	r   routerr'   r5   r@   rB   rE   rH   rK   s	   `        r/   create_routerrO      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)   rO   r%   rP   r/   <module>rY      sR    )  ,     $			8	$O: O) OrP   