a
    bg                  	   @   s@  d Z ddlmZmZ ddlmZmZmZmZmZ ddl	Z	ddl
mZ ddlmZmZmZmZ G dd dZz dd	lmZmZmZmZmZ W nz ey   ed
ef eeed
ef dddZeddddZeeddddZddedfeeeddddZY n0 e	je dddZ!G dd deZ"dZ#G dd  d eZ$dS )!z.Util that invokes the Passio Nutrition AI API.    )datetime	timedelta)AnyCallableDictOptionalfinalN)get_from_dict_or_env)	BaseModel
ConfigDictFieldmodel_validatorc                   @   s6   e Zd ZdZeddddZeeddddZdS )	NoDiskStoragez!Mixin to prevent storing on disk.Nreturnc                 C   s   t dd S NzDo not store on disk.AttributeErrorself r   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/passio_nutrition_ai.py__getstate__   s    zNoDiskStorage.__getstate__)stater   c                 C   s   t dd S r   r   )r   r   r   r   r   __setstate__   s    zNoDiskStorage.__setstate__)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   r   )retryretry_if_resultstop_after_attemptwait_exponentialwait_random.)fargskwargsr   c                 O   s   | S Nr   )r$   r%   r&   r   r   r   retry_fallback!   s    r(   )nr   c                 C   s   d S r'   r   )r)   r   r   r   stop_after_attempt_fallback&   s    r*   )abr   c                 C   s   d S r'   r   )r+   r,   r   r   r   wait_random_fallback)   s    r-      inf)
multiplierminmaxr   c                 C   s   d S r'   r   r0   r1   r2   r   r   r   wait_exponential_fallback,   s    r4   )rspr   c                 C   s   t | o| jdv S )z&Check if a HTTP response is retryable.)i  i  i  i  i  i  i  )boolstatus_code)r5   r   r   r   is_http_retryable2   s    r8   c                	   @   s   e Zd ZU dZee ed< edddZe	e
dddZedd	d
Zeeeededdedddd deejdddZddddZdS )ManagedPassioLifeAuthz)Manage the token for the NutritionAI API._access_token_expiry)subscription_keyc                 C   s"   || _ d | _d | _d | _d | _d S r'   )r;   _last_tokenr:   _access_token_customer_idr   r;   r   r   r   __init__<   s
    zManagedPassioLifeAuth.__init__r   c                 C   s$   |   s|   d| j | jdS )NzBearer )Authorizationz	Passio-ID)is_valid_nowrefresh_access_tokenr=   r>   r   r   r   r   headersC   s
    
zManagedPassioLifeAuth.headersc                 C   s,   | j d uo*| jd uo*| jd uo*| jt kS r'   )r=   r>   r:   r   nowr   r   r   r   rB   L   s    
z"ManagedPassioLifeAuth.is_valid_now   r   333333?r.   皙?   r3   r   stopwait)r;   r   c                 C   s   t d| S )Nz;https://api.passiolife.com/v2/token-cache/napi/oauth/token/)requestsgetr?   r   r   r   	_http_getT   s    zManagedPassioLifeAuth._http_getNc                 C   s`   |  | j}|std|  | _}|d | _|d | _t t	|d d t	dd | _
dS )z1Refresh the access token for the NutritionAI API.zCould not get access tokenZcustomer_idZaccess_tokenZ
expires_in)seconds   N)rO   r;   
ValueErrorjsonr<   r>   r=   r   rE   r   r:   )r   r5   tokenr   r   r   rC   ^   s    

z*ManagedPassioLifeAuth.refresh_access_token)r   r   r   r   r   r   __annotations__strr@   propertydictrD   r6   rB   r   r    r8   r!   r#   r"   rM   ResponserO   rC   r   r   r   r   r9   7   s   
r9   z@https://api.passiolife.com/v2/products/napi/food/search/advancedc                	   @   s   e Zd ZU dZeed< eedZeed< ee	dZ
e	ed< eed< edd	d
Zeeeededdedddd de	ejdddZee	dddZeddeeedddZeee ddd Zd!S )"NutritionAIAPIz(Wrapper for the Passio Nutrition AI API.nutritionai_subscription_key)defaultnutritionai_api_url)default_factorymore_kwargsauth_TZforbid)Zarbitrary_types_allowedextrarF   r   rG   r.   rH   rI   r3   rJ   )paramsr   c                 C   s   t j| j| jj|dS )N)rD   rb   )rM   rN   r]   r`   rD   )r   rb   r   r   r   rO      s
    zNutritionAIAPI._http_get)search_termr   c                 C   s0   |  d|i| j}|s td|  | S )z0Call the NutritionAI API and return the results.termz%Could not get NutritionAI API results)rO   r_   rR   raise_for_statusrS   )r   rc   r5   r   r   r   _api_call_results   s
    z NutritionAIAPI._api_call_resultsbefore)mode)valuesr   c                 C   s:   t |dd}||d< t |ddt}||d< t||d< |S )z9Validate that api key and endpoint exists in environment.r[   ZNUTRITIONAI_SUBSCRIPTION_KEYr]   ZNUTRITIONAI_API_URLr`   )r	   DEFAULT_NUTRITIONAI_API_URLr9   )clsri   r[   r]   r   r   r   validate_environment   s    z#NutritionAIAPI.validate_environment)queryr   c                 C   s"   |  |}|rt|dk rdS |S )z4Run query through NutrtitionAI API and parse result.r.   N)rf   len)r   rm   resultsr   r   r   run   s    
zNutritionAIAPI.runN)r   r   r   r   rV   rU   r   rj   r]   rX   r_   r9   r   Zmodel_configr   r    r8   r!   r#   r"   rM   rY   rO   rf   r   classmethodr   r   rl   r   rp   r   r   r   r   rZ   s   s(   
rZ   )%r   r   r   typingr   r   r   r   r   rM   Zlangchain_core.utilsr	   Zpydanticr
   r   r   r   r   tenacityr   r    r!   r"   r#   ImportErrorr(   intr*   floatr-   r4   rY   r6   r8   r9   rj   rZ   r   r   r   r   <module>   s0    

8