Files
epub_bilingual_translator/archive/v0.13/scripts/debug_config.py
T

103 lines
3.4 KiB
Python

import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import asyncio
import httpx
from openai import AsyncOpenAI
# Add project root to sys.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.common.config import load_global_config
async def main():
print("--- Environment Debug ---")
load_dotenv()
env_key = os.getenv("OPENAI_API_KEY")
if env_key:
print(f"OPENAI_API_KEY found in env: {env_key[:8]}...{env_key[-4:]}")
else:
print("OPENAI_API_KEY NOT found in env!")
print("\n--- Config Loader Debug ---")
try:
config = load_global_config()
llm_conf = config.get("llm", {})
conf_key = llm_conf.get("api_key")
base_url = llm_conf.get("base_url")
model = llm_conf.get("model")
print(f"Config Base URL: {base_url}")
print(f"Config Model: {model}")
if conf_key:
print(f"Config API Key: {conf_key[:8]}...{conf_key[-4:]}")
if env_key and conf_key == env_key:
print("Config Key matches Env Key.")
else:
print("Config Key DOES NOT match Env Key!")
else:
print("Config API Key NOT found!")
print("\n--- API Connectivity Test ---")
if not conf_key or not base_url:
print("Missing params for test.")
return
headers = {"Authorization": f"Bearer {conf_key}"}
url = f"{base_url}/models"
print(f"Requesting: {url}")
async with httpx.AsyncClient() as client:
try:
resp = await client.get(url, headers=headers, timeout=10)
print(f"Status Code: {resp.status_code}")
if resp.status_code == 200:
print("Success! Models listed.")
else:
print(f"Failed. Response: {resp.text}")
except Exception as e:
print(f"Exception during request: {e}")
print("\n--- Chat Completion Test (Mimicking LLMClient) ---")
proxy_url = os.environ.get("http_proxy") or os.environ.get("https_proxy")
print(f"Proxy detected: {proxy_url}")
http_client = httpx.AsyncClient(
proxy=proxy_url,
timeout=60.0,
follow_redirects=True
) if proxy_url else None
aclient = AsyncOpenAI(
api_key=conf_key,
base_url=base_url,
http_client=http_client
)
print(f"Model: {model}")
system_prompt = "You are a senior publishing editor."
user_prompt = "Analyze this text."
try:
print("Sending request with System Prompt...")
resp = await aclient.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.3,
)
print("Success!")
print(f"Response: {resp.choices[0].message.content}")
except Exception as e:
print(f"Chat Completion failed: {type(e).__name__}: {e}")
except Exception as e:
print(f"Config loading failed: {e}")
if __name__ == "__main__":
asyncio.run(main())