dify-docs/jp/guides/workflow/node/template.md

45 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# テンプレート変換
### 定義
Jinja2のPythonテンプレート言語を使って、データ変換やテキスト処理などを柔軟に行うことができます。
### Jinjaとは
> Jinjaは、高速で表現力豊かで拡張可能なテンプレートエンジンです。
>
> Jinja は、速く、表現力があり、拡張可能なテンプレートエンジンです。
—— [https://jinja.palletsprojects.com/en/3.1.x/](https://jinja.palletsprojects.com/en/3.1.x/)
### シーン
テンプレートードを使うことで、強力なPythonテンプレート言語であるJinja2を用いて、ワークフロー内で軽量かつ柔軟なデータ変換が可能になります。これは、テキスト処理やJSON変換などのシナリオに適しています。例えば、前のステップからの変数を柔軟にフォーマットして結合し、単一のテキスト出力を作成することができます。これは、複数のデータソースの情報を特定のフォーマットにまとめ、後続のステップの要件を満たすのに非常に適しています。
**例1**複数の入力(記事のタイトル、紹介、内容)を一つの完全なテキストに結合する
<figure><img src="../../../.gitbook/assets/image (158).png" alt="" width="375"><figcaption><p>テキストの結合</p></figcaption></figure>
**例2**ナレッジリトリーバルードで取得した情報およびその関連メタデータを、構造化されたMarkdown形式にまとめる
{% code fullWidth="false" %}
```Plain
{% raw %}
{% for item in chunks %}
### Chunk {{ loop.index }}.
### Similarity: {{ item.metadata.score | default('N/A') }}
#### {{ item.title }}
##### Content
{{ item.content | replace('\n', '\n\n') }}
---
{% endfor %}
{% endraw %}
```
{% endcode %}
<figure><img src="../../../.gitbook/assets/image (159).png" alt=""><figcaption><p>ナレッジリトリーバルードの出力をMarkdownに変換</p></figcaption></figure>
Jinjaの[公式ドキュメント](https://jinja.palletsprojects.com/en/3.1.x/templates/)を参考にして、さまざまなタスクを実行するためのより複雑なテンプレートを作成することができます。