
    7|h                     x    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y)    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y)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                        }|j                          |j                         }d|vrt        d	      |d   }t        |      d
k(  rt        d      |S # t         j                  j
                  $ r. t         j                  j                  d| j                   d      w xY w)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        g/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/llamafile.py_embedzLlamafileEmbeddings._embed0   s    	}}}}oZ0"$6 t ,,	H" 	!!#==?h&+ 
 [)	 y>S 4 
 = ""22 	%%55//3}}oQ@ 	s   4B ACtextsc                 X    g }|D ]"  }|j                  | j                  |             $ |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\   s5      	5D!!$++d"34	5    c                 $    | j                  |      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      sv    < ,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      s6     !  0 			8	$l!)Z l!r'   