a
    bg                     @   sX   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 	e
ZG dd deeZdS )    N)ListOptional)
Embeddings)	BaseModelc                   @   sr   e Zd ZU dZdZeed< dZee	 ed< ee
e dddZe
e e
e
e  d	d
dZee
e dd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                 C   s   z(t j| j dddid|i| jd}W n, t jjyT   t jd| j dY n0 |  | }d|vrvtd	|d }t	|d
krt
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    r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/llamafile.py_embed0   s6    



zLlamafileEmbeddings._embed)textsr
   c                 C   s"   g }|D ]}| | | q|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    Zdoc_embeddingsr	   r   r   r   embed_documents\   s    z#LlamafileEmbeddings.embed_documentsc                 C   s
   |  |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	   r   r   r   embed_queryl   s    zLlamafileEmbeddings.embed_query)__name__
__module____qualname____doc__r   str__annotations__r   r   intr   floatr   r"   r#   r   r   r   r   r      s   
,r   )loggingtypingr   r   r   Zlangchain_core.embeddingsr   Zpydanticr   	getLoggerr$   loggerr   r   r   r   r   <module>   s   
