
    g                       d dl mZ d dlmZ d dlmZ d dlmZ d dl	Z	d dl
Z
d dlZd dlZd dlmZmZmZ d dlmZ d dlmZ d d	lmZ d dlZd d
lmZmZ  e         ej6                  dd      Z ee      Z e	j<                  d       e G d d             Z e       Z dZ! eee!ed      Z"ddZ#e"jH                  	 	 	 	 	 	 dd       Z%e"jH                  dd       Z&e"jH                  dd       Z'y)    )annotations)	dataclass)load_dotenv)AsyncOpenAIN)Agent
ModelRetry
RunContext)OpenAIModel)Client)List)get_chroma_clientinit_collection	LLM_MODELzgpt-4o-minizif-token-present)send_to_logfirec                  "    e Zd ZU ded<   ded<   y)PydanticAIDepszchromadb.Collection
collectionr   openai_clientN)__name__
__module____qualname____annotations__     ./var/www/openai/rag-crawl/pydantic_ai_agent.pyr   r      s    ##r   r   a*  
You are an expert at Pydantic AI - a Python AI agent framework that you have access to all the documentation to,
including examples, an API reference, and other resources to help you build Pydantic AI agents.

Your only job is to assist with this and you don't answer other questions besides describing what you are able to do.

Don't ask the user before taking an action, just do it. Always make sure you look at the documentation with the provided tools before answering the user's question unless you have already.

When you first look at the documentation, always start with RAG.
Then also always check the list of available documentation pages and retrieve the content of page(s) if it'll help.

Always let the user know when you didn't find the answer in the documentation or the right URL - be honest.
   )system_prompt	deps_typeretriesc                   K   	 |j                   j                  d|        d{   }|j                  d   j                  S 7 # t        $ r}t        d|        dgdz  cY d}~S d}~ww xY ww)z!Get embedding vector from OpenAI.ztext-embedding-3-small)modelinputNr   zError getting embedding: i   )
embeddingscreatedata	embedding	Exceptionprint)textr   responsees       r   get_embeddingr,   8   sy     &1188*$ 9 
 
 }}Q)))
  )!-.sTzsD   A. A AA A.A 	A+A& A+!A.&A++A.c                  K   	 t        || j                  j                         d{   }| j                  j                  j	                  |gdddg      }|d   d   syg }t        |d   d   |d   d         D ]"  \  }}d|d	    d
| d}|j                  |       $ dj                  |      S 7 # t        $ r&}t        d|        dt        |       cY d}~S d}~ww xY ww)zM
    Retrieve relevant documentation chunks based on the query with RAG.
    N   	documents	metadatas)query_embeddings	n_resultsincluder   z No relevant documentation found.z
# title


z

---

z Error retrieving documentation: )r,   depsr   r   queryzipappendjoinr'   r(   str)	ctx
user_queryquery_embeddingresultsformatted_chunksdocmetadata
chunk_textr+   s	            r   retrieve_relevant_documentationrE   D   s    ; -j#((:P:P QQ ((%%++-. +. , 
 {#A&5  !5a!8'+:Nq:QRMCG  J
 ##J/ S !!"233- R0  ;0451#a&::;sQ   C$#B2 B07B2 C$ AB2 /C$0B2 2	C!;CC!C$C!!C$c                   K   	 | j                   j                  j                  dg      }|d   sg S t        t	        d |d   D                    }|S # t
        $ r}t        d|        g cY d}~S d}~ww xY ww)zK
    Retrieve a list of all available Pydantic AI documentation pages.
    r0   )r3   c              3  &   K   | ]	  }|d      yw)urlNr   ).0metas     r   	<genexpr>z+list_documentation_pages.<locals>.<genexpr>w   s     G2F$$u+2Fs   z&Error retrieving documentation pages: N)r7   r   getsortedsetr'   r(   )r=   r@   urlsr+   s       r   list_documentation_pagesrP   j   s|     
((%%));-)@{#I cG'+2FGGH 6qc:;	s>   A8-A A8A A8	A5A0*A5+A80A55A8c                  K   	 | j                   j                  j                  d|iddg      }|d   sd| S t        t	        |d   |d         d       }|d   d	   d
   j                  d      d   }d| dg}|D ]  \  }}|j                  |        dj                  |      S # t        $ r&}t        d|        dt        |       cY d}~S d}~ww xY ww)zE
    Retrieve the full content of a specific documentation page.
    rH   r/   r0   )wherer3   zNo content found for URL: c                    | d   d   S )N   chunk_numberr   )xs    r   <lambda>z"get_page_content.<locals>.<lambda>   s    !A$~.r   )keyr   rT   r4   z - z# r6   r5   zError retrieving page content: N)r7   r   rL   rM   r9   splitr:   r;   r'   r(   r<   )	r=   rH   r@   sorted_results
page_titleformatted_contentrB   _r+   s	            r   get_page_contentr^      s
    
:((%%))#,k(B * 
 {#/u55  $gk&:;.
 $A&q)'288?B
!*R01 %FC$$S) % {{,-- :/s340Q99:s?   C4B' CA-B' &C'	C0CCCCC)r)   r<   r   r   returnzList[float])r=   RunContext[PydanticAIDeps]r>   r<   r_   r<   )r=   r`   r_   z	List[str])r=   r`   rH   r<   r_   r<   )(
__future__r   _annotationsdataclassesr   dotenvr   litellmr   logfireasynciohttpxospydantic_air   r   r	   pydantic_ai.models.openair
   supabaser   typingr   chromadbdbr   r   getenvllmr!   	configurer   chroma_collectionr   pydantic_ai_agentr,   toolrE   rP   r^   r   r   r   <module>rv      s   2 !      	 5 5 1    1 bii]+C   "4 5    $%  	.! 
	 ";	#";14";"; ";J  ( : :r   