"""Ollama large language models."""from__future__importannotationsfromcollections.abcimportAsyncIterator,Iterator,Mappingfromtypingimport(Any,Literal,Optional,Union,)fromlangchain_core.callbacksimport(AsyncCallbackManagerForLLMRun,CallbackManagerForLLMRun,)fromlangchain_core.language_modelsimportBaseLLM,LangSmithParamsfromlangchain_core.outputsimportGenerationChunk,LLMResultfromollamaimportAsyncClient,Client,OptionsfrompydanticimportPrivateAttr,model_validatorfromtyping_extensionsimportSelffrom._utilsimportvalidate_model
[docs]classOllamaLLM(BaseLLM):"""OllamaLLM large language models. Example: .. code-block:: python from langchain_ollama import OllamaLLM model = OllamaLLM(model="llama3") print(model.invoke("Come up with 10 names for a song about parrots")) """model:str"""Model name to use."""reasoning:Optional[bool]=None"""Controls the reasoning/thinking mode for `supported models <https://ollama.com/search?c=thinking>`__. - ``True``: Enables reasoning mode. The model's reasoning process will be captured and returned separately in the ``additional_kwargs`` of the response message, under ``reasoning_content``. The main response content will not include the reasoning tags. - ``False``: Disables reasoning mode. The model will not perform any reasoning, and the response will not include any reasoning content. - ``None`` (Default): The model will use its default reasoning behavior. If the model performs reasoning, the ``<think>`` and ``</think>`` tags will be present directly within the main response content."""validate_model_on_init:bool=False"""Whether to validate the model exists in ollama locally on initialization."""mirostat:Optional[int]=None"""Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)"""mirostat_eta:Optional[float]=None"""Influences how quickly the algorithm responds to feedback from the generated text. A lower learning rate will result in slower adjustments, while a higher learning rate will make the algorithm more responsive. (Default: 0.1)"""mirostat_tau:Optional[float]=None"""Controls the balance between coherence and diversity of the output. A lower value will result in more focused and coherent text. (Default: 5.0)"""num_ctx:Optional[int]=None"""Sets the size of the context window used to generate the next token. (Default: 2048)"""num_gpu:Optional[int]=None"""The number of GPUs to use. On macOS it defaults to 1 to enable metal support, 0 to disable."""num_thread:Optional[int]=None"""Sets the number of threads to use during computation. By default, Ollama will detect this for optimal performance. It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number of cores)."""num_predict:Optional[int]=None"""Maximum number of tokens to predict when generating text. (Default: 128, -1 = infinite generation, -2 = fill context)"""repeat_last_n:Optional[int]=None"""Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = num_ctx)"""repeat_penalty:Optional[float]=None"""Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1)"""temperature:Optional[float]=None"""The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)"""seed:Optional[int]=None"""Sets the random number seed to use for generation. Setting this to a specific number will make the model generate the same text for the same prompt."""stop:Optional[list[str]]=None"""Sets the stop tokens to use."""tfs_z:Optional[float]=None"""Tail free sampling is used to reduce the impact of less probable tokens from the output. A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this setting. (default: 1)"""top_k:Optional[int]=None"""Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)"""top_p:Optional[float]=None"""Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)"""format:Literal["","json"]="""""Specify the format of the output (options: json)"""keep_alive:Optional[Union[int,str]]=None"""How long the model will stay loaded into memory."""base_url:Optional[str]=None"""Base url the model is hosted under."""client_kwargs:Optional[dict]={}"""Additional kwargs to pass to the httpx clients. These arguments are passed to both synchronous and async clients. Use sync_client_kwargs and async_client_kwargs to pass different arguments to synchronous and asynchronous clients. """async_client_kwargs:Optional[dict]={}"""Additional kwargs to merge with client_kwargs before passing to the HTTPX AsyncClient. For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#asyncclient>`__. """sync_client_kwargs:Optional[dict]={}"""Additional kwargs to merge with client_kwargs before passing to the HTTPX Client. For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#client>`__. """_client:Optional[Client]=PrivateAttr(default=None)""" The client to use for making requests. """_async_client:Optional[AsyncClient]=PrivateAttr(default=None)""" The async client to use for making requests. """def_generate_params(self,prompt:str,stop:Optional[list[str]]=None,**kwargs:Any,)->dict[str,Any]:ifself.stopisnotNoneandstopisnotNone:msg="`stop` found in both the input and default params."raiseValueError(msg)ifself.stopisnotNone:stop=self.stopoptions_dict=kwargs.pop("options",{"mirostat":self.mirostat,"mirostat_eta":self.mirostat_eta,"mirostat_tau":self.mirostat_tau,"num_ctx":self.num_ctx,"num_gpu":self.num_gpu,"num_thread":self.num_thread,"num_predict":self.num_predict,"repeat_last_n":self.repeat_last_n,"repeat_penalty":self.repeat_penalty,"temperature":self.temperature,"seed":self.seed,"stop":self.stopifstopisNoneelsestop,"tfs_z":self.tfs_z,"top_k":self.top_k,"top_p":self.top_p,},)return{"prompt":prompt,"stream":kwargs.pop("stream",True),"model":kwargs.pop("model",self.model),"think":kwargs.pop("reasoning",self.reasoning),"format":kwargs.pop("format",self.format),"options":Options(**options_dict),"keep_alive":kwargs.pop("keep_alive",self.keep_alive),**kwargs,}@propertydef_llm_type(self)->str:"""Return type of LLM."""return"ollama-llm"def_get_ls_params(self,stop:Optional[list[str]]=None,**kwargs:Any)->LangSmithParams:"""Get standard params for tracing."""params=super()._get_ls_params(stop=stop,**kwargs)ifmax_tokens:=kwargs.get("num_predict",self.num_predict):params["ls_max_tokens"]=max_tokensreturnparams@model_validator(mode="after")def_set_clients(self)->Self:"""Set clients to use for ollama."""client_kwargs=self.client_kwargsor{}sync_client_kwargs=client_kwargsifself.sync_client_kwargs:sync_client_kwargs={**sync_client_kwargs,**self.sync_client_kwargs}async_client_kwargs=client_kwargsifself.async_client_kwargs:async_client_kwargs={**async_client_kwargs,**self.async_client_kwargs}self._client=Client(host=self.base_url,**sync_client_kwargs)self._async_client=AsyncClient(host=self.base_url,**async_client_kwargs)ifself.validate_model_on_init:validate_model(self._client,self.model)returnselfasyncdef_acreate_generate_stream(self,prompt:str,stop:Optional[list[str]]=None,**kwargs:Any,)->AsyncIterator[Union[Mapping[str,Any],str]]:ifself._async_client:asyncforpartinawaitself._async_client.generate(**self._generate_params(prompt,stop=stop,**kwargs)):yieldpartdef_create_generate_stream(self,prompt:str,stop:Optional[list[str]]=None,**kwargs:Any,)->Iterator[Union[Mapping[str,Any],str]]:ifself._client:yield fromself._client.generate(**self._generate_params(prompt,stop=stop,**kwargs))asyncdef_astream_with_aggregation(self,prompt:str,stop:Optional[list[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,verbose:bool=False,# noqa: FBT001, FBT002**kwargs:Any,)->GenerationChunk:final_chunk=Nonethinking_content=""asyncforstream_respinself._acreate_generate_stream(prompt,stop,**kwargs):ifnotisinstance(stream_resp,str):ifstream_resp.get("thinking"):thinking_content+=stream_resp["thinking"]chunk=GenerationChunk(text=stream_resp.get("response",""),generation_info=(dict(stream_resp)ifstream_resp.get("done")isTrueelseNone),)iffinal_chunkisNone:final_chunk=chunkelse:final_chunk+=chunkifrun_manager:awaitrun_manager.on_llm_new_token(chunk.text,chunk=chunk,verbose=verbose,)iffinal_chunkisNone:msg="No data received from Ollama stream."raiseValueError(msg)ifthinking_content:iffinal_chunk.generation_info:final_chunk.generation_info["thinking"]=thinking_contentelse:final_chunk.generation_info={"thinking":thinking_content}returnfinal_chunkdef_stream_with_aggregation(self,prompt:str,stop:Optional[list[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,verbose:bool=False,# noqa: FBT001, FBT002**kwargs:Any,)->GenerationChunk:final_chunk=Nonethinking_content=""forstream_respinself._create_generate_stream(prompt,stop,**kwargs):ifnotisinstance(stream_resp,str):ifstream_resp.get("thinking"):thinking_content+=stream_resp["thinking"]chunk=GenerationChunk(text=stream_resp.get("response",""),generation_info=(dict(stream_resp)ifstream_resp.get("done")isTrueelseNone),)iffinal_chunkisNone:final_chunk=chunkelse:final_chunk+=chunkifrun_manager:run_manager.on_llm_new_token(chunk.text,chunk=chunk,verbose=verbose,)iffinal_chunkisNone:msg="No data received from Ollama stream."raiseValueError(msg)ifthinking_content:iffinal_chunk.generation_info:final_chunk.generation_info["thinking"]=thinking_contentelse:final_chunk.generation_info={"thinking":thinking_content}returnfinal_chunkdef_generate(self,prompts:list[str],stop:Optional[list[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->LLMResult:generations=[]forpromptinprompts:final_chunk=self._stream_with_aggregation(prompt,stop=stop,run_manager=run_manager,verbose=self.verbose,**kwargs,)generations.append([final_chunk])returnLLMResult(generations=generations)# type: ignore[arg-type]asyncdef_agenerate(self,prompts:list[str],stop:Optional[list[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->LLMResult:generations=[]forpromptinprompts:final_chunk=awaitself._astream_with_aggregation(prompt,stop=stop,run_manager=run_manager,verbose=self.verbose,**kwargs,)generations.append([final_chunk])returnLLMResult(generations=generations)# type: ignore[arg-type]def_stream(self,prompt:str,stop:Optional[list[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[GenerationChunk]:reasoning=kwargs.get("reasoning",self.reasoning)forstream_respinself._create_generate_stream(prompt,stop,**kwargs):ifnotisinstance(stream_resp,str):additional_kwargs={}ifreasoningand(thinking_content:=stream_resp.get("thinking")):additional_kwargs["reasoning_content"]=thinking_contentchunk=GenerationChunk(text=(stream_resp.get("response","")),generation_info={"finish_reason":self.stop,**additional_kwargs,**(dict(stream_resp)ifstream_resp.get("done")isTrueelse{}),},)ifrun_manager:run_manager.on_llm_new_token(chunk.text,verbose=self.verbose,)yieldchunkasyncdef_astream(self,prompt:str,stop:Optional[list[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->AsyncIterator[GenerationChunk]:reasoning=kwargs.get("reasoning",self.reasoning)asyncforstream_respinself._acreate_generate_stream(prompt,stop,**kwargs):ifnotisinstance(stream_resp,str):additional_kwargs={}ifreasoningand(thinking_content:=stream_resp.get("thinking")):additional_kwargs["reasoning_content"]=thinking_contentchunk=GenerationChunk(text=(stream_resp.get("response","")),generation_info={"finish_reason":self.stop,**additional_kwargs,**(dict(stream_resp)ifstream_resp.get("done")isTrueelse{}),},)ifrun_manager:awaitrun_manager.on_llm_new_token(chunk.text,verbose=self.verbose,)yieldchunk