-
Notifications
You must be signed in to change notification settings - Fork 0
feat: embed fragments before publication #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d758867
feat: embed fragments before publication
stone16 81f09b4
fix: harden embedding provider failures
stone16 e229198
fix: bound and validate fragment embeddings
stone16 a13de09
fix: keep embeddings in product lane
stone16 1300ee4
fix: validate stored embedding bounds
stone16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| """Network-free and external adapters for the Supply embedding seam.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Callable | ||
| from contextlib import closing | ||
| from dataclasses import dataclass, field | ||
| from hashlib import shake_256 | ||
| from math import sqrt | ||
| from typing import IO, BinaryIO, cast | ||
| from urllib.error import HTTPError | ||
| from urllib.parse import urlsplit | ||
| from urllib.request import HTTPRedirectHandler, Request, build_opener | ||
|
|
||
| from engine.supply.embeddings import ( | ||
| CONTEXT_FRAGMENT_EMBEDDING_DIMENSION, | ||
| EmbeddingProfile, | ||
| EmbeddingProviderUnavailable, | ||
| EmbeddingVector, | ||
| validate_embedding_batch, | ||
| ) | ||
|
|
||
| _MAX_EXTERNAL_RESPONSE_BYTES = 64 * 1024 * 1024 | ||
| _DEFAULT_TIMEOUT_SECONDS = 30.0 | ||
| EmbeddingTransport = Callable[[Request, float, int], bytes] | ||
|
|
||
|
|
||
| class _RejectRedirectHandler(HTTPRedirectHandler): | ||
| """Keep the configured endpoint as the only bearer-credential recipient.""" | ||
|
|
||
| def redirect_request( | ||
| self, | ||
| request: Request, | ||
| fp: IO[bytes], | ||
| code: int, | ||
| message: str, | ||
| headers: object, | ||
| new_url: str, | ||
| ) -> Request: | ||
| del message, new_url | ||
| raise HTTPError( | ||
| request.full_url, | ||
| code, | ||
| "Embedding redirect is unavailable", | ||
| headers, # type: ignore[arg-type] | ||
| fp, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class ExternalEmbeddingConfiguration: | ||
| """Environment-derived external provider configuration.""" | ||
|
|
||
| endpoint: str = field(repr=False) | ||
| model: str | ||
| api_key: str = field(repr=False) | ||
| dimension: int | ||
| batch_size: int | ||
| timeout_seconds: float = _DEFAULT_TIMEOUT_SECONDS | ||
|
|
||
| def __post_init__(self) -> None: | ||
| parsed = urlsplit(self.endpoint) | ||
| if ( | ||
| type(self.endpoint) is not str | ||
| or not self.endpoint | ||
| or self.endpoint != self.endpoint.strip() | ||
| or parsed.scheme != "https" | ||
| or not parsed.hostname | ||
| or parsed.username is not None | ||
| or parsed.password is not None | ||
| or bool(parsed.query) | ||
| or bool(parsed.fragment) | ||
| or type(self.model) is not str | ||
| or not self.model | ||
| or self.model != self.model.strip() | ||
| or type(self.api_key) is not str | ||
| or not self.api_key | ||
| or self.api_key != self.api_key.strip() | ||
| or type(self.timeout_seconds) not in {int, float} | ||
| or not 0 < float(self.timeout_seconds) <= 120 | ||
| or type(self.batch_size) is not int | ||
| or not 1 <= self.batch_size <= 256 | ||
| ): | ||
| raise ValueError("Embedding configuration is not available") | ||
| EmbeddingProfile(self.dimension) | ||
|
|
||
|
|
||
| def _default_transport(request: Request, timeout: float, maximum_bytes: int) -> bytes: | ||
| with closing( | ||
| cast( | ||
| BinaryIO, | ||
| build_opener(_RejectRedirectHandler()).open( # noqa: S310 | ||
| request, | ||
| timeout=timeout, | ||
| ), | ||
| ) | ||
| ) as response: | ||
| payload = response.read(maximum_bytes + 1) | ||
| if len(payload) > maximum_bytes: | ||
| raise OSError("embedding response exceeded the configured bound") | ||
| return payload | ||
|
|
||
|
|
||
| class ExternalEmbeddingProvider: | ||
| """Call one environment-configured JSON embedding endpoint.""" | ||
|
|
||
| __slots__ = ("_configuration", "_transport") | ||
|
|
||
| def __init__( | ||
| self, | ||
| configuration: ExternalEmbeddingConfiguration, | ||
| *, | ||
| transport: EmbeddingTransport = _default_transport, | ||
| ) -> None: | ||
| if type(configuration) is not ExternalEmbeddingConfiguration: | ||
| raise TypeError("External embedding configuration is required") | ||
| if not callable(transport): | ||
| raise TypeError("External embedding transport is required") | ||
| self._configuration = configuration | ||
| self._transport = transport | ||
|
|
||
| @property | ||
| def profile(self) -> EmbeddingProfile: | ||
| return EmbeddingProfile(self._configuration.dimension) | ||
|
|
||
| def embed(self, inputs: tuple[str, ...]) -> tuple[EmbeddingVector, ...]: | ||
| if ( | ||
| type(inputs) is not tuple | ||
| or not inputs | ||
| or any(type(value) is not str or not value for value in inputs) | ||
| ): | ||
| raise EmbeddingProviderUnavailable("Embedding provider is unavailable") | ||
| try: | ||
| vectors: list[EmbeddingVector] = [] | ||
| for offset in range(0, len(inputs), self._configuration.batch_size): | ||
| batch = inputs[offset : offset + self._configuration.batch_size] | ||
| vectors.extend(self._embed_batch(batch)) | ||
| return tuple(vectors) | ||
| except Exception: | ||
| raise EmbeddingProviderUnavailable( | ||
| "Embedding provider is unavailable" | ||
| ) from None | ||
|
|
||
| def _embed_batch(self, inputs: tuple[str, ...]) -> tuple[EmbeddingVector, ...]: | ||
| body = json.dumps( | ||
| { | ||
| "dimensions": self.profile.dimension, | ||
| "encoding_format": "float", | ||
| "input": list(inputs), | ||
| "model": self._configuration.model, | ||
| }, | ||
| ensure_ascii=False, | ||
| separators=(",", ":"), | ||
| ).encode("utf-8") | ||
| request = Request( | ||
| self._configuration.endpoint, | ||
| data=body, | ||
| headers={ | ||
| "Accept": "application/json", | ||
| "Authorization": f"Bearer {self._configuration.api_key}", | ||
| "Content-Type": "application/json", | ||
| }, | ||
| method="POST", | ||
| ) | ||
| raw_response = self._transport( | ||
| request, | ||
| float(self._configuration.timeout_seconds), | ||
| _MAX_EXTERNAL_RESPONSE_BYTES, | ||
| ) | ||
| response = json.loads(raw_response) | ||
| raw_data = response["data"] | ||
| if type(raw_data) is not list or len(raw_data) != len(inputs): | ||
| raise ValueError | ||
| ordered: list[list[object] | None] = [None] * len(inputs) | ||
| for item in raw_data: | ||
| if type(item) is not dict: | ||
| raise ValueError | ||
| index = item.get("index") | ||
| vector = item.get("embedding") | ||
| if ( | ||
| type(index) is not int | ||
| or not 0 <= index < len(inputs) | ||
| or ordered[index] is not None | ||
| or type(vector) is not list | ||
| ): | ||
| raise ValueError | ||
| ordered[index] = cast(list[object], vector) | ||
| if any(vector is None for vector in ordered): | ||
| raise ValueError | ||
| return validate_embedding_batch( | ||
| inputs, | ||
| cast(list[list[object]], ordered), | ||
| self.profile, | ||
| ) | ||
|
|
||
|
|
||
| class DeterministicEmbeddingTwin: | ||
| """Stable content-derived vectors for tests without network egress.""" | ||
|
|
||
| __slots__ = ("_profile",) | ||
|
|
||
| def __init__( | ||
| self, | ||
| dimension: int = CONTEXT_FRAGMENT_EMBEDDING_DIMENSION, | ||
| ) -> None: | ||
| self._profile = EmbeddingProfile(dimension) | ||
|
|
||
| @property | ||
| def profile(self) -> EmbeddingProfile: | ||
| return self._profile | ||
|
|
||
| def embed(self, inputs: tuple[str, ...]) -> tuple[EmbeddingVector, ...]: | ||
| if ( | ||
| type(inputs) is not tuple | ||
| or not inputs | ||
| or any(type(value) is not str or not value for value in inputs) | ||
| ): | ||
| raise EmbeddingProviderUnavailable("Embedding provider is unavailable") | ||
| vectors: list[EmbeddingVector] = [] | ||
| for value in inputs: | ||
| raw = shake_256( | ||
| b"context-engine.embedding-twin.v1\x00" + value.encode("utf-8") | ||
| ).digest(self.profile.dimension * 2) | ||
| unscaled = tuple( | ||
| (int.from_bytes(raw[offset : offset + 2], "big") - 32767.5) / 32767.5 | ||
| for offset in range(0, len(raw), 2) | ||
| ) | ||
| norm = sqrt(sum(component * component for component in unscaled)) | ||
| vectors.append(tuple(component / norm for component in unscaled)) | ||
| return validate_embedding_batch(inputs, vectors, self.profile) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.