feat: enhance configuration to find .env in backend subdirectory and update OpenAIEmbeddingProvider to accept base URL

This commit is contained in:
2026-06-12 14:57:51 +08:00
parent 05991609a0
commit 21cee0c2bd
4 changed files with 22 additions and 6 deletions
+2 -1
View File
@@ -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/"]
+7 -3
View File
@@ -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
+6
View File
@@ -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
+7 -2
View File
@@ -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.