ó
    |¤jÿ8  ã                   óÆ   • S r SSKrSSKJrJrJrJrJrJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJrJrJrJr  SS
KJrJr  SSKJr  SSKJr   " S S\5      r g)zo
@File    :   application.py
@Date    :   2024-02-24
@Desc    :   Application calls for both http and http sse
é    N)ÚAnyÚDictÚ	GeneratorÚListÚOptionalÚUnion)Ú_build_api_request)ÚMessageÚRole)ÚApplicationResponse)ÚBaseApi)Úget_default_api_key)ÚDEPRECATED_MESSAGEÚHISTORYÚMESSAGESÚPROMPT)ÚInputRequiredÚInvalidInput)Úlogger)Úmerge_single_responsec            3       ó–  • \ rS rSrSrSr  " S S5      r\S 5       r\                    S"S\	S	\	S
\
S\	S\	S\\   S\\   S\\   S\\   S\\   S\\   S\\	   S\\\	\4      S\\   S\\\	      S\\	   S\\	   S\\\	      S\\\	      S\\\	\4      S\\   S\\\\SS4   4   4,S jj5       r\S 5       r\S  5       rS!rg)#ÚApplicationé   ÚappsÚ
completionc                   ó    • \ rS rSrSrSrSrSrg)ÚApplication.DocReferenceTypeé"   z%doc reference type for rag completionÚsimpleÚindexed© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r    Ú__static_attributes__r!   ó    Ú§C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\dashscope/app/application.pyÚDocReferenceTyper   "   s   † Ù3àˆà‹r(   r*   c                 óL   • Uc
  [        5       nUb  U(       d  [        S5      eX4$ )NzApp id is required!)r   r   )ÚclsÚapi_keyÚapp_ids      r)   Ú_validate_paramsÚApplication._validate_params)   s,   € ð ‰?Ü)Ó+ˆGà‰>¦ÜÐ 5Ó6Ð6àˆÐr(   Nr.   ÚpromptÚhistoryÚ	workspacer-   ÚmessagesÚstreamÚtemperatureÚtop_pÚtop_kÚseedÚ
session_idÚ
biz_paramsÚhas_thoughtsÚdoc_tag_codesÚdoc_reference_typeÚ	memory_idÚ
image_listÚ	file_listÚrag_optionsÚincremental_outputÚreturnc                 óø  • [         R                  XQ5      u  pQUb  U(       d  Ub  [        U5      S:X  a  [        S5      e0 nUb#  U(       a  UR	                  S0 5      nUUS'   UUS'   Ub  UUS'   Ub  UUS'   U	b  U	US'   U
b  U
US	'   Ub  UUS
'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   Ub  UUS'   0 UEUEnUR                  SS5      nUR                  SS5      nU=(       a    USL nU(       a  SUS'   U(       a  SOSnUR                  SS5      nSU 3nU(       a  U SU 3R                  5       OUUS'   U R                  " UUU40 UD6u  n n![        S SU [         R                  U[         R                  UUSSS.	U!D6n"U"R                  5       n#U(       a!  U(       a  U R                  U#5      $ S U# 5       $ [        R                  " U#5      $ )!a7  Call app completion service.

Args:
    app_id (str): Id of bailian application
    prompt (str): The input prompt.
    history (list):The user provided history
        examples:
            [{'user':'The weather is fine today.',
            'bot': 'Suitable for outings'}].
        Defaults to None.
    workspace(str, `optional`): Workspace for app completion call
    api_key (str, optional): The api api_key, can be None,
        if None, will get by default rule(TODO: api key doc).
    messages(list): The generation messages.
    stream (bool, optional): Enable server-sent events
        (ref: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)  # noqa E501  # pylint: disable=line-too-long
        the result will back partially[qwen-turbo,bailian-v1].
    temperature (float, optional): Used to control the degree
        of randomness and diversity. Specifically, the temperature
        value controls the degree to which the probability distribution
        of each candidate word is smoothed when generating text.
        A higher temperature value will reduce the peak value of
        the probability, allowing more low-probability words to be
        selected, and the generated results will be more diverse;
        while a lower temperature value will enhance the peak value
        of the probability, making it easier for high-probability
        words to be selected, the generated results are more
        deterministic, range(0, 2) .[qwen-turbo,qwen-plus].
    top_p (float, optional): A sampling strategy, called nucleus
        sampling, where the model considers the results of the
        tokens with top_p probability mass. So 0.1 means only
        the tokens comprising the top 10% probability mass are
        considered[qwen-turbo,bailian-v1].
    top_k (int, optional): The size of the sample candidate set when generated.  # noqa E501  # pylint: disable=line-too-long
        For example, when the value is 50, only the 50 highest-scoring tokens  # noqa E501  # pylint: disable=line-too-long
        in a single generation form a randomly sampled candidate set. # noqa E501
        The larger the value, the higher the randomness generated;  # noqa E501
        the smaller the value, the higher the certainty generated. # noqa E501
        The default value is 0, which means the top_k policy is  # noqa E501
        not enabled. At this time, only the top_p policy takes effect. # noqa E501
    seed (int, optional): When generating, the seed of the random number is used to control the  # pylint: disable=line-too-long
        randomness of the model generation. If you use the same seed, each run will generate the same results;  # pylint: disable=line-too-long
        you can use the same seed when you need to reproduce the model's generated results.  # pylint: disable=line-too-long
        The seed parameter supports unsigned 64-bit integer types. Default value 1234
    session_id (str, optional): Session if for multiple rounds call.
    biz_params (dict, optional): The extra parameters for flow or plugin.
    has_thoughts (bool, optional): Flag to return rag or plugin process details. Default value false.  # pylint: disable=line-too-long
    doc_tag_codes (list[str], optional): Tag code list for doc retrival.
    doc_reference_type (str, optional): The type of doc reference.
        simple: simple format of doc retrival which not include index in response text but in doc reference list.  # pylint: disable=line-too-long
        indexed: include both index in response text and doc reference list
    memory_id (str, optional): Used to store long term context summary between end users and assistant.  # pylint: disable=line-too-long
    image_list (list, optional): Used to pass image url list.
    file_list (list, optional): Used to pass file url list.
    rag_options (dict, optional): Rag options for retrieval augmented generation options.  # pylint: disable=line-too-long
    incremental_output (bool, optional): In streaming mode, output only
        new tokens (True) vs. cumulative output (False).
    **kwargs: Additional parameters passed to the API.

Raises:
    InvalidInput: The history and auto_history are mutually exclusive.

Returns:
    Union[CompletionResponse,
          Generator[CompletionResponse, None, None]]: If
    stream is True, return Generator, otherwise GenerationResponse.
Nr   zprompt or messages is required!ÚheaderszX-DashScope-WorkSpacer5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   FTÚ1Ú0Ú
user_agentÚ zincremental_to_full/z; Úapp)	ÚmodelÚinputÚ
task_groupÚtaskÚfunctionr3   r-   Ú
is_serviceÚ
sdk_modulec              3   óN   #   • U  H  n[         R                  " U5      v •  M     g 7f©N)r   Úfrom_api_response)Ú.0Úrsps     r)   Ú	<genexpr>Ú#Application.call.<locals>.<genexpr>ý   s%   é € ð â'˜ô (×9Ò9¸#×>Ð>Ú'ùs   ‚#%r!   )r   r/   Úlenr   ÚpopÚgetÚstripÚ_build_input_parametersr	   rN   rP   ÚcallÚ_merge_application_responser   rU   )$r,   r.   r1   r2   r3   r-   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   ÚkwargsÚexplicit_kwargsrF   Úmerged_kwargsÚ	is_streamÚis_incremental_outputÚto_merge_incremental_outputÚflagÚexisting_uaÚnew_uarM   Ú
parametersÚrequestÚresponses$                                       r)   r_   ÚApplication.call7   sÄ  € ôN &×6Ñ6°wÓG‰ˆà‰N¦&ØÑ¤ H£°Ó 2äÐ AÓBÐBð ˆØÑ ¦YØ—j‘j ¨BÓ/ˆGØ/8ˆGÐ+Ñ,Ø)0ˆO˜IÑ&àÑØ(.ˆO˜HÑ%ØÑ"Ø-8ˆO˜MÑ*ØÑØ',ˆO˜GÑ$ØÑØ',ˆO˜GÑ$ØÑØ&*ˆO˜FÑ#ØÑ!Ø,6ˆO˜LÑ)ØÑ!Ø,6ˆO˜LÑ)ØÑ#Ø.:ˆO˜NÑ+ØÑ$Ø/<ˆO˜OÑ,ØÑ)Ø4FˆOÐ0Ñ1ØÑ Ø+4ˆO˜KÑ(ØÑ!Ø,6ˆO˜LÑ)ØÑ Ø+4ˆO˜KÑ(ØÑ"Ø-8ˆO˜MÑ*ØÑ)Ø4FˆOÐ0Ñ1ð 6˜6Ð5 _Ð5ˆð "×%Ñ% h°Ó6ˆ	Ø -× 1Ñ 1Ð2FÈÓ MÐà×8Ð/°5Ð8ð 	$ö 'Ø26ˆMÐ.Ñ/ö 2‰s°sˆØ#×'Ñ'¨°bÓ9ˆØ'¨ vÐ.ˆæ2=ˆ{ˆm˜2˜f˜XÐ&×,Ñ,Ô.À6ð 	�lÑ#ð ×'Ò'ØØØñ
ð ñ	
ñ	
ØØô %ð 
ØØÜ"×-Ñ-ØÜ ×)Ñ)ØØØØñ
ð ñ
ˆð —<‘<“>ˆæÞ*Ø×6Ñ6°xÓ@Ð@ñá'óð ô
 '×8Ò8¸ÓBÐBr(   c                 óX  • 0 n0 nUbK  [         R                  " U5      nUb)  U(       a"  UR                  [        R                  US.5        [
        U0nOGUb<  U(       a5  [        R                  " [        5        X&[        '   Ub  U(       a  X[        '   OX[        '   UR                  SS 5      nUb  U(       a  X†S'   UR                  SS 5      n	U	b  U	(       a  X–S'   UR                  SS 5      n
U
b<  [        U
[        5      (       a  [        S U
 5       5      (       a  X¦S'   O[        S5      eUR                  SS 5      nUb  X¶S'   UR                  SS 5      nUb  U(       a  XÆS'   UR                  S	S 5      nUb  U(       a  XÖS	'   UR                  S
S 5      nUb  U(       a  XæS
'   U0 UEUE4$ )N)ÚroleÚcontentr:   r>   r=   c              3   óB   #   • U  H  n[        U[        5      v •  M     g 7frT   )Ú
isinstanceÚstr)rV   Úitems     r)   rX   Ú6Application._build_input_parameters.<locals>.<genexpr>&  s   é € ð 7Ú2?¨$”
˜4¤×%Ð%²-ùs   ‚z doc_tag_codes is not a List[str]r?   r;   r@   rA   )ÚcopyÚdeepcopyÚappendr   ÚUSERr   r   Úwarningr   r   r   r[   rr   ÚlistÚallr   )r,   r1   r2   r4   ra   rj   Úinput_paramÚmsgsr:   r>   r=   r?   r;   r@   rA   s                  r)   r^   Ú#Application._build_input_parameters  s©  € ð ˆ
àˆØÑÜ—=’= Ó*ˆDØÑ!¦fØ—‘¤T§Y¡Y¸6ÑBÔCÜ# TÐ*‰KØÑ ¦WÜ�NŠNÔ-Ô.Ø#*œÑ ØÑ!¦fØ&,œFÑ#øà"(œÑà—Z‘Z ¨dÓ3ˆ
ØÑ!¦jØ(2˜Ñ%à#ŸZ™ZÐ(<¸dÓCÐØÑ)Ö.@Ø0BÐ,Ñ-àŸ
™
 ?°DÓ9ˆØÑ$Ü˜-¬×.Ñ.´3ñ 7Ù2?ó7÷ 4ñ 4ð 0=˜OÒ,ä"Ð#EÓFÐFà—J‘J˜{¨DÓ1ˆ	ØÑ Ø'0˜Ñ$à—Z‘Z ¨dÓ3ˆ
ØÑ!¦jØ(2˜Ñ%à—Z‘Z ¨dÓ3ˆ
ØÑ!¦jØ(2˜Ñ%à—J‘J˜{¨DÓ1ˆ	ØÑ ¦YØ'0˜Ñ$àÐ4˜zÐ4¨VÐ4Ð4Ð4r(   c              #   ó’  #   • 0 nU H»  n[         R                  " U5      nUR                  (       a,  [        UR                  S5      (       d  SUR                  l        [        UU5      nUSL a  Uv •  Mm  [        U[        5      (       a  U H  nUv •  M	     M‘  [        R                  " S[        U5      R                  5        M½     g7f)zfMerge incremental application response chunks.

Simulate non-incremental output by accumulating text.
ÚchoicesNTz*Unexpected merge result type: %s, skipping)r   rU   ÚoutputÚhasattrr�   r   rr   r{   r   rz   Útyper"   )r,   rl   Úaccumulated_datarW   Úparsed_responseÚresultÚresps          r)   r`   Ú'Application._merge_application_response?  s±   é € ð ÐãˆCÜ1×CÒCÀCÓHˆOØ×%×%¬gØ×&Ñ&Ø÷/ñ /ð 26�×&Ñ&Ô.Ü*ØØ óˆFð ˜Š~Ø%Ô%Ü˜F¤D×)Ñ)Û"�DØ”Jó #ô —’Ø@Ü˜“L×)Ñ)öò# ùs   ‚CCr!   )NNNNNNNNNNNNNNNNNNNN)r"   r#   r$   r%   rN   rP   r*   Úclassmethodr/   rs   r{   r   r
   r   ÚboolÚfloatÚintr   r   r   r   r   r_   r^   r`   r'   r!   r(   r)   r   r      s;  † Ø€JØ€Hð÷ñ ð ñó ðð ð ØØØØ"&Ø!%Ø'+Ø!%Ø#Ø"Ø$(Ø/3Ø'+Ø-1Ø,0Ø#'Ø*.Ø)-Ø04Ø-1ñ1JCð ð	JCð
 ðJCð ðJCð ðJCð ðJCð �w‘-ðJCð ˜‘ðJCð ˜e‘_ðJCð ˜‰ðJCð ˜‰}ðJCð �s‰mðJCð ˜S‘MðJCð  ˜T # s (™^Ñ,ð!JCð" ˜t‘nð#JCð$    S¡	Ñ*ð%JCð& % S™Mð'JCð( ˜C‘=ð)JCð* ˜T #™YÑ'ð+JCð, ˜D ™IÑ&ð-JCð. ˜d 3¨ 8™nÑ-ð/JCð0 % T™Nð1JCð4 
ØØØØØðñ	
ð	ñ
ô5JCó ðJCðX ñ85ó ð85ðt ñó ór(   r   )!r&   rv   Útypingr   r   r   r   r   r   Ú*dashscope.api_entities.api_request_factoryr	   Ú)dashscope.api_entities.dashscope_responser
   r   Ú"dashscope.app.application_responser   Údashscope.client.base_apir   Údashscope.common.api_keyr   Údashscope.common.constantsr   r   r   r   Údashscope.common.errorr   r   Údashscope.common.loggingr   Údashscope.utils.message_utilsr   r   r!   r(   r)   Ú<module>r˜      sG   ðñó
 ß >× >å Iß CÝ BÝ -Ý 8÷ó ÷ ?Ý +Ý ?ô@�'õ @r(   