Fix .env loading and add debug logging for API key

- Load .env from script directory explicitly
- Add logging to show .env file location and existence
- Show API key preview on startup for debugging
This commit is contained in:
Z User 2026-03-29 04:47:54 +00:00
parent b23964b35a
commit 5ec2ef5911

27
main.py
View File

@ -7,7 +7,7 @@ with any OpenAI-compatible client (like Open WebUI). Behind the scenes, it:
1. Detects URLs in user messages and auto-downloads websites
2. Ingests website content into the RAG knowledge base
3. Retrieves relevant context from the knowledge base
4. Passes the enriched context to GLM-4.7-Flash for response generation
4. Passes the enriched context to OpenRouter for response generation
The user sees a normal chat experience, but the system is actually doing
sophisticated RAG operations in the background.
@ -24,10 +24,16 @@ import sys
import time
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, AsyncIterator, Optional
# Load environment variables from .env file
# Load environment variables from .env file (look in script directory)
from dotenv import load_dotenv
SCRIPT_DIR = Path(__file__).parent.resolve()
ENV_FILE = SCRIPT_DIR / ".env"
load_dotenv(ENV_FILE)
# Also try loading from current working directory
load_dotenv()
# Configure logging
@ -208,11 +214,24 @@ async def lifespan(app: FastAPI):
state.tool_manager = None
# Initialize OpenRouter client for upstream LLM
# Debug: Show .env file status
log.info(f"Looking for .env file at: {ENV_FILE}")
log.info(f".env file exists: {ENV_FILE.exists()}")
api_key = config.OPENROUTER_API_KEY
if api_key:
key_preview = f"{api_key[:8]}...{api_key[-4:]}" if len(api_key) > 12 else "***"
log.info(f"OPENROUTER_API_KEY found: {key_preview}")
else:
log.warning("OPENROUTER_API_KEY not found in environment!")
log.warning(f"Checked .env at: {ENV_FILE}")
log.warning("Set OPENROUTER_API_KEY in .env file or as environment variable")
try:
if config.OPENROUTER_API_KEY:
if api_key:
log.info("Initializing OpenRouter client...")
state.llm_client = AsyncOpenAI(
api_key=config.OPENROUTER_API_KEY,
api_key=api_key,
base_url=config.OPENROUTER_BASE_URL,
)
log.info(f"OpenRouter client initialized successfully (model: {config.UPSTREAM_MODEL})")