
    Ng                         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 j        e          Z e	ddd	           G 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)sinceremovalalternative_importc                   &   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            Z"d
Z#ee         ed<   	 ede$ee!f         fd            Z% e&dd !          Z'd"edee         fd#Z(d"ee         deee                  fd$Z)d%ee         deee                  fd&Z*d'edee         f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_url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                     | 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"   selfs    a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/ollama.py_default_paramsz OllamaEmbeddings._default_paramsx   sh     Z M $ 1 $ 1<<"o!%!3"&"5#/	 
 
 	
    model_kwargsc                 &    i d| j         i| j        S )zGet the identifying parameters.r   )r   r+   r(   s    r*   _identifying_paramsz$OllamaEmbeddings._identifying_params   s     A7DJ'@4+?@@r,   forbid )extraprotected_namespacesinputc                    ddi| j         pi }	 t          j        | j         d|| j        |d| j                  }n.# t          j        j        $ r}t          d|           d}~ww xY w|j	        dk    rt          d	|j	        d
|j
                  	 |                                }|d         S # t          j        j        $ r}t          d| d|j
                   d}~ww xY 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: z, 	embeddingzError raised by inference API: z.
Response: )r$   requestspostr   r   r+   
exceptionsRequestException
ValueErrorstatus_codetextr7   JSONDecodeError)r)   r4   r$   resets         r*   _process_emb_responsez&OllamaEmbeddings._process_emb_response   sE    .
|!r

	I-=111#zUSSd>RS  CC
 "3 	I 	I 	IGAGGHHH	I ?c!!*???CHH.  	

A[>!"2 	 	 	L!LL#(LL  	s-   .> A)A$$A)B3 3C&C!!C&c                       j         rA	 ddlm}  ||d          }n.# t          $ r t                              d           |}Y nw xY w|} fd|D             S )Nr   )tqdmr   )desczgUnable to show progress bar because tqdm could not be imported. Please install with `pip install tqdm`.c                 :    g | ]}                     |          S r1   )rE   ).0r6   r)   s     r*   
<listcomp>z+OllamaEmbeddings._embed.<locals>.<listcomp>   s'    GGGv**622GGGr,   )r#   rG   ImportErrorloggerwarning)r)   r4   rG   iter_s   `   r*   _embedzOllamaEmbeddings._embed   s     		%%%%%%U);<<<   >    EGGGGGGGGs    &AAtextsc                 N      f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                 &    g | ]}j          | S r1   )r   )rJ   r@   r)   s     r*   rK   z4OllamaEmbeddings.embed_documents.<locals>.<listcomp>   s'    QQQ4 6>>>QQQr,   )rP   )r)   rQ   instruction_pairs
embeddingss   `   r*   embed_documentsz OllamaEmbeddings.embed_documents   s8     RQQQ5QQQ[[!233
r,   r@   c                 T    | 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   rP   )r)   r@   instruction_pairr9   s       r*   embed_queryzOllamaEmbeddings.embed_query   s8     #4<d<<KK!1 233A6	r,   ),__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   model_configrE   rP   rV   rY   r1   r,   r*   r   r      sB         . -Hc,,,-E3(s(((.&s&&&."Hhsm"""B %)L(5/(((5
 %)L(5/(((% "GXc]!!!$ "GXc]!!!+ !%J$$$N
 $(M8C='''= '+NHUO***, $(K%'''= !%D(49
$$$&!E8E?!!!M  E8C=0 "E8E?!!!C  M4J"GXd^"""
 
c3h 
 
 
 X
* $(L(4.'''"AWS#X%6 A A A XA :H2FFFL"3 "4; " " " "HHDI H$tE{*; H H H H T#Y 4U3D     U      r,   r   )loggingtypingr   r   r   r   r   r:   langchain_core._api.deprecationr   langchain_core.embeddingsr	   pydanticr
   r   	getLoggerrZ   rM   r   r1   r,   r*   <module>rl      s     5 5 5 5 5 5 5 5 5 5 5 5 5 5  6 6 6 6 6 6 0 0 0 0 0 0 * * * * * * * *		8	$	$ 
:  
S S S S Sy* S S 
S S Sr,   