-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathports.py
More file actions
65 lines (40 loc) · 1.83 KB
/
ports.py
File metadata and controls
65 lines (40 loc) · 1.83 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
from __future__ import annotations
from typing import Any, Protocol, runtime_checkable
from .catalog import CatalogEntry, TextDocument
class LLMPort(Protocol):
"""Abstracts LLM backends (Anthropic, OpenAI, etc.)."""
def invoke(self, messages: list[dict[str, str]]) -> str: ...
class DBPort(Protocol):
"""Abstracts database backends (SQLAlchemy, etc.)."""
def execute(self, sql: str) -> list[dict[str, Any]]: ...
class EmbeddingPort(Protocol):
"""Abstracts embedding backends (OpenAI, Azure, Bedrock, etc.)."""
def embed_query(self, text: str) -> list[float]: ...
def embed_texts(self, texts: list[str]) -> list[list[float]]: ...
class VectorStorePort(Protocol):
"""Abstracts vector store backends (InMemory, FAISS, pgvector, etc.)."""
def search(self, vector: list[float], k: int) -> list[tuple[str, float]]:
"""
Return the k nearest vectors.
Returns:
List of (chunk_id, score) sorted by score descending.
Score range: [-1, 1] (cosine similarity).
"""
...
def upsert(self, ids: list[str], vectors: list[list[float]]) -> None:
"""
Store or update vectors by chunk_id.
Implementations must merge incoming entries into existing ones —
calling upsert twice must not lose entries from the first call.
Args:
ids: List of chunk_ids.
vectors: Corresponding embedding vectors (len(ids) == len(vectors)).
"""
...
@runtime_checkable
class DocumentLoaderPort(Protocol):
"""Converts a file path or directory to list[TextDocument]."""
def load(self, path: str) -> list[TextDocument]: ...
class CatalogLoaderPort(Protocol):
"""Abstracts catalog loading from external sources (DataHub, file, database, etc.)."""
def load(self) -> list[CatalogEntry]: ...