o
    Zh                     @   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de ee!f fddZ"d
Z#ee ed< 	 ede$ee!f fdd Z%e&d!d"d#Z'd$edee fd%d&Z(d$ee deee  fd'd(Z)d)ee deee  fd*d+Z*d,edee f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#   \/var/www/html/lang_env/lib/python3.10/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inputc              
   C   s   ddi| j pi }ztj| j d|| j|d| jd}W n tjjy3 } ztd| d}~ww |j	dkrCtd	|j	|j
f z	| }|d
 W S  tjjye } ztd| d|j
 d}~ww )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   s:   





z&OllamaEmbeddings._process_emb_responsec                    sZ    j r"zddlm} ||dd}W n ty!   td |}Y nw |} fdd|D S )Nr   )tqdmr   )Z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>   s    z+OllamaEmbeddings._embed.<locals>.<listcomp>)r   r:   ImportErrorloggerwarning)r"   r)   r:   Ziter_r#   r!   r$   _embed   s   zOllamaEmbeddings._embedtextsc                    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<      s    z4OllamaEmbeddings.embed_documents.<locals>.<listcomp>)r@   )r"   rA   Zinstruction_pairsZ
embeddingsr#   r!   r$   embed_documents   s   	
z OllamaEmbeddings.embed_documentsr4   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   r@   )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   r@   rB   rC   r#   r#   r#   r$   r      sf   
 $r   )loggingtypingr   r   r   r   r   r.   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zpydanticr	   r
   	getLoggerrD   r>   r   r#   r#   r#   r$   <module>   s    
