
    中g                     x   d dl mZ d dlZd dlmZ ddlmZmZ ddlmZ d dl	m
Z
 d dlZd dlmZ  e         e
 ej                  d	      
      Z e       ZdefdZ G d de      Z e       Zej+                  d      d        Zej/                  d      defd       Zedk(  r ej4                  eddd       yy)    )FastAPIN)	BaseModel   )pydantic_ai_agentPydanticAIDeps)init_collection)AsyncOpenAI)load_dotenvOPENAI_API_KEY)api_key
user_inputc                   K   t        t        t              }t        j                  | |g       4 d{   }d}|j                  d      2 3 d{   }||z  }7 '7 6 |cddd      d{  7   S # 1 d{  7  sw Y   yxY ww)z<Run the agent with streaming text for the user_input prompt.)
collectionopenai_client)depsmessage_historyN T)delta)r   chroma_collectionr   r   
run_streamstream_text)r   r   resultpartial_textchunks        $/var/www/openai/gen-rag-crawl/api.pyrun_agent_with_streamingr      s     %6mTD ++  
!--D-9 	"%E!L	"9    sh   2B
AB
A5A AA A5B
A  A5"B
.A1/B
5B;A><BB
c                       e Zd ZU eed<   y)QuestionRequestquestionN)__name__
__module____qualname__str__annotations__     r   r   r   %   s    Mr&   r   /c                  
    ddiS )NmessagezAPI is running!r%   r%   r&   r   	read_rootr*   *   s    ())r&   z/askrequestc                 f   K   t        | j                         d {   }| j                  |dS 7 w)N)r   answer)r   r   )r+   responses     r   ask_questionr/   /   s0     -g.>.>??H((H== @s   1/1__main__z0.0.0.0i9!  info)hostport	log_level)fastapir   uvicornpydanticr   r   r   dbr   openair	   osdotenvr
   getenvr   r   r#   r   r   apigetr*   postr/   r    runr%   r&   r   <module>rA      s      
 A   	  IBII.>$?@#% s $i  i* * &> > > zGKK)$&A r&   