-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproviders.py
More file actions
306 lines (251 loc) · 9.76 KB
/
providers.py
File metadata and controls
306 lines (251 loc) · 9.76 KB
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
"""
feather_db.providers — LLM Provider Abstraction
================================================
Thin wrappers that expose a single `complete(messages) -> str` interface
for every major LLM backend — closed-source and open-source alike.
Supported backends
------------------
ClaudeProvider — Anthropic Claude (claude-haiku-4-5, claude-opus-4-6, …)
OpenAIProvider — OpenAI, Azure OpenAI, Groq, Mistral, Together AI, vLLM
OllamaProvider — Ollama local server (subclass of OpenAIProvider)
GeminiProvider — Google Gemini (gemini-2.0-flash, …)
All providers default to temperature=0.0 — deterministic output is critical
for the JSON classification task in ContextEngine.ingest().
Usage
-----
from feather_db.providers import ClaudeProvider, OllamaProvider
# Closed-source
p = ClaudeProvider(model="claude-haiku-4-5", api_key="sk-ant-...")
# Open-source local
p = OllamaProvider(model="llama3.1:8b")
reply = p.complete([
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Say hi."},
])
"""
from __future__ import annotations
import os
from abc import ABC, abstractmethod
from typing import Optional
# ── Base ──────────────────────────────────────────────────────────────────────
class LLMProvider(ABC):
"""
Minimal LLM abstraction for ContextEngine.
Subclass and implement `complete()`.
"""
@abstractmethod
def complete(
self,
messages: list[dict],
max_tokens: int = 512,
temperature: float = 0.0,
) -> str:
"""
Send `messages` to the model and return the text reply.
Parameters
----------
messages : OpenAI-style list of {"role": ..., "content": ...} dicts
max_tokens : upper bound on response length
temperature : 0.0 = fully deterministic (recommended for JSON tasks)
"""
...
def name(self) -> str:
return self.__class__.__name__
def __repr__(self) -> str:
return f"<{self.name()}>"
# ── Claude ────────────────────────────────────────────────────────────────────
class ClaudeProvider(LLMProvider):
"""
Anthropic Claude via the `anthropic` SDK.
Default model: claude-haiku-4-5 — fast and cheap for ingestion pipelines.
Swap to claude-opus-4-6 for highest-quality classification.
pip install anthropic
"""
def __init__(
self,
model: str = "claude-haiku-4-5-20251001",
api_key: Optional[str] = None,
max_retries: int = 2,
):
try:
import anthropic as _ant
except ImportError:
raise ImportError("pip install anthropic # required for ClaudeProvider")
self._model = model
self._client = _ant.Anthropic(
api_key=api_key or os.environ.get("ANTHROPIC_API_KEY"),
max_retries=max_retries,
)
def complete(
self,
messages: list[dict],
max_tokens: int = 512,
temperature: float = 0.0,
) -> str:
# Anthropic separates system from conversation messages
system = ""
conv: list[dict] = []
for m in messages:
if m["role"] == "system":
system = m["content"]
else:
conv.append(m)
kwargs: dict = {
"model": self._model,
"max_tokens": max_tokens,
"messages": conv,
}
if system:
kwargs["system"] = system
# Anthropic doesn't accept temperature=0.0 via kwarg the same way;
# pass only if non-default to avoid API version issues
if temperature != 1.0:
kwargs["temperature"] = temperature
response = self._client.messages.create(**kwargs)
return response.content[0].text
def name(self) -> str:
return f"ClaudeProvider({self._model})"
# ── OpenAI-compatible ─────────────────────────────────────────────────────────
class OpenAIProvider(LLMProvider):
"""
OpenAI Chat Completions API — and any OpenAI-compatible endpoint:
OpenAI, Azure OpenAI, Groq, Mistral, Together AI, vLLM, LM Studio, …
pip install openai
Examples
--------
OpenAIProvider() # OpenAI default
OpenAIProvider(model="gpt-4o")
OpenAIProvider( # Groq
model="llama-3.3-70b-versatile",
api_key=GROQ_KEY,
base_url="https://api.groq.com/openai/v1",
)
OpenAIProvider( # vLLM local
model="mistralai/Mistral-7B-Instruct-v0.3",
api_key="EMPTY",
base_url="http://localhost:8000/v1",
)
"""
def __init__(
self,
model: str = "gpt-4o-mini",
api_key: Optional[str] = None,
base_url: Optional[str] = None,
max_retries: int = 2,
json_mode: bool = True, # enable response_format=json_object when True
):
try:
from openai import OpenAI
except ImportError:
raise ImportError("pip install openai # required for OpenAIProvider")
self._model = model
self._json_mode = json_mode
self._client = OpenAI(
api_key=api_key or os.environ.get("OPENAI_API_KEY", ""),
base_url=base_url,
max_retries=max_retries,
)
def complete(
self,
messages: list[dict],
max_tokens: int = 512,
temperature: float = 0.0,
) -> str:
kwargs: dict = {
"model": self._model,
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
}
if self._json_mode:
kwargs["response_format"] = {"type": "json_object"}
response = self._client.chat.completions.create(**kwargs)
return response.choices[0].message.content or ""
def name(self) -> str:
return f"OpenAIProvider({self._model})"
# ── Ollama ────────────────────────────────────────────────────────────────────
class OllamaProvider(OpenAIProvider):
"""
Ollama local server — zero-config wrapper around OpenAIProvider.
Ollama exposes an OpenAI-compatible endpoint at /v1/chat/completions.
No extra SDK needed.
Usage
-----
OllamaProvider() # llama3.1:8b at localhost:11434
OllamaProvider(model="mistral:7b")
OllamaProvider(model="phi3", base_url="http://remote-host:11434/v1")
Ensure the model is pulled first:
ollama pull llama3.1:8b
"""
def __init__(
self,
model: str = "llama3.1:8b",
base_url: str = "http://localhost:11434/v1",
json_mode: bool = True,
):
# Ollama accepts any string as the API key
super().__init__(
model=model,
api_key="ollama",
base_url=base_url,
max_retries=1, # local server — one retry is enough
json_mode=json_mode,
)
def name(self) -> str:
return f"OllamaProvider({self._model})"
# ── Gemini ────────────────────────────────────────────────────────────────────
class GeminiProvider(LLMProvider):
"""
Google Gemini via the `google-genai` SDK.
pip install google-genai
Usage
-----
GeminiProvider(api_key="AIza...")
GeminiProvider(model="gemini-2.0-flash-lite") # cheapest Gemini
"""
def __init__(
self,
model: str = "gemini-2.0-flash",
api_key: Optional[str] = None,
):
try:
from google import genai
from google.genai import types as _gt
except ImportError:
raise ImportError("pip install google-genai # required for GeminiProvider")
self._model = model
key = api_key or os.environ.get("GOOGLE_API_KEY")
if not key:
raise ValueError(
"Provide api_key= or set GOOGLE_API_KEY env var. "
"Use GeminiProvider(api_key='AIza...')"
)
self._client = genai.Client(api_key=key)
self._types = _gt
def complete(
self,
messages: list[dict],
max_tokens: int = 512,
temperature: float = 0.0,
) -> str:
# Convert OpenAI-style messages to a Gemini-compatible prompt
system_parts = [m["content"] for m in messages if m["role"] == "system"]
user_parts = [m["content"] for m in messages if m["role"] != "system"]
system_text = "\n\n".join(system_parts)
user_text = "\n\n".join(user_parts)
config_kwargs: dict = {
"max_output_tokens": max_tokens,
"temperature": temperature,
"response_mime_type": "application/json", # JSON mode
}
if system_text:
config_kwargs["system_instruction"] = system_text
config = self._types.GenerateContentConfig(**config_kwargs)
response = self._client.models.generate_content(
model=self._model,
contents=user_text,
config=config,
)
return response.text or ""
def name(self) -> str:
return f"GeminiProvider({self._model})"