69 lines
2.6 KiB
Python
Raw Normal View History

2025-04-14 11:10:44 +08:00
import logging
from typing import Optional
import requests
from configs import dify_config
from services.rag_pipeline.pipeline_template.pipeline_template_base import PipelineTemplateRetrievalBase
2025-04-14 18:17:17 +08:00
from services.rag_pipeline.pipeline_template.pipeline_template_type import PipelineTemplateType
from services.recommend_app.buildin.buildin_retrieval import BuildInRecommendAppRetrieval
2025-04-14 11:10:44 +08:00
logger = logging.getLogger(__name__)
class RemotePipelineTemplateRetrieval(PipelineTemplateRetrievalBase):
"""
Retrieval recommended app from dify official
"""
def get_pipeline_template_detail(self, pipeline_id: str):
try:
result = self.fetch_pipeline_template_detail_from_dify_official(pipeline_id)
except Exception as e:
logger.warning(f"fetch recommended app detail from dify official failed: {e}, switch to built-in.")
result = BuildInRecommendAppRetrieval.fetch_recommended_app_detail_from_builtin(pipeline_id)
return result
2025-04-14 18:17:17 +08:00
def get_pipeline_templates(self, language: str) -> dict:
2025-04-14 11:10:44 +08:00
try:
2025-04-14 18:17:17 +08:00
result = self.fetch_pipeline_templates_from_dify_official(language)
2025-04-14 11:10:44 +08:00
except Exception as e:
2025-04-14 18:17:17 +08:00
logger.warning(f"fetch pipeline templates from dify official failed: {e}, switch to built-in.")
2025-04-14 11:10:44 +08:00
result = BuildInRecommendAppRetrieval.fetch_recommended_apps_from_builtin(language)
return result
def get_type(self) -> str:
2025-04-14 18:17:17 +08:00
return PipelineTemplateType.REMOTE
2025-04-14 11:10:44 +08:00
@classmethod
2025-04-14 18:17:17 +08:00
def fetch_pipeline_template_detail_from_dify_official(cls, pipeline_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 dify official.
:param pipeline_id: Pipeline ID
2025-04-14 11:10:44 +08:00
:return:
"""
2025-04-14 18:17:17 +08:00
domain = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_REMOTE_DOMAIN
url = f"{domain}/pipelines/{pipeline_id}"
2025-04-14 11:10:44 +08:00
response = requests.get(url, timeout=(3, 10))
if response.status_code != 200:
return None
data: dict = response.json()
return data
@classmethod
2025-04-14 18:17:17 +08:00
def fetch_pipeline_templates_from_dify_official(cls, language: str) -> dict:
2025-04-14 11:10:44 +08:00
"""
2025-04-14 18:17:17 +08:00
Fetch pipeline templates from dify official.
2025-04-14 11:10:44 +08:00
:param language: language
:return:
"""
2025-04-14 18:17:17 +08:00
domain = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_REMOTE_DOMAIN
url = f"{domain}/pipelines?language={language}"
2025-04-14 11:10:44 +08:00
response = requests.get(url, timeout=(3, 10))
if response.status_code != 200:
2025-04-14 18:17:17 +08:00
raise ValueError(f"fetch pipeline templates failed, status code: {response.status_code}")
2025-04-14 11:10:44 +08:00
result: dict = response.json()
return result