fix: 修复 AI 配置 API 启动问题
- 添加 pathlib.Path 导入 - 修复 OpenAICompatibleClient 延迟初始化 - 后端现在可以正常启动并响应配置请求
This commit is contained in:
parent
fd94057e2b
commit
b0a8c6edb9
|
|
@ -5,8 +5,10 @@ from sqlalchemy.future import select
|
||||||
from typing import List
|
from typing import List
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from pathlib import Path
|
||||||
import uuid
|
import uuid
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import os
|
||||||
|
|
||||||
from .database import get_db, engine, Base
|
from .database import get_db, engine, Base
|
||||||
from .models import ContextNode, Experiment, Signal, SignalFeedback
|
from .models import ContextNode, Experiment, Signal, SignalFeedback
|
||||||
|
|
|
||||||
|
|
@ -123,14 +123,24 @@ class OpenAICompatibleClient(LLMProvider):
|
||||||
"""OpenAI 兼容客户端 (适用于 DeepSeek, Qwen 等)"""
|
"""OpenAI 兼容客户端 (适用于 DeepSeek, Qwen 等)"""
|
||||||
|
|
||||||
def __init__(self, api_key: str, base_url: str, model: str):
|
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.model = model
|
||||||
self.client = httpx.Client(
|
self.base_url = base_url
|
||||||
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,
|
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:
|
def generate(self, prompt: str, **kwargs) -> str:
|
||||||
response = self.client.post("/chat/completions", json={
|
response = self.client.post("/chat/completions", json={
|
||||||
|
|
@ -154,7 +164,7 @@ class OpenAICompatibleClient(LLMProvider):
|
||||||
|
|
||||||
def get_embedding(self, text: str) -> List[float]:
|
def get_embedding(self, text: str) -> List[float]:
|
||||||
# DeepSeek 不支持 embedding,使用替代方案或返回空
|
# DeepSeek 不支持 embedding,使用替代方案或返回空
|
||||||
if "deepseek" in self.client.base_url:
|
if "deepseek" in self.base_url:
|
||||||
print("[WARNING] DeepSeek 不支持 embedding API,使用简化向量")
|
print("[WARNING] DeepSeek 不支持 embedding API,使用简化向量")
|
||||||
# 简化方案:基于文本内容生成伪向量(实际使用时应该用其他服务)
|
# 简化方案:基于文本内容生成伪向量(实际使用时应该用其他服务)
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue