+
    QV-jï'  ã                   ó8  € R t ^ RIt^ RIt^ RIHt ^ RIHt ^RIHt ^RI	H
t
 ]
! 4       '       d   ^ RIHt ^ RIHtHt ^ RIHtHtHt ^ R	IHt ^R
IHtHtHt ]'       d   ^ RIHtHtHtHt  ! R R]RR7      t0 Rmt]P@                  ! ]!4      t" ! R R]4      t#R# )a
  
Handler for the /v1/completions endpoint (OpenAI legacy Completions API).

Accepts a freeform text prompt (no chat template) and returns generated text
in choices[].text. Supports streaming and non-streaming modes, and suffix for
fill-in-the-middle text insertion.
N)ÚAsyncGenerator)ÚTYPE_CHECKING)Úlogging)Úis_serve_available)ÚHTTPException)ÚJSONResponseÚStreamingResponse)Ú
CompletionÚCompletionChoiceÚCompletionUsage)ÚCompletionCreateParamsBase)ÚBaseGenerateManagerÚBaseHandlerÚ_StreamError)ÚGenerationConfigÚPreTrainedModelÚPreTrainedTokenizerFastÚProcessorMixinc                   ó,   a € ] tR t^-t o V 3R ltRtV tR# )Ú&TransformersTextCompletionCreateParamsc                ó>   <€ V ^8„  d   Qh/ S[ ;R&   S[;R&   S[;R&   # )é   Úgeneration_configÚseedÚstream)ÚstrÚintÚbool)ÚformatÚ__classdict__s   "€Út/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/transformers/cli/serving/completion.pyÚ__annotate__Ú3TransformersTextCompletionCreateParams.__annotate__-   s&   ø‡ ‚ ÙÑñ á
�Iñ ñ �Lò ó    © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)r   s   @r    r   r   -   s   ø‡ ‡ „ r#   r   F)Útotalc                   ó¢   a a€ ] tR t^Bt oRt]t]tV3R lR lt	RV3R lR llt
RV3R lR lltRV3R lR	 lltRV3R
 lV 3R llltRtVtV ;t# )ÚCompletionHandlerzýHandler for the `/v1/completions` endpoint.

Takes a raw text ``prompt`` (no chat template) and generates text returned in
``choices[].text``. Supports streaming (SSE) and non-streaming (JSON) responses,
and ``suffix`` for fill-in-the-middle insertion.
c                ó*   <€ V ^8„  d   QhRS[ RS[RR/# )r   ÚbodyÚ
request_idÚreturnz StreamingResponse | JSONResponse)Údictr   )r   r   s   "€r    r!   ÚCompletionHandler.__annotate__M   s#   ø€ ÷ 'ñ '©ð '¹3ð 'ÐCeñ 'r#   c           
   ƒ  óD  "  € V P                  V4       VP                  RR4      p\        V\        4      '       g   \	        RRR7      hV P                  V4      w  rEpV P                  P                  WVR7      pV P                  P                  WW4      p\        P                  RV RV 24       V P                  P                  WHR	7      p	\        VR
V4      p
T
! Y8'       d   RMRR7      pV'       g   VP                  VP                  4      pV P!                  WP"                  VR	7      pV'       d   V	P%                  W\4       VP                  R4      pVP                  R4      pV'       d   V P'                  W%WdW¼W�4      # V P)                  W%WdW¼W�4      G Rj  x€L
 #  L5i)ac  Validate the request, load the model, and dispatch to streaming or non-streaming.

Args:
    body (`dict`): The raw JSON request body (OpenAI legacy completions format).
    request_id (`str`): Unique request identifier (from header or auto-generated).

Returns:
    `StreamingResponse | JSONResponse`: SSE stream or JSON depending on ``body["stream"]``.
ÚpromptÚ i�  zprompt must be a string.)Ústatus_codeÚdetail)Ú	processorz[Request received] Model: z, CB: ©Úuse_cbÚ	tokenizerNÚpt)Úreturn_tensorsÚsuffixr   )Ú_validate_requestÚgetÚ
isinstancer   r   Ú_resolve_modelÚmodel_managerÚget_model_modalityÚgeneration_stateÚuse_continuous_batchingÚloggerÚwarningÚget_managerÚgetattrÚtoÚdeviceÚ_build_generation_configr   Úinit_cbÚ
_streamingÚ_non_streaming)Úselfr0   r1   r6   Úmodel_idÚmodelr:   Úmodalityr<   Úgen_managerr=   ÚinputsÚ
gen_configr@   Ú	streamings   &&&            r    Úhandle_requestÚ CompletionHandler.handle_requestM   sr  é € ð 	×Ñ˜tÔ$à—‘˜( BÓ'ˆÜ˜&¤#×&Ò&Ü¨CÐ8RÔSÐSà%)×%8Ñ%8¸Ó%>Ñ"ˆ˜Ø×%Ñ%×8Ñ8¸Ð8ÓTˆØ×&Ñ&×>Ñ>¸uÓOˆÜ�‰Ð3°H°:¸VÀFÀ8ÐLÔMØ×+Ñ+×7Ñ7¸Ð7ÓPˆä˜I {°IÓ>ˆ	Ù˜6¾&±$ÀdÔKˆßØ—Y‘Y˜uŸ|™|Ó,ˆFà×2Ñ2°4×9PÑ9PÐY_Ð2Ó`ˆ
ßØ×Ñ Ô2à—‘˜(Ó#ˆØ—H‘H˜XÓ&ˆ	çØ—?‘? :°iÈ6Ð_jÓsÐsà×,Ñ,Ø 9¸ÈKó÷ ð ñ ùs   ‚C0F Ã3A:F Å.+F ÆFÆF c                óV   <€ V ^8„  d   QhRS[ RRRRRS[ RS[RR	R
S[RS[ R,          RR/	# )r   r1   rU   r   r:   ú(ProcessorMixin | PreTrainedTokenizerFastrT   rX   rY   r   rW   r@   Nr2   r   ©r   r3   r   )r   r   s   "€r    r!   r4   x   st   ø€ ÷ :Lñ :Láð:Lð !ð:Lð >ð	:Lñ
 ð:Lñ ð:Lð 'ð:Lñ )ð:Lñ �d•
ð:Lð 
ñ:Lr#   c	           	     ó   a aaaaaaa€ VP                  W#VSSR7      w  ooVR,          p	\        V	\        4      '       d   \        V	4      MV	P                  R,          oR VVVVVV VV3R llp
\        V
! 4       RR7      # )zStream tokens as SSE.©r1   Ú	input_idsc                ó>   € V ^8„  d   QhR\         \        R3,          /# )r   r2   N)r   r   )r   s   "r    r!   Ú2CompletionHandler._streaming.<locals>.__annotate__ˆ   s   € ÷ (	ñ (	œ~¬c°4¨iÕ8ñ (	r#   c            	     ó°  <"  €  R p V '       g?   S
P                  4       G Rj  x€L
 pV.p  VP                  S
P                  4       4       K"  SP                  RJ;'       d    SP                  SP                  8¬  pV'       d   RMRpSe   SP                  SS	SR7      5x € \        SSP                  SSP                  ,           R	7      pSP                  SS	WVR
7      5x € R#  LÂ  \        P                   d     Mi ; i. pT Fz  pTf   Rp  Ms\        T\        4      '       d7   TP                  RTP                   R24       RP                  T4      5x €  R# TP                  SP                  SS	TR7      4       K|  	  T'       g   EKˆ  RP                  T4      5x € EKŸ    \        \        P                  3 d    SP                  4        h i ; i5i)FNTzdata: {"error": "z"}

r7   )ÚtextÚlengthÚstop©Úprompt_tokensÚcompletion_tokensÚtotal_tokens)Úfinish_reasonÚusage)rB   ÚappendÚ
get_nowaitÚasyncioÚ
QueueEmptyrC   r   ÚmsgÚjoinÚ_build_chunk_sseÚmax_new_tokensrl   r   ÚGeneratorExitÚCancelledErrorÚcancel)Údonerf   ÚbatchÚ	sse_partsÚhit_maxrm   rn   rY   Ú	input_lenrT   Úqueuer1   rS   Ústreamerr@   s          €€€€€€€€r    Ússe_genÚ-CompletionHandler._streaming.<locals>.sse_genˆ   s¯  øé € ð'Ø�ßØ!&§¡£×,�DØ!˜F�EðØ"Ø!ŸL™L¨×)9Ñ)9Ó);Ö<ð& %×3Ñ3¸4Ð?×vÐvÀH×DYÑDYÐ]g×]vÑ]vÑDv�ß,3¡¸�àÒ%Ø×/Ñ/°
¸HÈ6Ð/ÓRÓRÜ'Ø"+Ø&.×&;Ñ&;Ø!*¨X×-BÑ-BÕ!Bô�ð
 ×+Ñ+¨J¸ÐP]Ð+ÓkÕkñC -øô
 #×-Ñ-ô Ùðúð ,.�IÛ %˜Øš<Ø#'˜DÙ!Ü% d¬L×9Ò9Ø%×,Ñ,Ð/AÀ$Ç(Á(ÀÈ7Ð-SÔTØ"$§'¡'¨)Ó"4Ó4Ú"à!×(Ñ(¨×)>Ñ)>¸zÈ8ÐZ^Ð)>Ó)_Ö`ñ !&÷ !’yØ Ÿg™g iÓ0Ö0øô "¤7×#9Ñ#9Ð:ô Ø—‘Ô!Øðüst   ƒG…	F& �F& ¢C%£F& «"C' ÁF& Á"!F& ÂAF& Ã#GÃ%F& Ã'C?Ã<F& Ã>C?Ã?AF& ÅGÅ,F& ÆF& Æ&-GÇGztext/event-stream©Ú
media_typeéÿÿÿÿ)Úgenerate_streamingrC   ÚlistÚlenÚshaper   )rS   r1   rU   r:   rT   rX   rY   rW   r@   rb   r�   r~   r   r€   s   ff&&f&f&f  @@@r    rQ   ÚCompletionHandler._streamingx   sl   ÿ€ ð &×8Ñ8¸È6ÐS]ÐjtÐ8Óu‰ˆˆxØ˜;Õ'ˆ	Ü&0°¼D×&AÒ&A”C˜	”NÀyÇÁÐWYÕGZˆ	÷(	÷ (	ôT !¡£Ð7JÔKÐKr#   c                óV   <€ V ^8„  d   QhRS[ RRRRRS[ RS[RR	R
S[RS[ R,          RR/	# )r   r1   rU   r   r:   r^   rT   rX   rY   r   rW   r@   Nr2   r   r_   )r   r   s   "€r    r!   r4   ¶   st   ø€ ÷ -añ -aáð-að !ð-að >ð	-añ
 ð-añ ð-að 'ð-añ )ð-añ �d•
ð-að 
ñ-ar#   c	              ƒ  ó²  "  € VP                  W#WVVR7      G Rj  x€L
 w  ršpVe	   W˜,           p	\        V4      pVP                  RJ;'       d    WÆP                  8¬  pV'       d   RMRp\        V
VW¬,           R7      p\	        V\        \        P                  ! 4       4      V\        V	^ RVR7      .RVR7      p\        VP                  R	R
7      RR7      #  Lº5i)z)Run generation and return a JSONResponse.ra   Nrg   rh   ri   ©rf   ÚindexÚlogprobsrm   Útext_completion)ÚidÚcreatedrU   ÚchoicesÚobjectrn   T)Úexclude_nonezapplication/jsonrƒ   )
Úgenerate_non_streamingrˆ   rv   r   r	   r   Útimer
   r   Ú
model_dump)rS   r1   rU   r:   rT   rX   rY   rW   r@   rf   r~   Úgenerated_idsrk   r}   rm   rn   Úresults   &&&&&&&&&        r    rR   Ú CompletionHandler._non_streaming¶   sî   é € ð 0;×/QÑ/QØ˜f¸Zð 0Ró 0
÷ *
Ñ&ˆ˜ð ÒØ•=ˆDä Ó.ÐØ×+Ñ+°4Ð7×jÐjÐ<M×QjÑQjÑ<jˆß$+™°ˆäØ#Ø/Ø"Õ6ô
ˆô ØÜœŸ	š	›Ó$Øä ØØØ!Ø"/ô	ðð %Øô
ˆô  ˜F×-Ñ-¸4Ð-Ó@ÐM_Ô`Ð`ñC*
ùs   ‚CšC›2CÁCÁ%A1Cc                óJ   <€ V ^8„  d   QhRS[ RS[ RS[ RS[ R,          RRRS[ /# )	r   r1   rT   rf   rm   Nrn   zCompletionUsage | Noner2   )r   )r   r   s   "€r    r!   r4   ç   sK   ø€ ÷ (ñ (áð(ñ ð(ñ ð	(ñ
 ˜T•zð(ð (ð(ñ 
ñ(r#   c                óÆ   € \         P                  ! VR\        \        P                  ! 4       4      V\        P                  ! V^ RVR7      .VR7      pV P                  V4      # )a  Build a streaming ``Completion`` chunk and format it as an SSE ``data:`` line.

Uses ``model_construct`` to bypass pydantic validation so that ``finish_reason``
can be ``None`` for mid-stream chunks (the OpenAI SDK's ``CompletionChoice`` only
accepts literal values).
r�   Nr�   )r‘   r”   r’   rU   r“   rn   )r	   Úmodel_constructr   r—   r
   Úchunk_to_sse)rS   r1   rT   rf   rm   rn   Úchunks   &&&&&& r    ru   Ú"CompletionHandler._build_chunk_sseç   sb   € ô ×*Ò*ØØ$ÜœŸ	š	›Ó$Øä ×0Ò0ØØØ!Ø"/ô	ðð ô
ˆð × Ñ  Ó'Ð'r#   c                ó*   <€ V ^8„  d   QhRS[ RRRS[/# )r   r0   Úmodel_generation_configr   r<   )r3   r   )r   r   s   "€r    r!   r4     s$   ø€ ÷ !ñ !©Tð !ÐL^ð !Ñhlñ !r#   c                ó"  <€ \         SV `  WVR7      pVP                  R4      e   \        VR,          4      Vn        VP                  R4      e   R\        VR,          4      ,           Vn        VP                  R4      e   VR,          Vn        V# )zgApply legacy completion params (``max_tokens``, ``frequency_penalty``, ``stop``) on top of base config.r;   Ú
max_tokensÚfrequency_penaltyg      ð?rh   )ÚsuperrO   rB   r   rv   ÚfloatÚrepetition_penaltyÚstop_strings)rS   r0   r£   r<   r   Ú	__class__s   &&&& €r    rO   Ú*CompletionHandler._build_generation_config  s†   ø€ ä!™GÑ<¸TÐciÐ<ÓjÐà�8‰8�LÓ!Ò-Ü/2°4¸Õ3EÓ/FÐÔ,Ø�8‰8Ð'Ó(Ò4Ø36¼¸tÐDWÕ?XÓ9YÕ3YÐÔ0Ø�8‰8�FÓÒ'Ø-1°&­\ÐÔ*à Ð r#   r$   )N)r7   NN)F)r%   r&   r'   r(   Ú__doc__r   Ú_valid_params_classÚUNUSED_LEGACY_COMPLETION_FIELDSÚ_unused_fieldsr[   rQ   rR   ru   rO   r*   r+   Ú__classcell__)r«   r   s   @@r    r.   r.   B   sW   ù‡ € ñð AÐØ4€N÷'ð '÷V:Lò :L÷|-aò -a÷b(ò (÷B!÷ !õ !r#   r.   >   ÚnÚechoÚuserÚbest_ofr�   Ústream_optionsÚpresence_penalty)$r­   rq   r—   Úcollections.abcr   Útypingr   Úutilsr   Úutils.import_utilsr   Úfastapir   Úfastapi.responsesr   r   Úopenai.typesr	   r
   r   Ú%openai.types.completion_create_paramsr   r   r   r   Útransformersr   r   r   r   r   r¯   Ú
get_loggerr%   rI   r.   r$   r#   r    Ú<module>rÂ      sƒ   ðñó Û Ý *Ý  å Ý 4ñ ×ÒÝ%ßAßJÑJÝP÷ BÑ A÷ ßgÓgôÐ-GÈuõ ò#Ð ð 
×	Ò	˜HÓ	%€ôQ!˜ö Q!r#   