|
1
|
"""Cohere provider implementation.""" |
|
2
|
|
|
3
|
import logging |
|
4
|
import os |
|
5
|
from pathlib import Path |
|
6
|
from typing import Optional |
|
7
|
|
|
8
|
from dotenv import load_dotenv |
|
9
|
|
|
10
|
from video_processor.providers.base import BaseProvider, ModelInfo, ProviderRegistry |
|
11
|
|
|
12
|
load_dotenv() |
|
13
|
logger = logging.getLogger(__name__) |
|
14
|
|
|
15
|
# Curated list of Cohere models |
|
16
|
_COHERE_MODELS = [ |
|
17
|
ModelInfo( |
|
18
|
id="command-r-plus", |
|
19
|
provider="cohere", |
|
20
|
display_name="Command R+", |
|
21
|
capabilities=["chat"], |
|
22
|
), |
|
23
|
ModelInfo( |
|
24
|
id="command-r", |
|
25
|
provider="cohere", |
|
26
|
display_name="Command R", |
|
27
|
capabilities=["chat"], |
|
28
|
), |
|
29
|
ModelInfo( |
|
30
|
id="command-light", |
|
31
|
provider="cohere", |
|
32
|
display_name="Command Light", |
|
33
|
capabilities=["chat"], |
|
34
|
), |
|
35
|
ModelInfo( |
|
36
|
id="command-nightly", |
|
37
|
provider="cohere", |
|
38
|
display_name="Command Nightly", |
|
39
|
capabilities=["chat"], |
|
40
|
), |
|
41
|
] |
|
42
|
|
|
43
|
|
|
44
|
class CohereProvider(BaseProvider): |
|
45
|
"""Cohere provider using the cohere SDK.""" |
|
46
|
|
|
47
|
provider_name = "cohere" |
|
48
|
|
|
49
|
def __init__(self, api_key: Optional[str] = None): |
|
50
|
try: |
|
51
|
import cohere |
|
52
|
except ImportError: |
|
53
|
raise ImportError("cohere package not installed. Install with: pip install cohere") |
|
54
|
|
|
55
|
self._api_key = api_key or os.getenv("COHERE_API_KEY") |
|
56
|
if not self._api_key: |
|
57
|
raise ValueError("COHERE_API_KEY not set") |
|
58
|
|
|
59
|
self._client = cohere.ClientV2(api_key=self._api_key) |
|
60
|
self._last_usage = {} |
|
61
|
|
|
62
|
def chat( |
|
63
|
self, |
|
64
|
messages: list[dict], |
|
65
|
max_tokens: int = 4096, |
|
66
|
temperature: float = 0.7, |
|
67
|
model: Optional[str] = None, |
|
68
|
) -> str: |
|
69
|
model = model or "command-r-plus" |
|
70
|
|
|
71
|
response = self._client.chat( |
|
72
|
model=model, |
|
73
|
messages=messages, |
|
74
|
max_tokens=max_tokens, |
|
75
|
temperature=temperature, |
|
76
|
) |
|
77
|
|
|
78
|
usage = getattr(response, "usage", None) |
|
79
|
tokens = getattr(usage, "tokens", None) if usage else None |
|
80
|
self._last_usage = { |
|
81
|
"input_tokens": getattr(tokens, "input_tokens", 0) if tokens else 0, |
|
82
|
"output_tokens": getattr(tokens, "output_tokens", 0) if tokens else 0, |
|
83
|
} |
|
84
|
return response.message.content[0].text if response.message.content else "" |
|
85
|
|
|
86
|
def analyze_image( |
|
87
|
self, |
|
88
|
image_bytes: bytes, |
|
89
|
prompt: str, |
|
90
|
max_tokens: int = 4096, |
|
91
|
model: Optional[str] = None, |
|
92
|
) -> str: |
|
93
|
raise NotImplementedError( |
|
94
|
"Cohere does not currently support vision/image analysis. " |
|
95
|
"Use OpenAI, Anthropic, or Gemini for image analysis." |
|
96
|
) |
|
97
|
|
|
98
|
def transcribe_audio( |
|
99
|
self, |
|
100
|
audio_path: str | Path, |
|
101
|
language: Optional[str] = None, |
|
102
|
model: Optional[str] = None, |
|
103
|
) -> dict: |
|
104
|
raise NotImplementedError( |
|
105
|
"Cohere does not provide a transcription API. " |
|
106
|
"Use OpenAI Whisper or Gemini for transcription." |
|
107
|
) |
|
108
|
|
|
109
|
def list_models(self) -> list[ModelInfo]: |
|
110
|
return list(_COHERE_MODELS) |
|
111
|
|
|
112
|
|
|
113
|
ProviderRegistry.register( |
|
114
|
name="cohere", |
|
115
|
provider_class=CohereProvider, |
|
116
|
env_var="COHERE_API_KEY", |
|
117
|
model_prefixes=["command-"], |
|
118
|
default_models={ |
|
119
|
"chat": "command-r-plus", |
|
120
|
"vision": "", |
|
121
|
"audio": "", |
|
122
|
}, |
|
123
|
) |
|
124
|
|