`Cohere` chat large language models. To use, you should have the ``cohere`` python package installed, and the environment variable ``COHERE_API_KEY`` set with your API key, or pass it as a named parameter to the constructor. Example: .. code-block:: python from
| 99 | since="0.0.30", removal="0.3.0", alternative_import="langchain_cohere.ChatCohere" |
| 100 | ) |
| 101 | class ChatCohere(BaseChatModel, BaseCohere): |
| 102 | """`Cohere` chat large language models. |
| 103 | |
| 104 | To use, you should have the ``cohere`` python package installed, and the |
| 105 | environment variable ``COHERE_API_KEY`` set with your API key, or pass |
| 106 | it as a named parameter to the constructor. |
| 107 | |
| 108 | Example: |
| 109 | .. code-block:: python |
| 110 | |
| 111 | from langchain_community.chat_models import ChatCohere |
| 112 | from langchain_core.messages import HumanMessage |
| 113 | |
| 114 | chat = ChatCohere(model="command", max_tokens=256, temperature=0.75) |
| 115 | |
| 116 | messages = [HumanMessage(content="knock knock")] |
| 117 | chat.invoke(messages) |
| 118 | """ |
| 119 | |
| 120 | class Config: |
| 121 | """Configuration for this pydantic object.""" |
| 122 | |
| 123 | allow_population_by_field_name = True |
| 124 | arbitrary_types_allowed = True |
| 125 | |
| 126 | @property |
| 127 | def _llm_type(self) -> str: |
| 128 | """Return type of chat model.""" |
| 129 | return "cohere-chat" |
| 130 | |
| 131 | @property |
| 132 | def _default_params(self) -> Dict[str, Any]: |
| 133 | """Get the default parameters for calling Cohere API.""" |
| 134 | return { |
| 135 | "temperature": self.temperature, |
| 136 | } |
| 137 | |
| 138 | @property |
| 139 | def _identifying_params(self) -> Dict[str, Any]: |
| 140 | """Get the identifying parameters.""" |
| 141 | return {**{"model": self.model}, **self._default_params} |
| 142 | |
| 143 | def _stream( |
| 144 | self, |
| 145 | messages: List[BaseMessage], |
| 146 | stop: Optional[List[str]] = None, |
| 147 | run_manager: Optional[CallbackManagerForLLMRun] = None, |
| 148 | **kwargs: Any, |
| 149 | ) -> Iterator[ChatGenerationChunk]: |
| 150 | request = get_cohere_chat_request(messages, **self._default_params, **kwargs) |
| 151 | |
| 152 | if hasattr(self.client, "chat_stream"): # detect and support sdk v5 |
| 153 | stream = self.client.chat_stream(**request) |
| 154 | else: |
| 155 | stream = self.client.chat(**request, stream=True) |
| 156 | |
| 157 | for data in stream: |
| 158 | if data.event_type == "text-generation": |
no outgoing calls
no test coverage detected