
    g-                       d Z ddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lZd
dlmZ d
dlmZmZ d
dlmZ erd
dlmZ d
dlm Z  ed   Z!	  G d de      Z" G d de      Z# G d de      Z$ G d de      Z%ee$e%f   Z&da'	 ddZ(ed d       Z)d!dZ*ed"d#d       Z+ed$d       Z,y	)%zLogic related to making requests to an LLM.

The aim here is to make a common interface for different LLMs, so that the rest of the code can be agnostic to the
specific LLM being used.
    )annotations)ABCabstractmethod)AsyncIteratorIterableIterator)asynccontextmanagercontextmanager)datetime)cache)TYPE_CHECKINGLiteralUnionN   )	UserError)ModelMessageModelResponse)ModelSettings)Usage)ToolDefinition)6zopenai:gpt-4ozopenai:gpt-4o-minizopenai:gpt-4-turbozopenai:gpt-4zopenai:o1-previewzopenai:o1-miniz	openai:o1zopenai:gpt-3.5-turbozgroq:llama-3.3-70b-versatilezgroq:llama-3.1-70b-versatilez*groq:llama3-groq-70b-8192-tool-use-previewz)groq:llama3-groq-8b-8192-tool-use-previewzgroq:llama-3.1-70b-specdeczgroq:llama-3.1-8b-instantzgroq:llama-3.2-1b-previewzgroq:llama-3.2-3b-previewz!groq:llama-3.2-11b-vision-previewz!groq:llama-3.2-90b-vision-previewzgroq:llama3-70b-8192zgroq:llama3-8b-8192zgroq:mixtral-8x7b-32768zgroq:gemma2-9b-itzgroq:gemma-7b-itzgoogle-gla:gemini-1.5-flashzgoogle-gla:gemini-1.5-prozgoogle-gla:gemini-2.0-flash-expzgoogle-vertex:gemini-1.5-flashzgoogle-vertex:gemini-1.5-proz"google-vertex:gemini-2.0-flash-expzmistral:mistral-small-latestzmistral:mistral-large-latestzmistral:codestral-latestz!mistral:mistral-moderation-latestzollama:codellamazollama:gemmazollama:gemma2zollama:llama3zollama:llama3.1zollama:llama3.2zollama:llama3.2-visionzollama:llama3.3zollama:mistralzollama:mistral-nemozollama:mixtralzollama:phi3z
ollama:qwqzollama:qwenzollama:qwen2zollama:qwen2.5zollama:starcoder2z!anthropic:claude-3-5-haiku-latestz"anthropic:claude-3-5-sonnet-latestzanthropic:claude-3-opus-latesttestc                  D    e Zd ZdZe	 	 	 	 	 	 	 	 dd       Zedd       Zy)ModelzAbstract class for a model.c                  K   t               w)a  Create an agent model, this is called for each step of an agent run.

        This is async in case slow/async config checks need to be performed that can't be done in `__init__`.

        Args:
            function_tools: The tools available to the agent.
            allow_text_result: Whether a plain text final response/result is permitted.
            result_tools: Tool definitions for the final result tool(s), if any.

        Returns:
            An agent model.
        NotImplementedError)selffunction_toolsallow_text_resultresult_toolss       P/var/www/openai/venv/lib/python3.12/site-packages/pydantic_ai/models/__init__.pyagent_modelzModel.agent_model\   s     ( "##   c                    t               )Nr   r   s    r!   namez
Model.namer   s    !##    N)r   list[ToolDefinition]r   boolr    r(   return
AgentModelr*   str)__name__
__module____qualname____doc__r   r"   r&    r'   r!   r   r   Y   sS    %$ -$  	$
 +$ 
$ $* $ $r'   r   c                  L    e Zd ZdZe	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 dd       Zy)r+   z/Model configured for each step of an Agent run.c                   K   t               w)zMake a request to the model.r   r   messagesmodel_settingss      r!   requestzAgentModel.requestz   s     
 "##r#   c               N   K   t        d| j                  j                         w)z<Make a request to the model and return a streaming response.z(Streamed requests not supported by this )r   	__class__r.   r5   s      r!   request_streamzAgentModel.request_stream   s&     
 "$LT^^MdMdLe"fggs   #%N)r6   list[ModelMessage]r7   ModelSettings | Noner*   ztuple[ModelResponse, Usage])r6   r<   r7   r=   r*   z%AsyncIterator[EitherStreamedResponse])r.   r/   r0   r1   r   r8   r	   r;   r2   r'   r!   r+   r+   w   s[    9$*$<P$	$$ $ *<P	. r'   r+   c                  f    e Zd ZdZd
dZedd       Zedddd       Zedd       Zedd       Z	y	)StreamTextResponsez2Streamed response from an LLM when returning text.c                    | S )a  Stream the response as an async iterable, building up the text as it goes.

        This is an async iterator that yields `None` to avoid doing the work of validating the input and
        extracting the text field when it will often be thrown away.
        r2   r%   s    r!   	__aiter__zStreamTextResponse.__aiter__   	     r'   c                   K   t               wzNProcess the next chunk of the response, see above for why this returns `None`.r   r%   s    r!   	__anext__zStreamTextResponse.__anext__         "##r#   Ffinalc                   t               )u  Returns an iterable of text since the last call to `get()` — e.g. the text delta.

        Args:
            final: If True, this is the final call, after iteration is complete, the response should be fully validated
                and all text extracted.
        r   r   rH   s     r!   getzStreamTextResponse.get   s     "##r'   c                    t               )zwReturn the usage of the request.

        NOTE: this won't return the full usage until the stream is finished.
        r   r%   s    r!   usagezStreamTextResponse.usage        "##r'   c                    t               z"Get the timestamp of the response.r   r%   s    r!   	timestampzStreamTextResponse.timestamp        "##r'   Nr*   zAsyncIterator[None]r*   None)rH   r)   r*   zIterable[str]r*   r   r*   r   
r.   r/   r0   r1   rA   r   rE   rK   rM   rQ   r2   r'   r!   r?   r?      s]    < $ $ #( $ $ $ $ $ $r'   r?   c                  f    e Zd ZdZd
dZedd       Zedddd       Zedd       Zedd       Z	y	)StreamStructuredResponsez2Streamed response from an LLM when calling a tool.c                    | S )zStream the response as an async iterable, building up the tool call as it goes.

        This is an async iterator that yields `None` to avoid doing the work of building the final tool call when
        it will often be thrown away.
        r2   r%   s    r!   rA   z"StreamStructuredResponse.__aiter__   rB   r'   c                   K   t               wrD   r   r%   s    r!   rE   z"StreamStructuredResponse.__anext__   rF   r#   FrG   c                   t               )a  Get the `ModelResponse` at this point.

        The `ModelResponse` may or may not be complete, depending on whether the stream is finished.

        Args:
            final: If True, this is the final call, after iteration is complete, the response should be fully validated.
        r   rJ   s     r!   rK   zStreamStructuredResponse.get   s     "##r'   c                    t               )ztGet the usage of the request.

        NOTE: this won't return the full usage until the stream is finished.
        r   r%   s    r!   rM   zStreamStructuredResponse.usage   rN   r'   c                    t               rP   r   r%   s    r!   rQ   z"StreamStructuredResponse.timestamp   rR   r'   NrS   rT   )rH   r)   r*   r   rV   rW   rX   r2   r'   r!   rZ   rZ      s]    < $ $ #( $ $ $ $ $ $r'   rZ   Tc                 &    t         st        d      y)a&  Check if model requests are allowed.

    If you're defining your own models that have costs or latency associated with their use, you should call this in
    [`Model.agent_model`][pydantic_ai.models.Model.agent_model].

    Raises:
        RuntimeError: If model requests are not allowed.
    zCModel requests are not allowed, since ALLOW_MODEL_REQUESTS is FalseN)ALLOW_MODEL_REQUESTSRuntimeErrorr2   r'   r!   check_allow_model_requestsrc      s      `aa  r'   c              #  8   K   t         }| a 	 d |a y# |a w xY ww)zContext manager to temporarily override [`ALLOW_MODEL_REQUESTS`][pydantic_ai.models.ALLOW_MODEL_REQUESTS].

    Args:
        allow_model_requests: Whether to allow model requests within the context.
    N)ra   )allow_model_requests	old_values     r!   override_allow_model_requestsrg      s'      %I/)(ys   	 c                @   t        | t              r| S | dk(  rddlm}  |       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  ||       S | j	                  d	      rdd
lm}  || dd       S | j	                  d      rdd
lm}  ||       S | j	                  d      rddl	m
}  || dd       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  || dd       S | j	                  d      rddlm}  ||       S t'        d|        )zInfer the model from the name.r      )	TestModelzopenai:)OpenAIModel   N)gpto1z
google-gla)GeminiModel   geminizgroq:)	GroqModel   zgoogle-vertex)VertexAIModel   z	vertexai:	   zmistral:)MistralModel   zollama:)OllamaModel	anthropic)AnthropicModel
   claudezUnknown model: )
isinstancer   r   rj   
startswithopenairk   rq   ro   groqrr   vertexairt   mistralrw   ollamary   rz   r{   r   )	modelrj   rk   ro   rr   rt   rw   ry   r{   s	            r!   infer_modelr     s   %	&#{			)	$'59%%			-	('5!!			,	''5:&&			(	#' 5!!			'	"#qr##			/	*+U23Z((			+	&+U12Y''			*	%)E!"I&&			)	$'59%%			+	&-eBCj))			(	#-e$$/%122r'   c                n    t        j                  t        j                  | |      dt               i      S )a#  Cached HTTPX async client so multiple agents and calls can share the same client.

    There are good reasons why in production you should use a `httpx.AsyncClient` as an async context manager as
    described in [encode/httpx#2026](https://github.com/encode/httpx/pull/2026), but when experimenting or showing
    examples, it's very useful not to, this allows multiple Agents to use a single client.

    The default timeouts match those of OpenAI,
    see <https://github.com/openai/openai-python/blob/v1.54.4/src/openai/_constants.py#L9>.
    timeoutconnectz
User-Agent)r   headers)httpxAsyncClientTimeoutget_user_agentr   s     r!   cached_async_http_clientr   D  s0     gw?~/0 r'   c                     ddl m}  d|  S )z.Get the user agent string for the HTTP client.r   __version__zpydantic-ai/) r   r   s    r!   r   r   U  s     +''r'   rT   )re   r)   r*   zIterator[None])r   zModel | KnownModelNamer*   r   )iX  rs   )r   intr   r   r*   zhttpx.AsyncClientr,   )-r1   
__future__r   _annotationsabcr   r   collections.abcr   r   r   
contextlibr	   r
   r   	functoolsr   typingr   r   r   r   
exceptionsr   r6   r   r   settingsr   resultr   toolsr   KnownModelNamer   r+   r?   rZ   EitherStreamedResponsera   rc   rg   r   r   r   r2   r'   r!   <module>r      s    3 # = = :   0 0  " 2 $& 57p$C $< *%$ %$P&$s &$R 13KKL   
b ) )93x    ( (r'   