-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathcohere.py
336 lines (301 loc) · 12.7 KB
/
cohere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from __future__ import annotations as _annotations
from collections.abc import Iterable
from dataclasses import dataclass, field
from itertools import chain
from typing import Literal, Union, cast, overload
from cohere import TextAssistantMessageContentItem
from httpx import AsyncClient as AsyncHTTPClient
from typing_extensions import assert_never, deprecated
from .. import ModelHTTPError, result
from .._utils import generate_tool_call_id as _generate_tool_call_id, guard_tool_call_id as _guard_tool_call_id
from ..messages import (
ModelMessage,
ModelRequest,
ModelResponse,
ModelResponsePart,
RetryPromptPart,
SystemPromptPart,
TextPart,
ToolCallPart,
ToolReturnPart,
UserPromptPart,
)
from ..providers import Provider, infer_provider
from ..settings import ModelSettings
from ..tools import ToolDefinition
from . import (
Model,
ModelRequestParameters,
cached_async_http_client,
check_allow_model_requests,
)
try:
from cohere import (
AssistantChatMessageV2,
AsyncClientV2,
ChatMessageV2,
ChatResponse,
SystemChatMessageV2,
ToolCallV2,
ToolCallV2Function,
ToolChatMessageV2,
ToolV2,
ToolV2Function,
UserChatMessageV2,
)
from cohere.core.api_error import ApiError
from cohere.v2.client import OMIT
except ImportError as _import_error:
raise ImportError(
'Please install `cohere` to use the Cohere model, '
'you can use the `cohere` optional group — `pip install "pydantic-ai-slim[cohere]"`'
) from _import_error
LatestCohereModelNames = Literal[
'c4ai-aya-expanse-32b',
'c4ai-aya-expanse-8b',
'command',
'command-light',
'command-light-nightly',
'command-nightly',
'command-r',
'command-r-03-2024',
'command-r-08-2024',
'command-r-plus',
'command-r-plus-04-2024',
'command-r-plus-08-2024',
'command-r7b-12-2024',
]
"""Latest Cohere models."""
CohereModelName = Union[str, LatestCohereModelNames]
"""Possible Cohere model names.
Since Cohere supports a variety of date-stamped models, we explicitly list the latest models but
allow any name in the type hints.
See [Cohere's docs](https://docs.cohere.com/v2/docs/models) for a list of all available models.
"""
class CohereModelSettings(ModelSettings):
"""Settings used for a Cohere model request."""
# This class is a placeholder for any future cohere-specific settings
@dataclass(init=False)
class CohereModel(Model):
"""A model that uses the Cohere API.
Internally, this uses the [Cohere Python client](
https://github.com/cohere-ai/cohere-python) to interact with the API.
Apart from `__init__`, all methods are private or match those of the base class.
"""
client: AsyncClientV2 = field(repr=False)
_model_name: CohereModelName = field(repr=False)
_system: str = field(default='cohere', repr=False)
@overload
def __init__(
self,
model_name: CohereModelName,
*,
provider: Literal['cohere'] | Provider[AsyncClientV2] = 'cohere',
api_key: None = None,
cohere_client: None = None,
http_client: None = None,
) -> None: ...
@deprecated('Use the `provider` parameter instead of `api_key`, `cohere_client`, and `http_client`.')
@overload
def __init__(
self,
model_name: CohereModelName,
*,
provider: None = None,
api_key: str | None = None,
cohere_client: AsyncClientV2 | None = None,
http_client: AsyncHTTPClient | None = None,
) -> None: ...
def __init__(
self,
model_name: CohereModelName,
*,
provider: Literal['cohere'] | Provider[AsyncClientV2] | None = None,
api_key: str | None = None,
cohere_client: AsyncClientV2 | None = None,
http_client: AsyncHTTPClient | None = None,
):
"""Initialize an Cohere model.
Args:
model_name: The name of the Cohere model to use. List of model names
available [here](https://docs.cohere.com/docs/models#command).
provider: The provider to use for authentication and API access. Can be either the string
'cohere' or an instance of `Provider[AsyncClientV2]`. If not provided, a new provider will be
created using the other parameters.
api_key: The API key to use for authentication, if not provided, the
`CO_API_KEY` environment variable will be used if available.
cohere_client: An existing Cohere async client to use. If provided,
`api_key` and `http_client` must be `None`.
http_client: An existing `httpx.AsyncClient` to use for making HTTP requests.
"""
self._model_name: CohereModelName = model_name
if provider is not None:
if isinstance(provider, str):
provider = infer_provider(provider)
self.client = provider.client
elif cohere_client is not None:
assert http_client is None, 'Cannot provide both `cohere_client` and `http_client`'
assert api_key is None, 'Cannot provide both `cohere_client` and `api_key`'
self.client = cohere_client
else:
self.client = AsyncClientV2(api_key=api_key, httpx_client=http_client or cached_async_http_client())
@property
def base_url(self) -> str:
client_wrapper = self.client._client_wrapper # type: ignore
return str(client_wrapper.get_base_url())
async def request(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> tuple[ModelResponse, result.Usage]:
check_allow_model_requests()
response = await self._chat(messages, cast(CohereModelSettings, model_settings or {}), model_request_parameters)
return self._process_response(response), _map_usage(response)
@property
def model_name(self) -> CohereModelName:
"""The model name."""
return self._model_name
@property
def system(self) -> str:
"""The system / model provider."""
return self._system
async def _chat(
self,
messages: list[ModelMessage],
model_settings: CohereModelSettings,
model_request_parameters: ModelRequestParameters,
) -> ChatResponse:
tools = self._get_tools(model_request_parameters)
cohere_messages = list(chain(*(self._map_message(m) for m in messages)))
try:
return await self.client.chat(
model=self._model_name,
messages=cohere_messages,
tools=tools or OMIT,
max_tokens=model_settings.get('max_tokens', OMIT),
temperature=model_settings.get('temperature', OMIT),
p=model_settings.get('top_p', OMIT),
seed=model_settings.get('seed', OMIT),
presence_penalty=model_settings.get('presence_penalty', OMIT),
frequency_penalty=model_settings.get('frequency_penalty', OMIT),
)
except ApiError as e:
if (status_code := e.status_code) and status_code >= 400:
raise ModelHTTPError(status_code=status_code, model_name=self.model_name, body=e.body) from e
raise
def _process_response(self, response: ChatResponse) -> ModelResponse:
"""Process a non-streamed response, and prepare a message to return."""
parts: list[ModelResponsePart] = []
if response.message.content is not None and len(response.message.content) > 0:
# While Cohere's API returns a list, it only does that for future proofing
# and currently only one item is being returned.
choice = response.message.content[0]
parts.append(TextPart(choice.text))
for c in response.message.tool_calls or []:
if c.function and c.function.name and c.function.arguments:
parts.append(
ToolCallPart(
tool_name=c.function.name,
args=c.function.arguments,
tool_call_id=c.id or _generate_tool_call_id(),
)
)
return ModelResponse(parts=parts, model_name=self._model_name)
def _map_message(self, message: ModelMessage) -> Iterable[ChatMessageV2]:
"""Just maps a `pydantic_ai.Message` to a `cohere.ChatMessageV2`."""
if isinstance(message, ModelRequest):
yield from self._map_user_message(message)
elif isinstance(message, ModelResponse):
texts: list[str] = []
tool_calls: list[ToolCallV2] = []
for item in message.parts:
if isinstance(item, TextPart):
texts.append(item.content)
elif isinstance(item, ToolCallPart):
tool_calls.append(self._map_tool_call(item))
else:
assert_never(item)
message_param = AssistantChatMessageV2(role='assistant')
if texts:
message_param.content = [TextAssistantMessageContentItem(text='\n\n'.join(texts))]
if tool_calls:
message_param.tool_calls = tool_calls
yield message_param
else:
assert_never(message)
def _get_tools(self, model_request_parameters: ModelRequestParameters) -> list[ToolV2]:
tools = [self._map_tool_definition(r) for r in model_request_parameters.function_tools]
if model_request_parameters.result_tools:
tools += [self._map_tool_definition(r) for r in model_request_parameters.result_tools]
return tools
@staticmethod
def _map_tool_call(t: ToolCallPart) -> ToolCallV2:
return ToolCallV2(
id=_guard_tool_call_id(t=t),
type='function',
function=ToolCallV2Function(
name=t.tool_name,
arguments=t.args_as_json_str(),
),
)
@staticmethod
def _map_tool_definition(f: ToolDefinition) -> ToolV2:
return ToolV2(
type='function',
function=ToolV2Function(
name=f.name,
description=f.description,
parameters=f.parameters_json_schema,
),
)
@classmethod
def _map_user_message(cls, message: ModelRequest) -> Iterable[ChatMessageV2]:
for part in message.parts:
if isinstance(part, SystemPromptPart):
yield SystemChatMessageV2(role='system', content=part.content)
elif isinstance(part, UserPromptPart):
if isinstance(part.content, str):
yield UserChatMessageV2(role='user', content=part.content)
else:
raise RuntimeError('Cohere does not yet support multi-modal inputs.')
elif isinstance(part, ToolReturnPart):
yield ToolChatMessageV2(
role='tool',
tool_call_id=_guard_tool_call_id(t=part),
content=part.model_response_str(),
)
elif isinstance(part, RetryPromptPart):
if part.tool_name is None:
yield UserChatMessageV2(role='user', content=part.model_response())
else:
yield ToolChatMessageV2(
role='tool',
tool_call_id=_guard_tool_call_id(t=part),
content=part.model_response(),
)
else:
assert_never(part)
def _map_usage(response: ChatResponse) -> result.Usage:
usage = response.usage
if usage is None:
return result.Usage()
else:
details: dict[str, int] = {}
if usage.billed_units is not None:
if usage.billed_units.input_tokens:
details['input_tokens'] = int(usage.billed_units.input_tokens)
if usage.billed_units.output_tokens:
details['output_tokens'] = int(usage.billed_units.output_tokens)
if usage.billed_units.search_units:
details['search_units'] = int(usage.billed_units.search_units)
if usage.billed_units.classifications:
details['classifications'] = int(usage.billed_units.classifications)
request_tokens = int(usage.tokens.input_tokens) if usage.tokens and usage.tokens.input_tokens else None
response_tokens = int(usage.tokens.output_tokens) if usage.tokens and usage.tokens.output_tokens else None
return result.Usage(
request_tokens=request_tokens,
response_tokens=response_tokens,
total_tokens=(request_tokens or 0) + (response_tokens or 0),
details=details,
)