fix: 修复 AI 配置 API 启动问题

- 添加 pathlib.Path 导入
- 修复 OpenAICompatibleClient 延迟初始化
- 后端现在可以正常启动并响应配置请求
This commit is contained in:
Yuuuu 2026-02-21 00:16:03 +08:00
parent fd94057e2b
commit b0a8c6edb9
2 changed files with 20 additions and 8 deletions

View File

@ -5,8 +5,10 @@ from sqlalchemy.future import select
from typing import List
from pydantic import BaseModel
from enum import Enum
from pathlib import Path
import uuid
import asyncio
import os
from .database import get_db, engine, Base
from .models import ContextNode, Experiment, Signal, SignalFeedback

View File

@ -123,14 +123,24 @@ class OpenAICompatibleClient(LLMProvider):
"""OpenAI 兼容客户端 (适用于 DeepSeek, Qwen 等)"""
def __init__(self, api_key: str, base_url: str, model: str):
if not api_key:
raise ValueError(f"请设置 {base_url} 的 API_KEY")
self.model = model
self.client = httpx.Client(
base_url=base_url,
self.base_url = base_url
self.api_key = api_key
# 延迟初始化客户端,在实际调用时才检查 API_KEY
self._client = None
@property
def client(self):
"""延迟初始化客户端"""
if self._client is None:
if not self.api_key:
raise ValueError(f"请设置 {self.base_url} 的 API_KEY")
self._client = httpx.Client(
base_url=self.base_url,
timeout=120.0,
headers={"Authorization": f"Bearer {api_key}"}
headers={"Authorization": f"Bearer {self.api_key}"}
)
return self._client
def generate(self, prompt: str, **kwargs) -> str:
response = self.client.post("/chat/completions", json={
@ -154,7 +164,7 @@ class OpenAICompatibleClient(LLMProvider):
def get_embedding(self, text: str) -> List[float]:
# DeepSeek 不支持 embedding,使用替代方案或返回空
if "deepseek" in self.client.base_url:
if "deepseek" in self.base_url:
print("[WARNING] DeepSeek 不支持 embedding API,使用简化向量")
# 简化方案:基于文本内容生成伪向量(实际使用时应该用其他服务)
import hashlib