变更项目架构,提高扩展性
This commit is contained in:
30
backend/services/llm_service.py
Normal file
30
backend/services/llm_service.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import dashscope
|
||||
from http import HTTPStatus
|
||||
from backend.core.config import settings
|
||||
|
||||
class LLMService:
|
||||
"""
|
||||
LLM 服务封装层
|
||||
负责与 DashScope 或其他模型供应商交互
|
||||
"""
|
||||
def __init__(self):
|
||||
dashscope.api_key = settings.DASHSCOPE_API_KEY
|
||||
|
||||
def get_embedding(self, text: str, dimension: int = 1536):
|
||||
"""生成文本向量"""
|
||||
try:
|
||||
resp = dashscope.TextEmbedding.call(
|
||||
model=dashscope.TextEmbedding.Models.text_embedding_v4,
|
||||
input=text,
|
||||
dimension=dimension
|
||||
)
|
||||
if resp.status_code == HTTPStatus.OK:
|
||||
return resp.output['embeddings'][0]['embedding']
|
||||
else:
|
||||
print(f"[ERROR] Embedding API Error: {resp}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Embedding Exception: {e}")
|
||||
return None
|
||||
|
||||
llm_service = LLMService()
|
||||
Reference in New Issue
Block a user