diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index ce8a3ed..0f82051 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -20,6 +20,7 @@ repos: - id: ruff-format - repo: https://github.com/astral-sh/ty-pre-commit - rev: v0.0.29 + rev: v0.0.49 hooks: - id: ty + args: ["--project", "backend/"] diff --git a/backend/app/core/config.py b/backend/app/core/config.py index d8feca7..2293082 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -9,10 +9,14 @@ from pydantic_settings import BaseSettings, SettingsConfigDict def find_env_file() -> Path | None: - """Find .env file in current or parent directories.""" + """Find .env file in current, parent, or backend/ subdirectory.""" current = Path.cwd() - for path in [current, current.parent]: - env_file = path / ".env" + for candidate in [ + current, + current.parent, + current / "backend", + ]: + env_file = candidate / ".env" if env_file.exists(): return env_file return None diff --git a/backend/app/main.py b/backend/app/main.py index 71f3f47..04c96af 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -7,6 +7,8 @@ from typing import TypedDict logger = logging.getLogger(__name__) +from typing import TYPE_CHECKING + from fastapi import FastAPI from fastapi_pagination import add_pagination @@ -14,6 +16,9 @@ from app.api.exception_handlers import register_exception_handlers from app.api.router import api_router from app.clients.redis import RedisClient from app.core.config import settings + +if TYPE_CHECKING: + from app.services.rag.reranker import RerankService from app.core.logfire_setup import instrument_app, setup_logfire from app.core.logging import setup_logging from app.core.middleware import RequestIDMiddleware @@ -27,6 +32,7 @@ class LifespanState(TypedDict, total=False): redis: RedisClient embedding_service: EmbeddingService vector_store: BaseVectorStore + rerank_service: "RerankService" @asynccontextmanager diff --git a/backend/app/services/rag/embeddings.py b/backend/app/services/rag/embeddings.py index 22480f9..2b94554 100644 --- a/backend/app/services/rag/embeddings.py +++ b/backend/app/services/rag/embeddings.py @@ -48,14 +48,19 @@ class OpenAIEmbeddingProvider(BaseEmbeddingProvider): Uses OpenAI's embedding models to generate text embeddings. """ - def __init__(self, model: str) -> None: + def __init__(self, model: str, base_url: str | None = None) -> None: """Initialize the OpenAI embedding provider. Args: model: The OpenAI embedding model name (e.g., 'text-embedding-3-small'). + base_url: Explicit base URL for the OpenAI API. If not provided, + defaults to the standard OpenAI API, explicitly ignoring any + OPENAI_BASE_URL env var (which may point to a chat-only local + server that doesn't support embeddings). """ self.model = model - self.client = OpenAI() + # Explicitly pass base_url to avoid picking up OPENAI_BASE_URL from env + self.client = OpenAI(base_url=base_url or "https://api.openai.com/v1") def embed_queries(self, texts: list[str]) -> list[list[float]]: """Embed a list of query texts using OpenAI.