U
    h                     @   sd   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 e eZG dd dee	ZdS )    N)AnyDictListMappingOptional)
Embeddings)	BaseModelc                   @   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%G 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!   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/ollama.py_default_paramsr   s     z OllamaEmbeddings._default_paramsmodel_kwargsc                 C   s   d| j i| jS )zGet the identifying parameters.r   )r   r#   r   r!   r!   r"   _identifying_params   s    z$OllamaEmbeddings._identifying_paramsc                   @   s   e Zd ZdZdS )zOllamaEmbeddings.ConfigZforbidN)__name__
__module____qualname__extrar!   r!   r!   r"   Config   s   r*   )inputr   c              
   C   s   ddi| j pi }z*tj| j d|| j|d| jd}W n4 tjjk
rp } ztd| W 5 d}~X Y nX |j	dkrtd	|j	|j
f z| }|d
 W S  tjjk
r } ztd| d|j
 W 5 d}~X Y nX 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   s4    
 

 


z&OllamaEmbeddings._process_emb_responsec                    s^    j rHzddlm} ||dd}W qL tk
rD   td |}Y qLX 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!   )r;   ).0r,   r   r!   r"   
<listcomp>   s     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>   r6   r   r!   r"   r?      s     z4OllamaEmbeddings.embed_documents.<locals>.<listcomp>)rC   )r    rD   Zinstruction_pairsZ
embeddingsr!   r   r"   embed_documents   s    	
z OllamaEmbeddings.embed_documents)r6   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   rC   )r    r6   Zinstruction_pairr/   r!   r!   r"   embed_query   s    	zOllamaEmbeddings.embed_query)+r&   r'   r(   __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*   r;   rC   rE   rF   r!   r!   r!   r"   r	      s<   
$r	   )loggingtypingr   r   r   r   r   r0   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   	getLoggerr&   rA   r	   r!   r!   r!   r"   <module>   s   
