mirror of
https://github.com/langgenius/dify.git
synced 2026-05-30 16:00:32 -04:00
feat: extract model runtime
Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
39
api/dify_graph/template_rendering.py
Normal file
39
api/dify_graph/template_rendering.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any, Protocol
|
||||
|
||||
from dify_graph.nodes.code.code_node import WorkflowCodeExecutor
|
||||
from dify_graph.nodes.code.entities import CodeLanguage
|
||||
|
||||
|
||||
class TemplateRenderError(ValueError):
|
||||
"""Raised when rendering a template fails."""
|
||||
|
||||
|
||||
class Jinja2TemplateRenderer(Protocol):
|
||||
"""Shared contract for rendering Jinja2 templates in graph nodes."""
|
||||
|
||||
def render_template(self, template: str, variables: Mapping[str, Any]) -> str: ...
|
||||
|
||||
|
||||
class CodeExecutorJinja2TemplateRenderer(Jinja2TemplateRenderer):
|
||||
"""Adapter that renders Jinja2 templates via the workflow code executor."""
|
||||
|
||||
_code_executor: WorkflowCodeExecutor
|
||||
|
||||
def __init__(self, code_executor: WorkflowCodeExecutor) -> None:
|
||||
self._code_executor = code_executor
|
||||
|
||||
def render_template(self, template: str, variables: Mapping[str, Any]) -> str:
|
||||
try:
|
||||
result = self._code_executor.execute(language=CodeLanguage.JINJA2, code=template, inputs=variables)
|
||||
except Exception as exc:
|
||||
if self._code_executor.is_execution_error(exc):
|
||||
raise TemplateRenderError(str(exc)) from exc
|
||||
raise
|
||||
|
||||
rendered = result.get("result")
|
||||
if not isinstance(rendered, str):
|
||||
raise TemplateRenderError("Template render result must be a string.")
|
||||
return rendered
|
||||
Reference in New Issue
Block a user