|
12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | | -from __future__ import annotations |
16 | 15 |
|
17 | | -from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Union, cast |
| 16 | +# built-in dependencies |
| 17 | +from __future__ import annotations |
| 18 | +from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Union, cast, overload |
18 | 19 |
|
| 20 | +# 3rd party dependencies |
19 | 21 | from pydantic import ValidationError |
20 | 22 |
|
| 23 | +# project dependencies |
21 | 24 | from neo4j_graphrag.exceptions import LLMGenerationError |
22 | | -from neo4j_graphrag.llm.base import LLMInterface |
| 25 | +from neo4j_graphrag.llm.base import LLMInterface, LLMInterfaceV2 |
23 | 26 | from neo4j_graphrag.utils.rate_limit import ( |
24 | 27 | RateLimitHandler, |
25 | 28 | rate_limit_handler, |
|
39 | 42 | from cohere import ChatMessages |
40 | 43 |
|
41 | 44 |
|
42 | | -class CohereLLM(LLMInterface): |
| 45 | +# pylint: disable=redefined-builtin, arguments-differ, raise-missing-from, no-else-return |
| 46 | +class CohereLLM(LLMInterface, LLMInterfaceV2): |
43 | 47 | """Interface for large language models on the Cohere platform |
44 | 48 |
|
45 | 49 | Args: |
@@ -82,28 +86,67 @@ def __init__( |
82 | 86 | self.client = cohere.ClientV2(**kwargs) |
83 | 87 | self.async_client = cohere.AsyncClientV2(**kwargs) |
84 | 88 |
|
85 | | - def get_messages( |
| 89 | + # overloads for LLMInterface and LLMInterfaceV2 methods |
| 90 | + @overload |
| 91 | + def invoke( |
86 | 92 | self, |
87 | 93 | input: str, |
88 | 94 | message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
89 | 95 | system_instruction: Optional[str] = None, |
90 | | - ) -> ChatMessages: |
91 | | - messages = [] |
92 | | - if system_instruction: |
93 | | - messages.append(SystemMessage(content=system_instruction).model_dump()) |
94 | | - if message_history: |
95 | | - if isinstance(message_history, MessageHistory): |
96 | | - message_history = message_history.messages |
97 | | - try: |
98 | | - MessageList(messages=cast(list[BaseMessage], message_history)) |
99 | | - except ValidationError as e: |
100 | | - raise LLMGenerationError(e.errors()) from e |
101 | | - messages.extend(cast(Iterable[dict[str, Any]], message_history)) |
102 | | - messages.append(UserMessage(content=input).model_dump()) |
103 | | - return messages # type: ignore |
| 96 | + ) -> LLMResponse: ... |
104 | 97 |
|
105 | | - @rate_limit_handler |
| 98 | + @overload |
| 99 | + def invoke( |
| 100 | + self, |
| 101 | + input: List[LLMMessage], |
| 102 | + ) -> LLMResponse: ... |
| 103 | + |
| 104 | + @overload |
| 105 | + async def ainvoke( |
| 106 | + self, |
| 107 | + input: str, |
| 108 | + message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
| 109 | + system_instruction: Optional[str] = None, |
| 110 | + ) -> LLMResponse: ... |
| 111 | + |
| 112 | + @overload |
| 113 | + async def ainvoke( |
| 114 | + self, |
| 115 | + input: List[LLMMessage], |
| 116 | + ) -> LLMResponse: ... |
| 117 | + |
| 118 | + # switching logics to LLMInterface or LLMInterfaceV2 |
106 | 119 | def invoke( |
| 120 | + self, |
| 121 | + input: Union[str, List[LLMMessage]], |
| 122 | + message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
| 123 | + system_instruction: Optional[str] = None, |
| 124 | + ) -> LLMResponse: |
| 125 | + if isinstance(input, str): |
| 126 | + return self.__legacy_invoke(input, message_history, system_instruction) |
| 127 | + elif isinstance(input, list): |
| 128 | + return self.__brand_new_invoke(input) |
| 129 | + else: |
| 130 | + raise ValueError(f"Invalid input type for invoke method - {type(input)}") |
| 131 | + |
| 132 | + async def ainvoke( |
| 133 | + self, |
| 134 | + input: Union[str, List[LLMMessage]], |
| 135 | + message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
| 136 | + system_instruction: Optional[str] = None, |
| 137 | + ) -> LLMResponse: |
| 138 | + if isinstance(input, str): |
| 139 | + return await self.__legacy_ainvoke( |
| 140 | + input, message_history, system_instruction |
| 141 | + ) |
| 142 | + elif isinstance(input, list): |
| 143 | + return await self.__brand_new_ainvoke(input) |
| 144 | + else: |
| 145 | + raise ValueError(f"Invalid input type for ainvoke method - {type(input)}") |
| 146 | + |
| 147 | + # implementations |
| 148 | + @rate_limit_handler |
| 149 | + def __legacy_invoke( |
107 | 150 | self, |
108 | 151 | input: str, |
109 | 152 | message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
@@ -134,8 +177,32 @@ def invoke( |
134 | 177 | content=res.message.content[0].text if res.message.content else "", |
135 | 178 | ) |
136 | 179 |
|
| 180 | + def __brand_new_invoke( |
| 181 | + self, |
| 182 | + input: List[LLMMessage], |
| 183 | + ) -> LLMResponse: |
| 184 | + """Sends text to the LLM and returns a response. |
| 185 | +
|
| 186 | + Args: |
| 187 | + input (str): The text to send to the LLM. |
| 188 | +
|
| 189 | + Returns: |
| 190 | + LLMResponse: The response from the LLM. |
| 191 | + """ |
| 192 | + try: |
| 193 | + messages = self.get_brand_new_messages(input) |
| 194 | + res = self.client.chat( |
| 195 | + messages=messages, |
| 196 | + model=self.model_name, |
| 197 | + ) |
| 198 | + except self.cohere_api_error as e: |
| 199 | + raise LLMGenerationError("Error calling cohere") from e |
| 200 | + return LLMResponse( |
| 201 | + content=res.message.content[0].text if res.message.content else "", |
| 202 | + ) |
| 203 | + |
137 | 204 | @async_rate_limit_handler |
138 | | - async def ainvoke( |
| 205 | + async def __legacy_ainvoke( |
139 | 206 | self, |
140 | 207 | input: str, |
141 | 208 | message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
@@ -165,3 +232,60 @@ async def ainvoke( |
165 | 232 | return LLMResponse( |
166 | 233 | content=res.message.content[0].text if res.message.content else "", |
167 | 234 | ) |
| 235 | + |
| 236 | + async def __brand_new_ainvoke( |
| 237 | + self, |
| 238 | + input: List[LLMMessage], |
| 239 | + ) -> LLMResponse: |
| 240 | + try: |
| 241 | + messages = self.get_brand_new_messages(input) |
| 242 | + res = await self.async_client.chat( |
| 243 | + messages=messages, |
| 244 | + model=self.model_name, |
| 245 | + ) |
| 246 | + except self.cohere_api_error as e: |
| 247 | + raise LLMGenerationError("Error calling cohere") from e |
| 248 | + return LLMResponse( |
| 249 | + content=res.message.content[0].text if res.message.content else "", |
| 250 | + ) |
| 251 | + |
| 252 | + # subsdiary methods |
| 253 | + def get_messages( |
| 254 | + self, |
| 255 | + input: str, |
| 256 | + message_history: Optional[Union[List[LLMMessage], MessageHistory]] = None, |
| 257 | + system_instruction: Optional[str] = None, |
| 258 | + ) -> ChatMessages: |
| 259 | + """Converts input and message history to ChatMessages for Cohere.""" |
| 260 | + messages = [] |
| 261 | + if system_instruction: |
| 262 | + messages.append(SystemMessage(content=system_instruction).model_dump()) |
| 263 | + if message_history: |
| 264 | + if isinstance(message_history, MessageHistory): |
| 265 | + message_history = message_history.messages |
| 266 | + try: |
| 267 | + MessageList(messages=cast(list[BaseMessage], message_history)) |
| 268 | + except ValidationError as e: |
| 269 | + raise LLMGenerationError(e.errors()) from e |
| 270 | + messages.extend(cast(Iterable[dict[str, Any]], message_history)) |
| 271 | + messages.append(UserMessage(content=input).model_dump()) |
| 272 | + return messages # type: ignore |
| 273 | + |
| 274 | + def get_brand_new_messages( |
| 275 | + self, |
| 276 | + input: list[LLMMessage], |
| 277 | + ) -> ChatMessages: |
| 278 | + """Converts a list of LLMMessage to ChatMessages for Cohere.""" |
| 279 | + messages: ChatMessages = [] |
| 280 | + for i in input: |
| 281 | + if i["role"] == "system": |
| 282 | + messages.append(self.cohere.SystemChatMessageV2(content=i["content"])) |
| 283 | + elif i["role"] == "user": |
| 284 | + messages.append(self.cohere.UserChatMessageV2(content=i["content"])) |
| 285 | + elif i["role"] == "assistant": |
| 286 | + messages.append( |
| 287 | + self.cohere.AssistantChatMessageV2(content=i["content"]) |
| 288 | + ) |
| 289 | + else: |
| 290 | + raise ValueError(f"Unknown role: {i['role']}") |
| 291 | + return messages |
0 commit comments