a
    bg                     @   s   d dl Z d dlmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZmZ e eZe	dddd	G d
d deeZdS )    N)AnyDictListMappingOptional)
deprecated)
Embeddings)	BaseModel
ConfigDictz0.3.1z1.0.0z!langchain_ollama.OllamaEmbeddings)ZsinceZremovalZalternative_importc                   @   s  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 ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zee ed< d
Zeee  ed< d
Zee ed< d
Zee ed< d
Zee ed< dZeed< d
Zee ed< ee ee!f dddZ"d
Z#ee ed< ee$ee!f ddd Z%e&d!d"d#Z'eee d$d%d&Z(ee 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 )/OllamaEmbeddingsa  Ollama locally runs large language models.

    To use, follow the instructions at https://ollama.ai/.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import OllamaEmbeddings
            ollama_emb = OllamaEmbeddings(
                model="llama:7b",
            )
            r1 = ollama_emb.embed_documents(
                [
                    "Alpha is the first letter of Greek alphabet",
                    "Beta is the second letter of Greek alphabet",
                ]
            )
            r2 = ollama_emb.embed_query(
                "What is the second letter of Greek alphabet"
            )

    zhttp://localhost:11434base_urlZllama2modelz	passage: embed_instructionzquery: query_instructionNmirostatmirostat_etamirostat_taunum_ctxnum_gpu
num_threadrepeat_last_nrepeat_penaltytemperaturestoptfs_ztop_ktop_pFshow_progressheaders)returnc                 C   sB   | j | j| j| j| j| j| j| j| j| j	| j
| j| j| jddS )z.Get the default parameters for calling Ollama.)r   r   r   r   r   r   r   r   r   r   r   r   r   )r   options)r   r   r   r   r   r   r   r   r   r   r   r   r   r   self r#   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/ollama.py_default_paramsx   s     z OllamaEmbeddings._default_paramsmodel_kwargsc                 C   s   i d| j i| jS )zGet the identifying parameters.r   )r   r%   r!   r#   r#   r$   _identifying_params   s    z$OllamaEmbeddings._identifying_paramsZforbidr#   )extraZprotected_namespaces)inputr   c              
   C   s   ddi| j pi }z*tj| j d|| j|d| jd}W n6 tjjyr } ztd| W Y d}~n
d}~0 0 |j	dkrtd	|j	|j
f z| }|d
 W S  tjjy } z"td| d|j
 W Y d}~n
d}~0 0 dS )zProcess a response from the API.

        Args:
            response: The response from the API.

        Returns:
            The response as a dictionary.
        zContent-Typezapplication/jsonz/api/embeddings)r   prompt)r   jsonz$Error raised by inference endpoint: N   z/Error raised by inference API HTTP code: %s, %s	embeddingzError raised by inference API: z.
Response: )r   requestspostr   r   r%   
exceptionsRequestException
ValueErrorstatus_codetextr+   JSONDecodeError)r"   r)   r   resetr#   r#   r$   _process_emb_response   s2    


$


z&OllamaEmbeddings._process_emb_responsec                    s\    j rFzddlm} ||dd}W qJ tyB   td |}Y qJ0 n|} fdd|D S )Nr   )tqdmr   )desczgUnable to show progress bar because tqdm could not be imported. Please install with `pip install tqdm`.c                    s   g | ]}  |qS r#   )r9   ).0r*   r!   r#   r$   
<listcomp>       z+OllamaEmbeddings._embed.<locals>.<listcomp>)r   r:   ImportErrorloggerwarning)r"   r)   r:   Ziter_r#   r!   r$   _embed   s    zOllamaEmbeddings._embed)textsr   c                    s     fdd|D }  |}|S )zEmbed documents using an Ollama deployed embedding model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                    s   g | ]} j  | qS r#   )r   )r<   r4   r!   r#   r$   r=      r>   z4OllamaEmbeddings.embed_documents.<locals>.<listcomp>)rB   )r"   rC   Zinstruction_pairsZ
embeddingsr#   r!   r$   embed_documents   s    	
z OllamaEmbeddings.embed_documents)r4   r   c                 C   s"   | j  | }| |gd }|S )zEmbed a query using a Ollama deployed embedding model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   rB   )r"   r4   Zinstruction_pairr-   r#   r#   r$   embed_query   s    	zOllamaEmbeddings.embed_query),__name__
__module____qualname____doc__r   str__annotations__r   r   r   r   r   intr   floatr   r   r   r   r   r   r   r   r   r   r   r   r   boolr   dictpropertyr   r   r%   r&   r   r'   r
   Zmodel_configr9   rB   rD   rE   r#   r#   r#   r$   r      s<   
$r   )loggingtypingr   r   r   r   r   r.   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zpydanticr	   r
   	getLoggerrF   r@   r   r#   r#   r#   r$   <module>   s   
