o
    +if.                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ ejejdd eeZeejZde	eeeee	e f f  d	dfd
dZG dd dZdddZdddZedkrye  dS dS )    N)Path)DictListLiteralMappingOptionalUnioncastenv)utilsz%(message)s)levelformatservices_statusreturnc                 C   s  g }| D ]-}t |d t |d d}ttt |dg }|r,ddd |D |d< || qtd	d
 |D }tdd
 |D }dd|d  d|d  d g}|D ]#}|d |d }|d |d }	|dd}
|||	 |
  qY|d t	
d| d S )NServiceStatus)r   r   
Publishersz, c                 S   s   g | ]}t |d  qS )PublishedPort)str).0	publisher r   K/var/www/html/corbot_env/lib/python3.10/site-packages/langsmith/cli/main.py
<listcomp>   s    z#pprint_services.<locals>.<listcomp>PublishedPortsc                 s       | ]	}t |d  V  qdS )r   Nlenr   servicer   r   r   	<genexpr>"       z"pprint_services.<locals>.<genexpr>c                 s   r   )r   Nr   r   r   r   r   r!   #   r"   
   zPublished Ports z
To connect, set the following environment variables in your LangChain application:
LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT=http://localhost:80/api)r   r	   r   r   getjoinappendmaxljustloggerinfo)r   servicesr    service_status
publishersmax_service_lenmax_state_lenservice_messageservice_str	state_str	ports_strr   r   r   pprint_services   s<   

r6   c                   @   s   e Zd ZdZd#ddZedee fddZdeddfd	d
Z	d#ddZ
ddddeed ed ed f deddfddZdddddeed ed ed f dee dededdf
ddZd$deddfddZd#dd Zd#d!d"ZdS )%LangSmithCommandz$Manage the LangSmith Tracing server.r   Nc                 C   s   t t jd | _d S )Nzdocker-compose.yaml)r   __file__absoluteparentdocker_compose_fileselfr   r   r   __init__<   s   zLangSmithCommand.__init__c                 C   s   t  S N)ls_utilsget_docker_compose_commandr<   r   r   r   docker_compose_commandA   s   z'LangSmithCommand.docker_compose_commandurlc                 C   s*   z
t d|g W d S  ty   Y d S w )Nopen)
subprocessrunFileNotFoundError)r=   rC   r   r   r   _open_browserE   s
   zLangSmithCommand._open_browserc                 C   s^   g | j dt| j}tg |ddd td td td | d d S )	N-fupz--quiet-pullz--waita  LangSmith server is running at http://localhost:80/api.
To view the app, navigate your browser to http://localhost:80

To connect your LangChain application to the server locally,
set the following environment variable when running your LangChain application.
z	LANGCHAIN_TRACING_V2=truez,	LANGCHAIN_ENDPOINT=http://localhost:80/api
zhttp://localhost)rB   r   r;   rE   rF   r+   r,   rH   )r=   commandr   r   r   _start_localK   s0   

zLangSmithCommand._start_localprodlateststageversionrP   devbetarQ   c                C   sV   |dkr
dt jd< n	|dkrdt jd< |t jd< tg | jdt| jd d	S )
zPull the latest LangSmith images.

        Args:
            stage: Which stage of LangSmith images to pull.
                One of "prod", "dev", or "beta".
        rR   zdev-_LANGSMITH_IMAGE_PREFIXrS   zrc-_LANGSMITH_IMAGE_VERSIONrI   pullN)osenvironrE   rF   rB   r   r;   )r=   rP   rQ   r   r   r   rV   e   s    

zLangSmithCommand.pull)rP   openai_api_keyrQ   rY   langsmith_license_keyc                C   s>   |dur	|t jd< |dur|t jd< | j||d |   dS )a  Run the LangSmith server locally.

        Args:
            expose: If True, expose the server to the internet using ngrok.
            auth_token: The ngrok authtoken to use (visible in the ngrok dashboard).
                If not provided, ngrok server session length will be restricted.
            stage: Which set of images to pull when running.
                One of "prod", "dev", or "beta".
            openai_api_key: The OpenAI API key to use for LangSmith
                If not provided, the OpenAI API Key will be read from the
                OPENAI_API_KEY environment variable. If neither are provided,
                some features of LangSmith will not be available.
            langsmith_license_key: The LangSmith license key to use for LangSmith
                If not provided, the LangSmith license key will be read from the
                LANGSMITH_LICENSE_KEY environment variable. If neither are provided,
                Langsmith will not start up.
        NOPENAI_API_KEYLANGSMITH_LICENSE_KEYrO   )rW   rX   rV   rL   )r=   rP   rY   rZ   rQ   r   r   r   start   s   

zLangSmithCommand.startFclear_volumesc                 C   sX   g | j dt| jd}|r%td}| dkr td dS |d t| dS )Stop the LangSmith server.rI   downzYou are about to delete all the locally cached LangSmith containers and volumes. This operation cannot be undone. Are you sure? [y/N]yz	Aborting.Nz	--volumes)	rB   r   r;   inputlowerprintr(   rE   rF   )r=   r^   cmdconfirmr   r   r   stop   s$   
zLangSmithCommand.stopc                 C   s&   t g | jdt| jd dS )z)Print the logs from the LangSmith server.rI   logsN)rE   rF   rB   r   r;   r<   r   r   r   rh      s   zLangSmithCommand.logsc                 C   s   g | j dt| jddd}tj|tjtjd}z|jd}t	|}W n tj
y9   td Y dS w |rGtd	 t| dS td
 dS )z6Provide information about the status LangSmith server.rI   psz--formatjson)stdoutstderrzutf-8z'Error checking LangSmith server status.Nz*The LangSmith server is currently running.z$The LangSmith server is not running.)rB   r   r;   rE   rF   PIPErk   decoderj   loadsJSONDecodeErrorr+   errorr,   r6   )r=   rK   resultcommand_stdoutr   r   r   r   status   s<   	


zLangSmithCommand.statusr   N)F)__name__
__module____qualname____doc__r>   propertyr   r   rB   rH   rL   r   r   rV   r   r]   boolrg   rh   rt   r   r   r   r   r7   9   sB    



 
r7   c                  C   sr   t  } | t   | t   tdd |  D }td | 	 D ]\}}t|| d|  q&dS )z*Print the runtime environment information.c                 s   s    | ]}t |V  qd S r?   r   )r   keyr   r   r   r!      s    zenv.<locals>.<genexpr>zLangChain Environment:z: N)
ls_envget_runtime_environmentupdateget_docker_environmentget_langchain_env_varsr)   keysr+   r,   items)r   max_key_lengthkvr   r   r   r      s   
r   c            	         s  t d t d t } | jdd}t  |jddd}|jddg d	d
d |jdtddd |jdtddd |jdddd |j	 fddd |jddd}|jdddd |j	 fddd |jd d!d}|jddg d	d"d |jdddd |j	 fd#dd |jd$d%d}|j	 fd&dd |jd'd(d}|j	 fd)dd |d*}|j	d+d d | 
 }t|d,s|   d-S || d-S ).zMain entrypoint for the CLI.z<BY USING THIS SOFTWARE YOU AGREE TO THE TERMS OF SERVICE AT:z0https://smith.langchain.com/terms-of-service.pdfzLangSmith CLI commands)descriptionr]   zStart the LangSmith server.z--stagerM   )rM   rR   rS   z)Which set of images to pull when running.)defaultchoiceshelpz--openai-api-keyr[   zThe OpenAI API key to use for LangSmith. If not provided, the OpenAI API Key will be read from the OPENAI_API_KEY environment variable. If neither are provided, some features of LangSmith will not be available.)r   r   z--langsmith-license-keyr\   zThe LangSmith license key to use for LangSmith. If not provided, the LangSmith License Key will be read from the LANGSMITH_LICENSE_KEY environment variable. If neither are provided, the Langsmith application will not spin up.z	--versionrN   zThe LangSmith version to use for LangSmith. Defaults to latest. We recommend pegging this to the latest static version available at https://hub.docker.com/repository/docker/langchain/langchainplus-backend if you are using Langsmith in production.c                    s    j | j| j| j| jdS )N)rP   rY   rZ   rQ   )r]   rP   rY   rZ   rQ   argsserver_commandr   r   <lambda>  s    zmain.<locals>.<lambda>)funcrg   r_   z--clear-volumes
store_truez?Delete all the locally cached LangSmith containers and volumes.)actionr   c                    s    j | jdS )N)r^   )rg   r^   r   r   r   r   r   (  s    rV   z!Pull the latest LangSmith images.z(Which stage of LangSmith images to pull.c                    s    j | j| jdS )NrO   )rV   rP   rQ   r   r   r   r   r   =  s    rh   zShow the LangSmith server logs.c                          S r?   )rh   r   r   r   r   r   B      rt   z!Show the LangSmith server status.c                    r   r?   )rt   r   r   r   r   r   F  r   r   c                 S   s   t  S r?   r
   r   r   r   r   r   H  s    r   N)rd   argparseArgumentParseradd_subparsersr7   
add_parseradd_argumentrW   getenvset_defaults
parse_argshasattr
print_helpr   )	parser
subparsersserver_start_parserserver_stop_parserserver_pull_parserserver_logs_parserserver_status_parser
env_parserr   r   r   r   main   s   
	



r   __main__ru   ) r   rj   loggingrW   rE   pathlibr   typingr   r   r   r   r   r   r	   	langsmithr   r}   r   r@   basicConfigINFO	getLoggerrv   r+   r8   r:   _DIRr   r6   r7   r   r   r   r   r   <module>   s(    $

*' 
&
e
