Ë
    kQêjÙ  ã                   óx   — d dl mZ d dlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ  e	d	«       G d
„ de«      «       Zy)é    )ÚListNé   )Úattempt_import_or_raiseé   )ÚTextEmbeddingFunction)Úregister)ÚTEXTÚweak_lruÚ
instructorc                   ó"  — e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
eed	<   dZeed
<   dZeed<   dZeed<   dZeed<    ed¬«      d„ «       Zdedeej(                     fd„Zdedeej(                     fd„Zdedefd„Z ed¬«      d„ «       Zy)ÚInstructorEmbeddingFunctiona<  
    An embedding function that uses the InstructorEmbedding library. Instructor models
    support multi-task learning, and can be used for a variety of tasks, including
    text classification, sentence similarity, and document retrieval. If you want to
    calculate customized embeddings for specific sentences, you may follow the unified
    template to write instructions:
        "Represent the `domain` `text_type` for `task_objective`":

        * domain is optional, and it specifies the domain of the text, e.g., science,
          finance, medicine, etc.
        * text_type is required, and it specifies the encoding unit, e.g., sentence,
          document, paragraph, etc.
        * task_objective is optional, and it specifies the objective of embedding,
          e.g., retrieve a document, classify the sentence, etc.

    For example, if you want to calculate embeddings for a document, you may write the
    instruction as follows:
        "Represent the document for retrieval"

    Parameters
    ----------
    name : str
        The name of the model to use. Available models are listed at
        https://github.com/xlang-ai/instructor-embedding#model-list;
        The default model is hkunlp/instructor-base
    batch_size : int, default 32
        The batch size to use when generating embeddings
    device : str, default "cpu"
        The device to use when generating embeddings
    show_progress_bar : bool, default True
        Whether to show a progress bar when generating embeddings
    normalize_embeddings : bool, default True
        Whether to normalize the embeddings
    quantize : bool, default False
        Whether to quantize the model
    source_instruction : str, default "represent the document for retrieval"
        The instruction for the source column
    query_instruction : str, default "represent the document for retrieving the most
        similar documents"
        The instruction for the query

    Examples
    --------

    import lancedb
    from lancedb.pydantic import LanceModel, Vector
    from lancedb.embeddings import get_registry, InstructorEmbeddingFunction

    instructor = get_registry().get("instructor").create(
        source_instruction="represent the document for retrieval",
        query_instruction="represent the document for retrieving the most "
                          "similar documents"
    )

    class Schema(LanceModel):
        vector: Vector(instructor.ndims()) = instructor.VectorField()
        text: str = instructor.SourceField()

    db = lancedb.connect("~/.lancedb")
    tbl = db.create_table("test", schema=Schema, mode="overwrite")

    texts = [{"text": "Capitalism has been dominant in the Western world since the "
                      "end of feudalism, but most feel[who?] that..."},
            {"text": "The disparate impact theory is especially controversial under "
                     "the Fair Housing Act because the Act..."},
            {"text": "Disparate impact in United States labor law refers to practices "
                     "in employment, housing, and other areas that.."}]

    tbl.add(texts)

    zhkunlp/instructor-baseÚnameé    Ú
batch_sizeÚcpuÚdeviceTÚshow_progress_barÚnormalize_embeddingsFÚquantizez$represent the document for retrievalÚsource_instructionz@represent the document for retrieving the most similar documentsÚquery_instructionr   )Úmaxsizec                 óV   — t        | j                  | j                  dgg«      d   «      S )NÚfoor   )ÚlenÚgenerate_embeddingsr   )Úselfs    úb/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/embeddings/instructor.pyÚndimsz!InstructorEmbeddingFunction.ndimsf   s,   € ä�4×+Ñ+¨d×.EÑ.EÀuÐ-MÐ,NÓOÐPQÑRÓSÐSó    ÚqueryÚreturnc                 ó>   — | j                  | j                  |gg«      S ©N)r   r   )r   r!   ÚargsÚkwargss       r   Úcompute_query_embeddingsz4InstructorEmbeddingFunction.compute_query_embeddingsj   s!   € Ø×'Ñ'¨$×*@Ñ*@À%Ð)HÐ(IÓJÐJr    Útextsc                 ó†   — | j                  |«      }|D �cg c]  }| j                  |g‘Œ }}| j                  |«      S c c}w r$   )Úsanitize_inputr   r   )r   r(   r%   r&   ÚtextÚtexts_formatteds         r   Úcompute_source_embeddingsz5InstructorEmbeddingFunction.compute_source_embeddingsm   sG   € Ø×#Ñ# EÓ*ˆØGLÖM¸t˜D×3Ñ3°TÒ:ÐMˆÐMØ×'Ñ'¨Ó8Ð8ùò Ns   –>c                 ó¾   — | j                  «       }|j                  || j                  | j                  | j                  | j
                  ¬«      j                  «       }|S )N)r   r   r   r   )Ú	get_modelÚencoder   r   r   r   Útolist)r   r(   ÚmodelÚress       r   r   z/InstructorEmbeddingFunction.generate_embeddingsr   sW   € Ø—‘Ó ˆØ�l‰lØØ—‘Ø"×4Ñ4Ø!%×!:Ñ!:Ø—;‘;ð ó 
÷ ‰&‹(ð 	ð ˆ
r    c                 óx  — t        dd«      }t        dd«      }|j                  | j                  «      }| j                  rzd|j                  j
                  j                  v rd|j                  j
                  _        |j                  j                  ||j                  j                  h|j                  ¬«      }|S )NÚInstructorEmbeddingÚtorchÚqnnpack)Údtype)r   Ú
INSTRUCTORr   r   ÚbackendsÚ	quantizedÚsupported_enginesÚengineÚquantizationÚquantize_dynamicÚnnÚLinearÚqint8)r   Úinstructor_embeddingr6   r2   s       r   r/   z%InstructorEmbeddingFunction.get_model}   sž   € ä6Ø!Ð#8ó 
Ðô (¨°Ó9ˆà$×/Ñ/°·	±	Ó:ˆØ�=Š=à˜UŸ^™^×5Ñ5×GÑGÑGà2;�—‘×(Ñ(Ô/Ø×&Ñ&×7Ñ7Ø˜Ÿ™Ÿ™Ð(°·±ð 8ó ˆEð ˆr    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__r   Úintr   r   Úboolr   r   r   r   r
   r   r   ÚnpÚarrayr'   r	   r-   r   r/   © r    r   r   r      sæ   … ñFðP )€Dˆ#Ó(Ø€J�ÓØ€FˆCÓØ"Ð�tÓ"Ø!%Ð˜$Ó%Ø€HˆdÓð EÐ˜ÓDàJð �só ñ �aÔñTó ðTðK¨cð KÀtÈBÏHÉHÁ~ó Kð9¨tð 9ÈÈbÏhÉhÉó 9ð
	¨ð 	°$ó 	ñ �aÔñó ñr    r   )Útypingr   ÚnumpyrL   Úutilr   Úbaser   Úregistryr   Úutilsr	   r
   r   rN   r    r   ú<module>rU      s=   ðõ
 ã å *Ý 'Ý ß !ñ 
ˆ,Óô}Ð"7ó }ó ñ}r    