Fix/language unification (#283)
* [changes]add user_summary language unification * [add]Entity extraction, user memory, emotion suggestions, unified language type for writing * [add]Complete the switch between Chinese and English for the emotion labels and emotion suggestions fields. * [changes]add user_summary language unification * [add]Entity extraction, user memory, emotion suggestions, unified language type for writing * [add]Complete the switch between Chinese and English for the emotion labels and emotion suggestions fields. * [changes]Modify the code based on the AI review
This commit is contained in:
@@ -25,12 +25,12 @@ from fastapi import APIRouter, Depends, HTTPException, Header
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.error_codes import BizCode
|
||||
from app.core.language_utils import get_language_from_header
|
||||
from app.core.logging_config import get_api_logger
|
||||
from app.core.response_utils import fail, success
|
||||
from app.db import get_db
|
||||
from app.dependencies import get_current_user
|
||||
from app.models.user_model import User
|
||||
from app.services.memory_base_service import Translation_English
|
||||
from app.core.memory.models.ontology_models import OntologyClass
|
||||
from typing import List
|
||||
from app.schemas.ontology_schemas import (
|
||||
@@ -63,72 +63,6 @@ router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
async def translate_ontology_classes(
|
||||
classes: List[OntologyClass],
|
||||
model_id: str
|
||||
) -> List[OntologyClass]:
|
||||
"""翻译本体类列表
|
||||
|
||||
将本体类的中文字段翻译为英文,包括:
|
||||
- name_chinese: 中文名称
|
||||
- description: 描述
|
||||
- examples: 示例列表
|
||||
|
||||
Args:
|
||||
classes: 本体类列表
|
||||
model_id: LLM模型ID,用于翻译
|
||||
|
||||
Returns:
|
||||
List[OntologyClass]: 翻译后的本体类列表
|
||||
"""
|
||||
translated_classes = []
|
||||
|
||||
for ontology_class in classes:
|
||||
# 创建类的副本,避免修改原对象
|
||||
translated_class = ontology_class.model_copy(deep=True)
|
||||
|
||||
# 翻译 name_chinese 字段
|
||||
if translated_class.name_chinese:
|
||||
try:
|
||||
translated_class.name_chinese = await Translation_English(
|
||||
model_id,
|
||||
translated_class.name_chinese
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to translate name_chinese: {e}")
|
||||
# 保留原文
|
||||
|
||||
# 翻译 description 字段
|
||||
if translated_class.description:
|
||||
try:
|
||||
translated_class.description = await Translation_English(
|
||||
model_id,
|
||||
translated_class.description
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to translate description: {e}")
|
||||
# 保留原文
|
||||
|
||||
# 翻译 examples 列表
|
||||
if translated_class.examples:
|
||||
translated_examples = []
|
||||
for example in translated_class.examples:
|
||||
try:
|
||||
translated_example = await Translation_English(
|
||||
model_id,
|
||||
example
|
||||
)
|
||||
translated_examples.append(translated_example)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to translate example: {e}")
|
||||
translated_examples.append(example) # 保留原文
|
||||
translated_class.examples = translated_examples
|
||||
|
||||
translated_classes.append(translated_class)
|
||||
|
||||
return translated_classes
|
||||
|
||||
|
||||
def _get_ontology_service(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
@@ -243,7 +177,7 @@ def _get_ontology_service(
|
||||
@router.post("/extract", response_model=ApiResponse)
|
||||
async def extract_ontology(
|
||||
request: ExtractionRequest,
|
||||
language_type: str = Header(default="zh", alias="X-Language-Type"),
|
||||
language_type: str = Header(default=None, alias="X-Language-Type"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
@@ -260,31 +194,6 @@ async def extract_ontology(
|
||||
db: 数据库会话
|
||||
current_user: 当前用户
|
||||
|
||||
Returns:
|
||||
ApiResponse: 包含提取结果的响应
|
||||
|
||||
Response format:
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "本体提取成功",
|
||||
"data": {
|
||||
"classes": [
|
||||
{
|
||||
"id": "147d9db50b524a9e909e01a753d3acdd",
|
||||
"name": "Patient",
|
||||
"name_chinese": "患者",
|
||||
"description": "在医疗机构中接受诊疗、护理或健康管理的个体",
|
||||
"examples": ["糖尿病患者", "术后康复患者", "门诊初诊患者"],
|
||||
"parent_class": null,
|
||||
"entity_type": "Person",
|
||||
"domain": "Healthcare"
|
||||
},
|
||||
...
|
||||
],
|
||||
"domain": "Healthcare",
|
||||
"extracted_count": 7
|
||||
}
|
||||
}
|
||||
"""
|
||||
api_logger.info(
|
||||
f"Ontology extraction requested by user {current_user.id}, "
|
||||
@@ -296,6 +205,9 @@ async def extract_ontology(
|
||||
)
|
||||
|
||||
try:
|
||||
# 使用集中化的语言校验
|
||||
language = get_language_from_header(language_type)
|
||||
|
||||
# 获取当前工作空间ID
|
||||
workspace_id = current_user.current_workspace_id
|
||||
if not workspace_id:
|
||||
@@ -314,33 +226,11 @@ async def extract_ontology(
|
||||
scenario=request.scenario,
|
||||
domain=request.domain,
|
||||
scene_id=request.scene_id,
|
||||
workspace_id=workspace_id
|
||||
workspace_id=workspace_id,
|
||||
language=language
|
||||
)
|
||||
|
||||
# ===== 新增:翻译逻辑 =====
|
||||
# 如果需要英文,则翻译数据
|
||||
if language_type != 'zh':
|
||||
api_logger.info(f"Translating extraction result to English")
|
||||
|
||||
# 翻译 classes 列表
|
||||
result.classes = await translate_ontology_classes(
|
||||
result.classes,
|
||||
request.llm_id
|
||||
)
|
||||
|
||||
# 翻译 domain 字段
|
||||
if result.domain:
|
||||
try:
|
||||
result.domain = await Translation_English(
|
||||
request.llm_id,
|
||||
result.domain
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to translate domain: {e}")
|
||||
# 保留原文
|
||||
# ===== 翻译逻辑结束 =====
|
||||
|
||||
# 构建响应
|
||||
# 构建响应(语言已在提取时通过模板控制,无需二次翻译)
|
||||
response = ExtractionResponse(
|
||||
classes=result.classes,
|
||||
domain=result.domain,
|
||||
|
||||
Reference in New Issue
Block a user