feat(07-02): singleton OpenAIProvider + GenericOpenAIProvider + MAX_AI_CHARS removal

- openai_provider.py: singleton self._client=AsyncOpenAI(...) in __init__ (D-07),
  _truncate() 60/40 smart truncation (D-13), removed MAX_AI_CHARS constant,
  changed __init__ signature to include context_chars, removed _client() method
- generic_openai_provider.py: new class, subclasses OpenAIProvider, conditional
  response_format={"type":"json_object"} on supports_json_mode flag (D-01/D-02),
  imports parse_classification + parse_suggestions from ai.utils (D-02 contract)
- ollama_provider.py: added context_chars kwarg with default 8000, passes through
- lmstudio_provider.py: added context_chars kwarg with default 8000, passes through
- classifier.py: removed MAX_AI_CHARS constant and text[:MAX_AI_CHARS] slices;
  truncation now handled inside each provider via _truncate()
- requirements.txt: bumped anthropic floor to >=0.95.0 (D-03 output_config support)
This commit is contained in:
curo1305
2026-06-04 18:58:19 +02:00
parent beb5b5e49d
commit 02bcbb9143
6 changed files with 152 additions and 20 deletions
+32 -13
View File
@@ -1,18 +1,39 @@
from __future__ import annotations
from openai import AsyncOpenAI
from ai.base import AIProvider, ClassificationResult
from ai.utils import parse_classification, parse_suggestions
MAX_AI_CHARS = 8_000
class OpenAIProvider(AIProvider):
def __init__(self, api_key: str, model: str = "gpt-4o", base_url=None): # type: ignore[type-arg]
self._api_key = api_key
def __init__(
self,
api_key: str,
model: str = "gpt-4o",
base_url: str | None = None,
context_chars: int = 8000,
):
self._api_key = api_key or "not-needed"
self._model = model
self._base_url = base_url
self._context_chars = context_chars
# Singleton: created once in __init__, reused for all calls on this instance.
# Do NOT recreate per API call — AsyncOpenAI wraps an httpx.AsyncClient
# that maintains a connection pool; recreating per call destroys pool reuse
# and forces a new TLS handshake per request (D-07 / RESEARCH.md).
self._client = AsyncOpenAI(api_key=self._api_key, base_url=self._base_url)
def _client(self) -> AsyncOpenAI:
return AsyncOpenAI(api_key=self._api_key or "placeholder", base_url=self._base_url)
def _truncate(self, text: str) -> str:
"""D-13 smart truncation: first 60% + last 40% of context window.
Captures both document introduction and conclusion, which carry the
most topic signal for long documents.
"""
if len(text) <= self._context_chars:
return text
head_len = int(self._context_chars * 0.6)
tail_len = self._context_chars - head_len
return text[:head_len] + "\n[...truncated...]\n" + text[-tail_len:]
async def classify(
self,
@@ -23,9 +44,9 @@ class OpenAIProvider(AIProvider):
topics_str = ", ".join(existing_topics) if existing_topics else "(none yet)"
user_msg = (
f"Existing topics: [{topics_str}]\n\n"
f"Document text:\n{document_text[:MAX_AI_CHARS]}"
f"Document text:\n{self._truncate(document_text)}"
)
response = await self._client().chat.completions.create(
response = await self._client.chat.completions.create(
model=self._model,
max_tokens=1024,
messages=[
@@ -44,9 +65,9 @@ class OpenAIProvider(AIProvider):
user_msg = (
"Suggest 3-5 topic names for this document. "
"Return ONLY valid JSON: {\"suggested_topics\": [\"topic1\", \"topic2\"]}\n\n"
f"Document text:\n{document_text[:MAX_AI_CHARS]}"
f"Document text:\n{self._truncate(document_text)}"
)
response = await self._client().chat.completions.create(
response = await self._client.chat.completions.create(
model=self._model,
max_tokens=256,
messages=[
@@ -59,7 +80,7 @@ class OpenAIProvider(AIProvider):
async def health_check(self) -> bool:
try:
await self._client().chat.completions.create(
await self._client.chat.completions.create(
model=self._model,
max_tokens=5,
messages=[{"role": "user", "content": "ping"}],
@@ -67,5 +88,3 @@ class OpenAIProvider(AIProvider):
return True
except Exception:
return False