84 lines
3.3 KiB
Python
Raw Normal View History

2025-04-14 11:10:44 +08:00
from typing import Optional
2025-05-29 23:04:04 +08:00
import yaml
2025-06-03 19:02:57 +08:00
from flask_login import current_user
2025-04-14 18:17:17 +08:00
2025-04-14 11:10:44 +08:00
from extensions.ext_database import db
2025-05-29 23:04:04 +08:00
from models.dataset import PipelineCustomizedTemplate
2025-04-14 11:10:44 +08:00
from services.rag_pipeline.pipeline_template.pipeline_template_base import PipelineTemplateRetrievalBase
from services.rag_pipeline.pipeline_template.pipeline_template_type import PipelineTemplateType
class CustomizedPipelineTemplateRetrieval(PipelineTemplateRetrievalBase):
"""
Retrieval recommended app from database
"""
def get_pipeline_templates(self, language: str) -> dict:
2025-04-14 18:17:17 +08:00
result = self.fetch_pipeline_templates_from_customized(
tenant_id=current_user.current_tenant_id, language=language
)
2025-04-14 11:10:44 +08:00
return result
2025-04-14 18:17:17 +08:00
def get_pipeline_template_detail(self, template_id: str):
result = self.fetch_pipeline_template_detail_from_db(template_id)
2025-04-14 11:10:44 +08:00
return result
def get_type(self) -> str:
return PipelineTemplateType.CUSTOMIZED
@classmethod
2025-04-14 18:17:17 +08:00
def fetch_pipeline_templates_from_customized(cls, tenant_id: str, language: str) -> dict:
2025-04-14 11:10:44 +08:00
"""
2025-04-14 18:17:17 +08:00
Fetch pipeline templates from db.
:param tenant_id: tenant id
2025-04-14 11:10:44 +08:00
:param language: language
:return:
"""
2025-05-29 23:04:04 +08:00
pipeline_customized_templates = (
2025-04-14 18:17:17 +08:00
db.session.query(PipelineCustomizedTemplate)
.filter(PipelineCustomizedTemplate.tenant_id == tenant_id, PipelineCustomizedTemplate.language == language)
2025-07-02 11:33:00 +08:00
.order_by(PipelineCustomizedTemplate.position.asc(), PipelineCustomizedTemplate.created_at.desc())
2025-04-14 11:10:44 +08:00
.all()
)
2025-05-29 23:04:04 +08:00
recommended_pipelines_results = []
for pipeline_customized_template in pipeline_customized_templates:
recommended_pipeline_result = {
"id": pipeline_customized_template.id,
"name": pipeline_customized_template.name,
"description": pipeline_customized_template.description,
"icon": pipeline_customized_template.icon,
"position": pipeline_customized_template.position,
"chunk_structure": pipeline_customized_template.chunk_structure,
}
recommended_pipelines_results.append(recommended_pipeline_result)
return {"pipeline_templates": recommended_pipelines_results}
2025-04-14 11:10:44 +08:00
@classmethod
2025-04-14 18:17:17 +08:00
def fetch_pipeline_template_detail_from_db(cls, template_id: str) -> Optional[dict]:
2025-04-14 11:10:44 +08:00
"""
2025-04-14 18:17:17 +08:00
Fetch pipeline template detail from db.
:param template_id: Template ID
2025-04-14 11:10:44 +08:00
:return:
"""
2025-04-14 18:17:17 +08:00
pipeline_template = (
db.session.query(PipelineCustomizedTemplate).filter(PipelineCustomizedTemplate.id == template_id).first()
2025-04-14 11:10:44 +08:00
)
2025-04-14 18:17:17 +08:00
if not pipeline_template:
2025-04-14 11:10:44 +08:00
return None
2025-06-17 19:06:17 +08:00
2025-06-11 16:36:36 +08:00
dsl_data = yaml.safe_load(pipeline_template.yaml_content)
graph_data = dsl_data.get("workflow", {}).get("graph", {})
2025-04-14 11:10:44 +08:00
return {
2025-05-29 23:04:04 +08:00
"id": pipeline_template.id,
"name": pipeline_template.name,
2025-06-11 13:12:18 +08:00
"icon_info": pipeline_template.icon,
"description": pipeline_template.description,
"chunk_structure": pipeline_template.chunk_structure,
2025-06-11 14:03:32 +08:00
"export_data": pipeline_template.yaml_content,
2025-06-11 16:36:36 +08:00
"graph": graph_data,
2025-06-11 13:12:18 +08:00
"created_by": pipeline_template.created_user_name,
2025-04-14 11:10:44 +08:00
}