
    Ng                     |    d dl Z d dlmZmZ d dlZd dlmZ d dlmZ  e j	        e
          Z G d dee          ZdS )    N)ListOptional)
Embeddings)	BaseModelc                       e Zd ZU dZdZeed<   	 dZee	         ed<   	 dede
e         fdZd	e
e         de
e
e                  fd
Zdede
e         fdZdS )LlamafileEmbeddingsa  Llamafile lets you distribute and run large language models with a
    single file.

    To get started, see: https://github.com/Mozilla-Ocho/llamafile

    To use this class, you will need to first:

    1. Download a llamafile.
    2. Make the downloaded file executable: `chmod +x path/to/model.llamafile`
    3. Start the llamafile in server mode with embeddings enabled:

        `./path/to/model.llamafile --server --nobrowser --embedding`

    Example:
        .. code-block:: python

            from langchain_community.embeddings import LlamafileEmbeddings
            embedder = LlamafileEmbeddings()
            doc_embeddings = embedder.embed_documents(
                [
                    "Alpha is the first letter of the Greek alphabet",
                    "Beta is the second letter of the Greek alphabet",
                ]
            )
            query_embedding = embedder.embed_query(
                "What is the second letter of the Greek alphabet"
            )

    zhttp://localhost:8080base_urlNrequest_timeouttextreturnc                    	 t          j        | j         dddid|i| j                  }n@# t           j        j        $ r) t           j                            d| j         d          w xY w|                                 |                                }d|vrt          d	          |d         }t          |          d
k    rt          d          |S )Nz
/embeddingzContent-Typezapplication/jsoncontent)urlheadersjsontimeoutzTCould not connect to Llamafile server. Please make sure that a server is running at .	embeddingzPUnexpected output from /embedding endpoint, output dict missing 'embedding' key.g        z^Embedding sums to 0, did you start the llamafile server with the `--embedding` option enabled?)requestspostr	   r
   
exceptionsConnectionErrorraise_for_statusr   KeyErrorsum
ValueError)selfr   responsecontentsr   s        d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/llamafile.py_embedzLlamafileEmbeddings._embed0   s(   	}}000"$6 t ,	 	 	HH "2 	 	 	%55@/3}@ @ @  	 	!!###==??h&&+  
 [)	 y>>S  4  
 s	   ), =A)textsc                 d    g }|D ]*}|                     |                     |                     +|S )a6  Embed documents using a llamafile server running at `self.base_url`.
        llamafile server should be started in a separate process before invoking
        this method.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        )appendr!   )r   r"   doc_embeddingsr   s       r    embed_documentsz#LlamafileEmbeddings.embed_documents\   sA      	5 	5D!!$++d"3"34444    c                 ,    |                      |          S )a  Embed a query using a llamafile server running at `self.base_url`.
        llamafile server should be started in a separate process before invoking
        this method.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        )r!   )r   r   s     r    embed_queryzLlamafileEmbeddings.embed_queryl   s     {{4   r'   )__name__
__module____qualname____doc__r	   str__annotations__r
   r   intr   floatr!   r&   r)    r'   r    r   r      s          < ,Hc+++;%)OXc])))%*3 *4; * * * *XT#Y 4U3D     ! !U ! ! ! ! ! !r'   r   )loggingtypingr   r   r   langchain_core.embeddingsr   pydanticr   	getLoggerr*   loggerr   r2   r'   r    <module>r9      s     ! ! ! ! ! ! ! !  0 0 0 0 0 0      		8	$	$l! l! l! l! l!)Z l! l! l! l! l!r'   