Ollama locally runs large language models. To use, follow the instructions at https://ollama.ai/. Example: .. code-block:: python from langchain_community.chat_models import ChatOllama ollama = ChatOllama(model="llama2")
| 48 | |
| 49 | |
| 50 | class ChatOllama(BaseChatModel, _OllamaCommon): |
| 51 | """Ollama locally runs large language models. |
| 52 | |
| 53 | To use, follow the instructions at https://ollama.ai/. |
| 54 | |
| 55 | Example: |
| 56 | .. code-block:: python |
| 57 | |
| 58 | from langchain_community.chat_models import ChatOllama |
| 59 | ollama = ChatOllama(model="llama2") |
| 60 | """ |
| 61 | |
| 62 | @property |
| 63 | def _llm_type(self) -> str: |
| 64 | """Return type of chat model.""" |
| 65 | return "ollama-chat" |
| 66 | |
| 67 | @classmethod |
| 68 | def is_lc_serializable(cls) -> bool: |
| 69 | """Return whether this model can be serialized by Langchain.""" |
| 70 | return False |
| 71 | |
| 72 | def _get_ls_params( |
| 73 | self, stop: Optional[List[str]] = None, **kwargs: Any |
| 74 | ) -> LangSmithParams: |
| 75 | """Get standard params for tracing.""" |
| 76 | params = self._get_invocation_params(stop=stop, **kwargs) |
| 77 | ls_params = LangSmithParams( |
| 78 | ls_provider="ollama", |
| 79 | ls_model_name=self.model, |
| 80 | ls_model_type="chat", |
| 81 | ls_temperature=params.get("temperature", self.temperature), |
| 82 | ) |
| 83 | if ls_max_tokens := params.get("num_predict", self.num_predict): |
| 84 | ls_params["ls_max_tokens"] = ls_max_tokens |
| 85 | if ls_stop := stop or params.get("stop", None) or self.stop: |
| 86 | ls_params["ls_stop"] = ls_stop |
| 87 | return ls_params |
| 88 | |
| 89 | @deprecated("0.0.3", alternative="_convert_messages_to_ollama_messages") |
| 90 | def _format_message_as_text(self, message: BaseMessage) -> str: |
| 91 | if isinstance(message, ChatMessage): |
| 92 | message_text = f"\n\n{message.role.capitalize()}: {message.content}" |
| 93 | elif isinstance(message, HumanMessage): |
| 94 | if isinstance(message.content, List): |
| 95 | first_content = cast(List[Dict], message.content)[0] |
| 96 | content_type = first_content.get("type") |
| 97 | if content_type == "text": |
| 98 | message_text = f"[INST] {first_content['text']} [/INST]" |
| 99 | elif content_type == "image_url": |
| 100 | message_text = first_content["image_url"]["url"] |
| 101 | else: |
| 102 | message_text = f"[INST] {message.content} [/INST]" |
| 103 | elif isinstance(message, AIMessage): |
| 104 | message_text = f"{message.content}" |
| 105 | elif isinstance(message, SystemMessage): |
| 106 | message_text = f"<<SYS>> {message.content} <</SYS>>" |
| 107 | else: |
no outgoing calls