feat:重构UI
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import httpx
|
||||
import json
|
||||
from typing import AsyncGenerator
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from ..core import get_settings
|
||||
from ..core.database import AsyncSessionLocal
|
||||
from ..models import AIProvider
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
@@ -16,6 +21,29 @@ class LLMService:
|
||||
if self.client:
|
||||
await self.client.aclose()
|
||||
|
||||
async def _get_provider(self) -> dict:
|
||||
"""从数据库获取默认的 AI Provider 配置"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
result = await session.execute(
|
||||
select(AIProvider).where(
|
||||
AIProvider.is_active == True,
|
||||
AIProvider.is_default == True
|
||||
)
|
||||
)
|
||||
provider = result.scalar_one_or_none()
|
||||
if provider:
|
||||
return {
|
||||
"api_key": provider.api_key,
|
||||
"base_url": provider.base_url or "https://api.openai.com/v1",
|
||||
"model": provider.model_id,
|
||||
}
|
||||
# 回退到环境变量配置
|
||||
return {
|
||||
"api_key": settings.llm_api_key,
|
||||
"base_url": settings.llm_base_url or "https://api.openai.com/v1",
|
||||
"model": settings.llm_model,
|
||||
}
|
||||
|
||||
def _build_prompt(
|
||||
self,
|
||||
source_text: str,
|
||||
@@ -44,14 +72,14 @@ class LLMService:
|
||||
if not self.client:
|
||||
raise RuntimeError("LLM client not initialized")
|
||||
|
||||
provider = await self._get_provider()
|
||||
messages = self._build_prompt(source_text, source_lang, target_lang, style)
|
||||
base_url = settings.llm_base_url or "https://api.openai.com/v1"
|
||||
|
||||
response = await self.client.post(
|
||||
f"{base_url}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {settings.llm_api_key}"},
|
||||
f"{provider['base_url']}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {provider['api_key']}"},
|
||||
json={
|
||||
"model": settings.llm_model,
|
||||
"model": provider["model"],
|
||||
"messages": messages,
|
||||
"temperature": settings.default_temperature,
|
||||
},
|
||||
@@ -70,15 +98,15 @@ class LLMService:
|
||||
if not self.client:
|
||||
raise RuntimeError("LLM client not initialized")
|
||||
|
||||
provider = await self._get_provider()
|
||||
messages = self._build_prompt(source_text, source_lang, target_lang, style)
|
||||
base_url = settings.llm_base_url or "https://api.openai.com/v1"
|
||||
|
||||
async with self.client.stream(
|
||||
"POST",
|
||||
f"{base_url}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {settings.llm_api_key}"},
|
||||
f"{provider['base_url']}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {provider['api_key']}"},
|
||||
json={
|
||||
"model": settings.llm_model,
|
||||
"model": provider["model"],
|
||||
"messages": messages,
|
||||
"temperature": settings.default_temperature,
|
||||
"stream": True,
|
||||
@@ -89,7 +117,6 @@ class LLMService:
|
||||
data = line[6:]
|
||||
if data == "[DONE]":
|
||||
break
|
||||
import json
|
||||
chunk = json.loads(data)
|
||||
delta = chunk["choices"][0].get("delta", {})
|
||||
if "content" in delta:
|
||||
|
||||
Reference in New Issue
Block a user