切换成new-api方式进行llm调用

This commit is contained in:
zhaoawd
2025-12-08 23:11:43 +08:00
parent eefaf91ed1
commit f261121845
7 changed files with 145 additions and 57 deletions

View File

@ -22,14 +22,24 @@ from app.models import (
LLMResponse,
LLMRole,
)
from app.settings import DEFAULT_IMPORT_MODEL, get_supported_import_models
from app.settings import (
DEFAULT_IMPORT_MODEL,
NEW_API_AUTH_TOKEN,
NEW_API_BASE_URL,
get_supported_import_models,
)
from app.utils.llm_usage import extract_usage
logger = logging.getLogger(__name__)
IMPORT_GATEWAY_BASE_URL = os.getenv(
"IMPORT_GATEWAY_BASE_URL", "http://localhost:8000"
)
IMPORT_GATEWAY_BASE_URL = os.getenv("IMPORT_GATEWAY_BASE_URL", NEW_API_BASE_URL)
def build_import_gateway_headers() -> dict[str, str]:
headers = {"Content-Type": "application/json"}
if NEW_API_AUTH_TOKEN:
headers["Authorization"] = f"Bearer {NEW_API_AUTH_TOKEN}"
return headers
def _env_float(name: str, default: float) -> float:
@ -314,16 +324,18 @@ async def dispatch_import_analysis_job(
url = f"{IMPORT_GATEWAY_BASE_URL.rstrip('/')}/v1/chat/completions"
logger.info(
"Dispatching import %s to %s: %s",
"Dispatching import %s to %s using provider=%s model=%s",
request.import_record_id,
url,
json.dumps(payload, ensure_ascii=False),
payload.get("provider"),
payload.get("model"),
)
timeout = httpx.Timeout(IMPORT_CHAT_TIMEOUT_SECONDS)
headers = build_import_gateway_headers()
try:
response = await client.post(url, json=payload, timeout=timeout)
response = await client.post(url, json=payload, timeout=timeout, headers=headers)
response.raise_for_status()
except httpx.HTTPStatusError as exc:
body_preview = ""
@ -348,9 +360,10 @@ async def dispatch_import_analysis_job(
response.status_code,
)
logger.info(
"LLM response for %s: %s",
"LLM response received for %s (status %s, choices=%s)",
request.import_record_id,
json.dumps(response_data, ensure_ascii=False),
response.status_code,
len(response_data.get("choices") or []),
)
try:
@ -404,6 +417,7 @@ async def process_import_analysis_job(
request: DataImportAnalysisJobRequest,
client: httpx.AsyncClient,
) -> None:
# Run the import analysis and ensure the callback fires regardless of success/failure.
try:
payload = await dispatch_import_analysis_job(request, client)
except ProviderAPICallError as exc: