
    dh9                         S SK Jr  S SKJrJrJrJrJrJr  S SK	J
r
  S SKJrJr   " S S\5      r " S S\5      r " S	 S
\\5      r " S S\5      r " S S\
5      rg)    )Enum)AnyDictListOptionalSetUnion)
Embeddings)	BaseModel
ConfigDictc                       \ rS rSrSrSrg)TakeoffEmbeddingException   z>Custom exception for interfacing with Takeoff Embedding class. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       d/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/titan_takeoff.pyr   r      s    Hr   r   c                       \ rS rSrSrSrg)MissingConsumerGroup   zoException raised when no consumer group is provided on initialization of
TitanTakeoffEmbed or in embed request.r   Nr   r   r   r   r   r      s    .r   r   c                        \ rS rSrSrSrSrSrg)Device   z)Device to use for inference, cuda or cpu.cudacpur   N)r   r   r   r   r   r    r!   r   r   r   r   r   r      s    3D
Cr   r   c                   d    \ rS rSr% Sr\" SS9r\\S'    \	R                  r\	\S'    Sr\\S'   Srg	)
ReaderConfig   z7Configuration for the reader to be deployed in Takeoff.r   )protected_namespaces
model_namedeviceprimaryconsumer_groupN)r   r   r   r   r   r   model_configstr__annotations__r   r    r'   r)   r   r   r   r   r#   r#      s:    AL O&[[FF 6#NC#5r   r#   c            
       F  ^  \ rS rSr% SrSr\\S'    Sr\	\S'    Sr
\	\S'    S	r\\S
'    \" 5       r\\   \S'    SSS/ 4S\S\	S\	S\\   4U 4S jjjrS\\\   \4   S\\   S\\\4   4S jr SS\\   S\\   S\\\      4S jjr SS\S\\   S\\   4S jjrSrU =r$ )TitanTakeoffEmbed)   a:  Interface with Takeoff Inference API for embedding models.

Use it to send embedding requests and to deploy embedding
readers with Takeoff.

Examples:
    This is an example how to deploy an embedding model and send requests.

    .. code-block:: python
        # Import the TitanTakeoffEmbed class from community package
        import time
        from langchain_community.embeddings import TitanTakeoffEmbed

        # Specify the embedding reader you'd like to deploy
        reader_1 = {
            "model_name": "avsolatorio/GIST-large-Embedding-v0",
            "device": "cpu",
            "consumer_group": "embed"
        }

        # For every reader you pass into models arg Takeoff will spin up a reader
        # according to the specs you provide. If you don't specify the arg no models
        # are spun up and it assumes you have already done this separately.
        embed = TitanTakeoffEmbed(models=[reader_1])

        # Wait for the reader to be deployed, time needed depends on the model size
        # and your internet speed
        time.sleep(60)

        # Returns the embedded query, ie a List[float], sent to `embed` consumer
        # group where we just spun up the embedding reader
        print(embed.embed_query(
            "Where can I see football?", consumer_group="embed"
        ))

        # Returns a List of embeddings, ie a List[List[float]], sent to `embed`
        # consumer group where we just spun up the embedding reader
        print(embed.embed_document(
            ["Document1", "Document2"],
            consumer_group="embed"
        ))
zhttp://localhostbase_urli  porti  	mgmt_portNclientembed_consumer_groupsmodelsc                   > Xl         X l        X0l         SSKJn  U" U R                   U R                  U R                  S9U l        U H  nU R                  R                  U5        [        U[        5      (       a,  U R                  R                  UR                  S5      5        M_  U R                  R                  UR                  5        M     [        [        U ]C  5         g! [
         a    [        S5      ef = f)ax  Initialize the Titan Takeoff embedding wrapper.

Args:
    base_url (str, optional): The base url where Takeoff Inference Server is
    listening. Defaults to "http://localhost".
    port (int, optional): What port is Takeoff Inference API listening on.
    Defaults to 3000.
    mgmt_port (int, optional): What port is Takeoff Management API listening on.
    Defaults to 3001.
    models (List[ReaderConfig], optional): Any readers you'd like to spin up on.
    Defaults to [].

Raises:
    ImportError: If you haven't installed takeoff-client, you will get an
    ImportError. To remedy run `pip install 'takeoff-client==0.4.0'`
r   )TakeoffClientzjtakeoff-client is required for TitanTakeoff. Please install it with `pip install 'takeoff-client==0.4.0'`.)r1   r2   r)   N)r0   r1   r2   takeoff_clientr7   ImportErrorr3   create_reader
isinstancedictr4   addgetr)   superr.   __init__)selfr0   r1   r2   r5   r7   model	__class__s          r   r@   TitanTakeoffEmbed.__init__d   s    . !	"	4 $MM		T^^
 EKK%%e,%&&**..uyy9I/JK**..u/C/CD  	/1  	P 	s   C# #C9inputr)   returnc                    U(       da  [        U R                  5      S:X  a  [        U R                  5      S   nO/[        U R                  5      S:  a  [        S5      e[        S5      eU R                  R                  X5      $ )a  Embed text.

Args:
    input (Union[List[str], str]): prompt/document or list of prompts/documents
    to embed
    consumer_group (Optional[str]): what consumer group to send the embedding
    request to. If not specified and there is only one
    consumer group specified during initialization, it will be used. If there
    are multiple consumer groups specified during initialization, you must
    specify which one to use.

Raises:
    MissingConsumerGroup: The consumer group can not be inferred from the
    initialization and must be specified with request.

Returns:
    Dict[str, Any]: Result of query, {"result": List[List[float]]} or
    {"result": List[float]}
   r   ziTakeoffEmbedding was initialized with multiple embedding readergroups, you must specify which one to use.zYou must specify what consumer group you want to send embeddingresponse to as TitanTakeoffEmbed was not initialized with an embedding reader.)lenr4   listr   r3   embed)rA   rE   r)   s      r   _embedTitanTakeoffEmbed._embed   s    , 4--.!3!%d&@&@!A!!DT//014*A 
 +( 
 {{  77r   textsc                 *    U R                  X5      S   $ )a  Embed documents.

Args:
    texts (List[str]): List of prompts/documents to embed
    consumer_group (Optional[str], optional): Consumer group to send request
    to containing embedding model. Defaults to None.

Returns:
    List[List[float]]: List of embeddings
resultrL   )rA   rN   r)   s      r   embed_documents!TitanTakeoffEmbed.embed_documents   s     {{51(;;r   textc                 *    U R                  X5      S   $ )zEmbed query.

Args:
    text (str): Prompt/document to embed
    consumer_group (Optional[str], optional): Consumer group to send request
    to containing embedding model. Defaults to None.

Returns:
    List[float]: Embedding
rP   rQ   )rA   rT   r)   s      r   embed_queryTitanTakeoffEmbed.embed_query   s     {{40::r   )r0   r3   r2   r1   )N)r   r   r   r   r   r0   r+   r,   r1   intr2   r3   r   setr4   r   r   r#   r@   r	   r   r   rL   floatrR   rV   r   __classcell__)rC   s   @r   r.   r.   )   s2   )V 'Hc&WD#EIsPFCE&)e3s8+G +%'*2*2 *2 	*2
 \"*2 *2X$849c>*$8<DSM$8	c3h$8N AE<#Y<08<	d5k	<  :>;;)1#;	e; ;r   r.   N)enumr   typingr   r   r   r   r   r	   langchain_core.embeddingsr
   pydanticr   r   	Exceptionr   r   r+   r   r#   r.   r   r   r   <module>ra      sW     8 8 0 *I	 I.4 .
S$ 69 6"i;
 i;r   