File size: 11,714 Bytes
d520909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
Embedding Adapters for RAG Subsystem

Provides:
- Abstract EmbeddingAdapter interface
- Ollama embeddings (local, default)
- OpenAI embeddings (optional, feature-flagged)
"""

from abc import ABC, abstractmethod
from typing import List, Optional, Union
from pydantic import BaseModel, Field
from loguru import logger
import hashlib
import json
from pathlib import Path

try:
    import httpx
    HTTPX_AVAILABLE = True
except ImportError:
    HTTPX_AVAILABLE = False

try:
    import openai
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False


class EmbeddingConfig(BaseModel):
    """Configuration for embedding adapters."""
    # Adapter selection
    adapter_type: str = Field(
        default="ollama",
        description="Embedding adapter type: ollama, openai"
    )

    # Ollama settings
    ollama_base_url: str = Field(
        default="http://localhost:11434",
        description="Ollama API base URL"
    )
    ollama_model: str = Field(
        default="nomic-embed-text",
        description="Ollama embedding model (nomic-embed-text, mxbai-embed-large)"
    )

    # OpenAI settings (feature-flagged)
    openai_enabled: bool = Field(
        default=False,
        description="Enable OpenAI embeddings"
    )
    openai_model: str = Field(
        default="text-embedding-3-small",
        description="OpenAI embedding model"
    )
    openai_api_key: Optional[str] = Field(
        default=None,
        description="OpenAI API key (or use OPENAI_API_KEY env var)"
    )

    # Common settings
    batch_size: int = Field(default=32, ge=1, description="Batch size for embedding")
    timeout: float = Field(default=60.0, ge=1.0, description="Request timeout in seconds")

    # Caching
    enable_cache: bool = Field(default=True, description="Enable embedding cache")
    cache_directory: str = Field(
        default="./data/embedding_cache",
        description="Cache directory for embeddings"
    )


class EmbeddingAdapter(ABC):
    """Abstract interface for embedding adapters."""

    @abstractmethod
    def embed_text(self, text: str) -> List[float]:
        """
        Embed a single text.

        Args:
            text: Text to embed

        Returns:
            Embedding vector
        """
        pass

    @abstractmethod
    def embed_batch(self, texts: List[str]) -> List[List[float]]:
        """
        Embed multiple texts.

        Args:
            texts: List of texts to embed

        Returns:
            List of embedding vectors
        """
        pass

    @property
    @abstractmethod
    def embedding_dimension(self) -> int:
        """Return embedding dimension."""
        pass

    @property
    @abstractmethod
    def model_name(self) -> str:
        """Return model name."""
        pass


class EmbeddingCache:
    """Simple file-based embedding cache."""

    def __init__(self, cache_dir: str, model_name: str):
        """Initialize cache."""
        self.cache_dir = Path(cache_dir) / model_name.replace("/", "_")
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        self._memory_cache: dict = {}

    def _hash_text(self, text: str) -> str:
        """Generate cache key from text."""
        return hashlib.sha256(text.encode()).hexdigest()[:32]

    def get(self, text: str) -> Optional[List[float]]:
        """Get cached embedding."""
        key = self._hash_text(text)

        # Check memory cache first
        if key in self._memory_cache:
            return self._memory_cache[key]

        # Check file cache
        cache_file = self.cache_dir / f"{key}.json"
        if cache_file.exists():
            try:
                with open(cache_file, "r") as f:
                    embedding = json.load(f)
                self._memory_cache[key] = embedding
                return embedding
            except:
                pass

        return None

    def put(self, text: str, embedding: List[float]):
        """Cache embedding."""
        key = self._hash_text(text)

        # Memory cache
        self._memory_cache[key] = embedding

        # File cache
        cache_file = self.cache_dir / f"{key}.json"
        try:
            with open(cache_file, "w") as f:
                json.dump(embedding, f)
        except Exception as e:
            logger.warning(f"Failed to cache embedding: {e}")


class OllamaEmbedding(EmbeddingAdapter):
    """
    Ollama embedding adapter for local embeddings.

    Supports models:
    - nomic-embed-text (768 dimensions, recommended)
    - mxbai-embed-large (1024 dimensions)
    - all-minilm (384 dimensions)
    """

    # Known embedding dimensions
    MODEL_DIMENSIONS = {
        "nomic-embed-text": 768,
        "mxbai-embed-large": 1024,
        "all-minilm": 384,
        "snowflake-arctic-embed": 1024,
    }

    def __init__(self, config: Optional[EmbeddingConfig] = None):
        """Initialize Ollama embedding adapter."""
        if not HTTPX_AVAILABLE:
            raise ImportError("httpx is required for Ollama. Install with: pip install httpx")

        self.config = config or EmbeddingConfig()
        self._base_url = self.config.ollama_base_url.rstrip("/")
        self._model = self.config.ollama_model
        self._dimension: Optional[int] = self.MODEL_DIMENSIONS.get(self._model)

        # Initialize cache if enabled
        self._cache: Optional[EmbeddingCache] = None
        if self.config.enable_cache:
            self._cache = EmbeddingCache(self.config.cache_directory, self._model)

        logger.info(f"OllamaEmbedding initialized: {self._model}")

    def embed_text(self, text: str) -> List[float]:
        """Embed a single text."""
        # Check cache
        if self._cache:
            cached = self._cache.get(text)
            if cached is not None:
                return cached

        # Call Ollama API
        with httpx.Client(timeout=self.config.timeout) as client:
            response = client.post(
                f"{self._base_url}/api/embeddings",
                json={
                    "model": self._model,
                    "prompt": text,
                }
            )
            response.raise_for_status()
            result = response.json()

        embedding = result["embedding"]

        # Update dimension if not known
        if self._dimension is None:
            self._dimension = len(embedding)

        # Cache result
        if self._cache:
            self._cache.put(text, embedding)

        return embedding

    def embed_batch(self, texts: List[str]) -> List[List[float]]:
        """Embed multiple texts."""
        embeddings = []

        for i in range(0, len(texts), self.config.batch_size):
            batch = texts[i:i + self.config.batch_size]

            for text in batch:
                embedding = self.embed_text(text)
                embeddings.append(embedding)

        return embeddings

    @property
    def embedding_dimension(self) -> int:
        """Return embedding dimension."""
        if self._dimension is None:
            # Probe with a test embedding
            test_embedding = self.embed_text("test")
            self._dimension = len(test_embedding)
        return self._dimension

    @property
    def model_name(self) -> str:
        """Return model name."""
        return f"ollama/{self._model}"


class OpenAIEmbedding(EmbeddingAdapter):
    """
    OpenAI embedding adapter (feature-flagged).

    Supports models:
    - text-embedding-3-small (1536 dimensions)
    - text-embedding-3-large (3072 dimensions)
    - text-embedding-ada-002 (1536 dimensions, legacy)
    """

    MODEL_DIMENSIONS = {
        "text-embedding-3-small": 1536,
        "text-embedding-3-large": 3072,
        "text-embedding-ada-002": 1536,
    }

    def __init__(self, config: Optional[EmbeddingConfig] = None):
        """Initialize OpenAI embedding adapter."""
        if not OPENAI_AVAILABLE:
            raise ImportError("openai is required. Install with: pip install openai")

        self.config = config or EmbeddingConfig()

        if not self.config.openai_enabled:
            raise ValueError("OpenAI embeddings not enabled in config")

        self._model = self.config.openai_model
        self._dimension = self.MODEL_DIMENSIONS.get(self._model, 1536)

        # Initialize OpenAI client
        api_key = self.config.openai_api_key
        self._client = openai.OpenAI(api_key=api_key) if api_key else openai.OpenAI()

        # Initialize cache if enabled
        self._cache: Optional[EmbeddingCache] = None
        if self.config.enable_cache:
            self._cache = EmbeddingCache(self.config.cache_directory, self._model)

        logger.info(f"OpenAIEmbedding initialized: {self._model}")

    def embed_text(self, text: str) -> List[float]:
        """Embed a single text."""
        # Check cache
        if self._cache:
            cached = self._cache.get(text)
            if cached is not None:
                return cached

        # Call OpenAI API
        response = self._client.embeddings.create(
            model=self._model,
            input=text,
        )

        embedding = response.data[0].embedding

        # Cache result
        if self._cache:
            self._cache.put(text, embedding)

        return embedding

    def embed_batch(self, texts: List[str]) -> List[List[float]]:
        """Embed multiple texts."""
        embeddings = []

        for i in range(0, len(texts), self.config.batch_size):
            batch = texts[i:i + self.config.batch_size]

            # Check cache for batch
            to_embed = []
            cached_indices = {}

            for j, text in enumerate(batch):
                if self._cache:
                    cached = self._cache.get(text)
                    if cached is not None:
                        cached_indices[j] = cached
                        continue
                to_embed.append((j, text))

            # Embed uncached texts
            if to_embed:
                indices, texts_to_embed = zip(*to_embed)
                response = self._client.embeddings.create(
                    model=self._model,
                    input=list(texts_to_embed),
                )

                for idx, (j, text) in enumerate(to_embed):
                    embedding = response.data[idx].embedding
                    cached_indices[j] = embedding

                    if self._cache:
                        self._cache.put(text, embedding)

            # Reconstruct batch order
            for j in range(len(batch)):
                embeddings.append(cached_indices[j])

        return embeddings

    @property
    def embedding_dimension(self) -> int:
        """Return embedding dimension."""
        return self._dimension

    @property
    def model_name(self) -> str:
        """Return model name."""
        return f"openai/{self._model}"


# Factory function
_embedding_adapter: Optional[EmbeddingAdapter] = None


def get_embedding_adapter(
    config: Optional[EmbeddingConfig] = None,
) -> EmbeddingAdapter:
    """
    Get or create singleton embedding adapter.

    Args:
        config: Embedding configuration

    Returns:
        EmbeddingAdapter instance
    """
    global _embedding_adapter

    if _embedding_adapter is None:
        config = config or EmbeddingConfig()

        if config.adapter_type == "openai" and config.openai_enabled:
            _embedding_adapter = OpenAIEmbedding(config)
        else:
            # Default to Ollama
            _embedding_adapter = OllamaEmbedding(config)

    return _embedding_adapter


def reset_embedding_adapter():
    """Reset the global embedding adapter instance."""
    global _embedding_adapter
    _embedding_adapter = None