Skip to content

Add codebase embeddings and reranking endpoints - #105

Merged
maiphucgiang merged 1 commit into
mainfrom
feature/codebase-embeddings-rerank
Oct 29, 2026
Merged

maiphucgiang merged 1 commit into
mainfrom
feature/codebase-embeddings-rerank

Conversation

@maiphucgiang

@maiphucgiang maiphucgiang commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Add OpenAI-compatible /v1/embeddings backed by the domestic codebase service, exposing BGE-M3 and Qwen embedding aliases with 1024-dimensional float/base64 output.
  • Prefer the no-usage codebase route, keep a bounded paid fallback only for BGE route unavailability, and reuse account routing, binding, cooldown, failover and concurrency controls.
  • Add /v1/rerank for the fixed codebase reranker without exposing unverified relevance scores, plus model catalog, management and WebUI metadata.
  • Add preview simulations, English/Chinese client documentation and offline regression coverage.

Tests

  • for s in tests/test_*.py; do uv run python -B "$s"; done — 2474 passed, 2 skipped, 5576 subtests passed.
  • uv run python -B -m pytest -q tests/test_embeddings.py tests/test_rerank.py tests/test_preview.py tests/test_observability.py tests/test_region_routing.py — 205 passed, 365 subtests passed.
  • cd web && vp check — formatting, lint and type checks passed.
  • git diff --check — passed.

Summary by Sourcery

Expose codebase embedding and reranking capabilities through OpenAI-compatible APIs with managed routing, catalog integration, documentation, and regression coverage.

New Features:

  • Add OpenAI-compatible embedding support for BGE-M3 and Qwen models with float and base64 vector responses.
  • Add a codebase-backed reranking endpoint that returns ranked document indices and optional documents without relevance scores.

Enhancements:

  • Integrate embedding and reranking models with account routing, policy bindings, eligibility checks, failover, cooldowns, concurrency limits, observability, and model catalogs.
  • Provide bounded BGE fallback routing to the paid embedding service when the codebase route is unavailable.
  • Expose embedding and reranking capabilities in the management API and WebUI model inventory.

Documentation:

  • Document embedding and reranking APIs, supported models, routing behavior, limits, and client examples in English and Chinese.

Tests:

  • Add offline contract, routing, fallback, preview, observability, and validation coverage for embeddings and reranking.

- Route embeddings through the codebase service with BGE-M3 and Qwen aliases, bounded paid fallback, account-aware failover and usage handling.
- Expose the fixed codebase reranker without unverified relevance scores, including model catalog, management and WebUI metadata.
- Add preview simulations, English/Chinese client documentation and offline regression coverage.

Tests:
- uv run python -B -m pytest -q tests/test_embeddings.py tests/test_rerank.py tests/test_preview.py tests/test_observability.py tests/test_region_routing.py
- cd web && vp check
@sourcery-ai

sourcery-ai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Sorry @maiphucgiang, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 3 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR adds domestic codebase-backed /v1/embeddings and /v1/rerank endpoints, integrating strict OpenAI-compatible contracts with existing account routing, policy, concurrency, failover, observability, model catalog, management, WebUI, preview simulation, documentation, and offline regression coverage.

Sequence diagram for codebase embeddings with bounded fallback

sequenceDiagram
    participant Client
    participant Gateway
    participant AccountPool
    participant Codebase
    participant PaidEmbeddings

    Client->>Gateway: POST /v1/embeddings
    Gateway->>Gateway: request_body(payload, model)
    Gateway->>AccountPool: _embedding_cred_for(model)
    AccountPool-->>Gateway: credential and headers
    Gateway->>Codebase: POST /codebase/openai/v1/embeddings
    alt codebase route unavailable for bge-m3
        Gateway->>PaidEmbeddings: POST /v2/embeddings
        PaidEmbeddings-->>Gateway: vectors and usage
    else route succeeds
        Codebase-->>Gateway: vectors and usage
    end
    Gateway->>Gateway: parse_reply(payload, model, encoding, expected_count)
    Gateway-->>Client: 1024-dimensional OpenAI-compatible vectors
Loading

Sequence diagram for codebase reranking

sequenceDiagram
    participant Client
    participant Gateway
    participant AccountPool
    participant Codebase

    Client->>Gateway: POST /v1/rerank
    Gateway->>Gateway: request_body(payload)
    Gateway->>AccountPool: _embedding_cred_for(model, kind=rerank)
    AccountPool-->>Gateway: credential and headers
    Gateway->>Codebase: POST /codebase/openai/v1/rerank
    Codebase-->>Gateway: ordered result indices
    Gateway->>Gateway: parse_reply(payload, documents, top_n, return_documents)
    Gateway-->>Client: ranked indices and optional documents
Loading

File-Level Changes

Change Details Files
Implemented OpenAI-compatible embedding contracts, validation, vector encoding conversion, model aliases, and strict upstream response handling.
  • Supports text, batched text, token IDs, 1024 dimensions, float/base64 output, and sanitized usage fields.
  • Maps public BGE and Qwen identifiers to codebase upstream models while rejecting embedding models on chat routes.
  • Validates vector count, ordering, dimensions, finite values, and upstream error payloads.
app/embeddings.py
app/model_policy.py
converter.py
Added account-aware embedding and reranking request routing with bounded concurrency, failover, cooldown, binding, and observability integration.
  • Routes domestic codebase traffic through shared credential eligibility and capacity controls.
  • Allows BGE-only fallback from unavailable codebase embedding routes to the paid endpoint; Qwen has no paid fallback.
  • Adds reranker routing and bounded replay across eligible accounts without exposing relevance scores.
converter.py
app/site_routing.py
app/request_context.py
Added the public rerank endpoint and strict request/response contract for the fixed codebase reranker.
  • Validates query, document count, top_n, and return_documents.
  • Returns ranked indices and optional source documents while omitting upstream scores and preserving usage metadata.
  • Publishes reranker model metadata and prevents chat/rate-probe misuse.
app/rerank.py
converter.py
app/model_policy.py
app/gateway_management.py
Integrated embedding and reranker models into catalogs, management previews, WebUI metadata, and client documentation.
  • Marks models with embedding/rerank capabilities and filters availability using codebase account eligibility.
  • Adds management inventory and preview handling plus WebUI badges.
  • Documents endpoint formats, aliases, fallback behavior, concurrency, billing caveats, and unverified reranker identity in English and Chinese.
app/gateway_management.py
app/model_policy.py
converter.py
web/src/api.ts
web/src/pages/Models.tsx
docs/clients.md
docs/clients.zh-CN.md
Extended the offline upstream simulator and regression suite to cover contracts, routing, fallback, preview billing, observability, and model catalog behavior.
  • Simulates paid embeddings, zero-usage codebase embeddings, and deterministic reranking.
  • Covers validation, aliases, bindings, disabled routes, failover boundaries, base64 conversion, concurrency, and no-score responses.
  • Adds protocol/usage observability and separates embedding/reranker models from chat catalog assertions.
preview/upstream.py
tests/test_embeddings.py
tests/test_rerank.py
tests/test_preview.py
tests/test_observability.py
tests/test_region_routing.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@maiphucgiang
maiphucgiang merged commit ae6819c into main Oct 29, 2026
9 checks passed
@maiphucgiang
maiphucgiang deleted the feature/codebase-embeddings-rerank branch October 29, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant