
    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  e         ej.                  d
d      Z ee      Z e	j4                  d       e G d d             ZdZ eeeed      ZddZej>                  dd       Z ej>                  dd       Z!ej>                  dd       Z"y)    )annotations)	dataclass)load_dotenv)AsyncOpenAIN)Agent
ModelRetry
RunContext)OpenAIModel)Client)List	LLM_MODELzgpt-4o-minizif-token-present)send_to_logfirec                  "    e Zd ZU ded<   ded<   y)PydanticAIDepsr   supabaser   openai_clientN)__name__
__module____qualname____annotations__     $/var/www/openai/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*   1   sz     &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	                  d|dddid      j                         }|j                  syg }|j                  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)a?  
    Retrieve relevant documentation chunks based on the query with RAG.
    
    Args:
        ctx: The context including the Supabase client and OpenAI client
        user_query: The user's question or query
        
    Returns:
        A formatted string containing the top 5 most relevant documentation chunks
    Nmatch_site_pages   sourcepydantic_ai_docs)query_embeddingmatch_countfilterz No relevant documentation found.z
# title

content
z

---

z Error retrieving documentation: )r*   depsr   r   rpcexecuter#   appendjoinr%   r&   str)ctx
user_queryr0   resultformatted_chunksdoc
chunk_textr)   s           r   retrieve_relevant_documentationrC   =   s
     ; -j#((:P:P QQ ""&&#2 #%78
 ') 	 {{5 ;;Cw<. Y  J
 ##J/  !!"2335 R8  ;0451#a&::;sR   C+#B9 B7A
B9 2C+3AB9 6C+7B9 9	C(C#C(C+#C((C+c                f  K   	 | j                   j                  j                  d      j                  d      j	                  dd      j                         }|j                  sg S t        t        d |j                  D                    }|S # t        $ r}t        d|        g cY d}~S d}~ww xY ww)z
    Retrieve a list of all available Pydantic AI documentation pages.
    
    Returns:
        List[str]: List of unique URLs for all documentation pages
    
site_pagesurlmetadata->>sourcer/   c              3  &   K   | ]	  }|d      yw)rF   Nr   ).0rA   s     r   	<genexpr>z+list_documentation_pages.<locals>.<genexpr>~   s     <#e*s   z&Error retrieving documentation pages: N)r7   r   from_selecteqr9   r#   sortedsetr%   r&   )r=   r?   urlsr)   s       r   list_documentation_pagesrQ   k   s     ""((6VE]R#%78WY 	
 {{I c<<<= 6qc:;	sA   B1AB #B1$&B 
B1	B.B)#B.$B1)B..B1c                6  K   	 | j                   j                  j                  d      j                  d      j	                  d|      j	                  dd      j                  d      j                         }|j                  sd| S |j                  d   d	   j                  d
      d   }d| dg}|j                  D ]  }|j                  |d           dj                  |      S # t        $ r&}t        d|        dt        |       cY d}~S d}~ww xY ww)a3  
    Retrieve the full content of a specific documentation page by combining all its chunks.
    
    Args:
        ctx: The context including the Supabase client
        url: The URL of the page to retrieve
        
    Returns:
        str: The complete page content with all chunks combined in order
    rE   ztitle, content, chunk_numberrF   rG   r/   chunk_numberzNo content found for URL: r   r3   z - z# r6   r5   r4   zError retrieving page content: N)r7   r   rK   rL   rM   orderr9   r#   splitr:   r;   r%   r&   r<   )r=   rF   r?   
page_titleformatted_contentchunkr)   s          r   get_page_contentrY      s    :""((6V23Rs^R#%78U>"WY 	 {{/u55 [[^G,2259!<
!*R01 [[E$$U9%56 ! {{,-- :/s340Q99:sB   DBC' DA C' &D'	D0DDDDD)r'   r<   r   r   returnzList[float])r=   RunContext[PydanticAIDeps]r>   r<   rZ   r<   )r=   r[   rZ   z	List[str])r=   r[   rF   r<   rZ   r<   )#
__future__r   _annotationsdataclassesr   dotenvr   litellmr   logfireasynciohttpxospydantic_air   r   r	   pydantic_ai.models.openair
   r   r   typingr   getenvllmr   	configurer   r   pydantic_ai_agentr*   toolrC   rQ   rY   r   r   r   <module>rm      s    2 !      	 5 5 1   bii]+C   "4 5
   		 
 +; +;Z  2 $: $:r   