mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-08-12 07:19:04 +08:00
Template conversion adds Jinjia2 syntax support (#4545)
### What problem does this PR solve? Template conversion adds Jinjia2 syntax support ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: wangrui <wangrui@haima.me> Co-authored-by: Yingfeng <yingfeng.zhang@gmail.com> Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com>
This commit is contained in:
parent
b23a4a8fea
commit
bbc1d02c96
@ -13,8 +13,10 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import json
|
||||
import re
|
||||
from agent.component.base import ComponentBase, ComponentParamBase
|
||||
from jinja2 import Template as Jinja2Template
|
||||
|
||||
|
||||
class TemplateParam(ComponentParamBase):
|
||||
@ -36,10 +38,15 @@ class Template(ComponentBase):
|
||||
component_name = "Template"
|
||||
|
||||
def get_dependent_components(self):
|
||||
cpnts = set([para["component_id"].split("@")[0] for para in self._param.parameters \
|
||||
if para.get("component_id") \
|
||||
and para["component_id"].lower().find("answer") < 0 \
|
||||
and para["component_id"].lower().find("begin") < 0])
|
||||
cpnts = set(
|
||||
[
|
||||
para["component_id"].split("@")[0]
|
||||
for para in self._param.parameters
|
||||
if para.get("component_id")
|
||||
and para["component_id"].lower().find("answer") < 0
|
||||
and para["component_id"].lower().find("begin") < 0
|
||||
]
|
||||
)
|
||||
return list(cpnts)
|
||||
|
||||
def _run(self, history, **kwargs):
|
||||
@ -54,9 +61,8 @@ class Template(ComponentBase):
|
||||
cpn_id, key = para["component_id"].split("@")
|
||||
for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
|
||||
if p["key"] == key:
|
||||
kwargs[para["key"]] = p.get("value", "")
|
||||
self._param.inputs.append(
|
||||
{"component_id": para["component_id"], "content": kwargs[para["key"]]})
|
||||
value = p.get("value", "")
|
||||
self.make_kwargs(para, kwargs, value)
|
||||
break
|
||||
else:
|
||||
assert False, f"Can't find parameter '{key}' for {cpn_id}"
|
||||
@ -69,18 +75,49 @@ class Template(ComponentBase):
|
||||
hist = hist[0]["content"]
|
||||
else:
|
||||
hist = ""
|
||||
kwargs[para["key"]] = hist
|
||||
self.make_kwargs(para, kwargs, hist)
|
||||
continue
|
||||
|
||||
_, out = cpn.output(allow_partial=False)
|
||||
if "content" not in out.columns:
|
||||
kwargs[para["key"]] = ""
|
||||
else:
|
||||
kwargs[para["key"]] = " - "+"\n - ".join([o if isinstance(o, str) else str(o) for o in out["content"]])
|
||||
self._param.inputs.append({"component_id": para["component_id"], "content": kwargs[para["key"]]})
|
||||
|
||||
result = ""
|
||||
if "content" in out.columns:
|
||||
result = "\n".join(
|
||||
[o if isinstance(o, str) else str(o) for o in out["content"]]
|
||||
)
|
||||
|
||||
self.make_kwargs(para, kwargs, result)
|
||||
|
||||
template = Jinja2Template(content)
|
||||
|
||||
try:
|
||||
content = template.render(kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
for n, v in kwargs.items():
|
||||
content = re.sub(r"\{%s\}" % re.escape(n), str(v).replace("\\", " "), content)
|
||||
try:
|
||||
v = json.dumps(v, ensure_ascii=False)
|
||||
except Exception:
|
||||
pass
|
||||
content = re.sub(
|
||||
r"\{%s\}" % re.escape(n), v, content
|
||||
)
|
||||
content = re.sub(
|
||||
r"(\\\"|\")", "", content
|
||||
)
|
||||
content = re.sub(
|
||||
r"(#+)", r" \1 ", content
|
||||
)
|
||||
|
||||
return Template.be_output(content)
|
||||
|
||||
def make_kwargs(self, para, kwargs, value):
|
||||
self._param.inputs.append(
|
||||
{"component_id": para["component_id"], "content": value}
|
||||
)
|
||||
try:
|
||||
value = json.loads(value)
|
||||
except Exception:
|
||||
pass
|
||||
kwargs[para["key"]] = value
|
||||
|
@ -1123,7 +1123,7 @@ This procedure will improve precision of retrieval by adding more information to
|
||||
testRun: 'Test Run',
|
||||
template: 'Template',
|
||||
templateDescription:
|
||||
'A component that formats user inputs or the outputs of other components.',
|
||||
'A component that formats the output of other components.1. Supports Jinja2 templates, will first convert the input to an object and then render the template, 2. Simultaneously retains the original method of using {parameter} string replacement',
|
||||
emailComponent: 'Email',
|
||||
emailDescription: 'Send an email to a specified address.',
|
||||
smtpServer: 'SMTP Server',
|
||||
|
@ -1049,7 +1049,7 @@ export default {
|
||||
testRun: 'テスト実行',
|
||||
template: 'テンプレート',
|
||||
templateDescription:
|
||||
'このコンポーネントは、さまざまなコンポーネントの出力を組版するために使用されます。',
|
||||
'このコンポーネントは、さまざまなコンポーネントの出力を組版するために使用されます。1. Jinja2テンプレートをサポートし、最初に入力をオブジェクトに変換してからテンプレートをレンダリングします。2. {parameter}文字列置換を使用する元の方法も同時に保持します',
|
||||
emailComponent: 'メール',
|
||||
emailDescription: '指定されたアドレスにメールを送信',
|
||||
smtpServer: 'SMTPサーバー',
|
||||
|
@ -1073,7 +1073,7 @@ export default {
|
||||
pasteFileLink: 'Dán liên kết tệp',
|
||||
testRun: 'Chạy thử nghiệm',
|
||||
template: 'Mẫu',
|
||||
templateDescription: `Thành phần này được sử dụng để sắp chữ đầu ra của nhiều thành phần khác nhau.`,
|
||||
templateDescription: `Thành phần này được sử dụng để sắp chữ đầu ra của nhiều thành phần khác nhau.1. Hỗ trợ mẫu Jinja2, trước tiên chuyển đầu vào thành đối tượng và sau đó kết xuất mẫu. 2. Phương pháp ban đầu sử dụng thay thế chuỗi {parameter} cũng được giữ lại đồng thời`,
|
||||
arXivTip: `Thành phần này được sử dụng để lấy kết quả tìm kiếm từ https://arxiv.org/. Thông thường, nó hoạt động như một phần bổ sung cho cơ sở tri thức. Top N chỉ định số lượng kết quả tìm kiếm bạn cần điều chỉnh.`,
|
||||
googleTip: `Thành phần này được sử dụng để lấy kết quả tìm kiếm từ https://www.google.com/. Thông thường, nó hoạt động như một phần bổ sung cho cơ sở tri thức. Top N và khóa API SerpApi chỉ định số lượng kết quả tìm kiếm bạn cần điều chỉnh.`,
|
||||
bingTip: `Thành phần này được sử dụng để lấy kết quả tìm kiếm từ https://www.bing.com/. Thông thường, nó hoạt động như một phần bổ sung cho cơ sở tri thức. Top N và khóa đăng ký Bing chỉ định số lượng kết quả tìm kiếm bạn cần điều chỉnh.`,
|
||||
|
@ -1087,7 +1087,7 @@ export default {
|
||||
pasteFileLink: '貼上文件連結',
|
||||
testRun: '試運行',
|
||||
template: '模板轉換',
|
||||
templateDescription: '此元件用於排版各種元件的輸出。 ',
|
||||
templateDescription: '此元件用於排版各種元件的輸出。1、支持Jinja2模板,會先將輸入轉為對象後進行模板渲染2、同時保留原使用{參數}字符串替換的方式',
|
||||
jsonUploadTypeErrorMessage: '請上傳json檔',
|
||||
jsonUploadContentErrorMessage: 'json 檔案錯誤',
|
||||
iterationDescription: `此元件首先透過「分隔符號」將輸入拆分為陣列。
|
||||
|
@ -1107,7 +1107,7 @@ General:实体和关系提取提示来自 GitHub - microsoft/graphrag:基于
|
||||
pasteFileLink: '粘贴文件链接',
|
||||
testRun: '试运行',
|
||||
template: '模板转换',
|
||||
templateDescription: '该组件用于排版各种组件的输出。',
|
||||
templateDescription: '该组件用于排版各种组件的输出。1、支持Jinja2模板,会先将输入转为对象后进行模版渲染2、同时保留原使用{参数}字符串替换的方式',
|
||||
emailComponent: '邮件',
|
||||
emailDescription: '发送邮件到指定邮箱',
|
||||
smtpServer: 'SMTP服务器',
|
||||
|
Loading…
x
Reference in New Issue
Block a user