开发标准流#
通过本文档,你可以学习如何从头开始编写流 YAML 文件来开发标准流。你可以在流 YAML 架构中找到关于流 YAML 架构的更多信息。
流输入数据#
流输入数据是您希望在流中处理的数据。
你可以在流 YAML 文件的 inputs 部分添加流输入。
inputs:
url:
type: string
default: https://www.microsoft.com/en-us/d/xbox-wireless-controller-stellar-shift-special-edition/94fbjc7h0h6h
在创作页面展开“输入”部分时,您可以设置和查看流输入,包括输入架构(名称和类型)和输入值。
对于如上截图所示的 Web 分类示例,流输入是字符串类型的 URL。有关 Python 工具中更多输入类型,请参阅输入类型。
使用不同工具开发流#
在一个流中,你可以使用不同类型的工具。我们现在支持内置工具,如LLM、Python和Prompt,以及第三方工具,如Serp API、向量搜索等。
根据需要添加工具#
你可以在流 yaml 的 nodes 部分添加工具节点。例如,下面的 yaml 展示了如何在流中添加一个 Python 工具节点。
nodes:
- name: fetch_text_content_from_url
type: python
source:
type: code
path: fetch_text_content_from_url.py
inputs:
url: ${inputs.url}
通过选择最顶部的工具卡片,您将向流中添加一个新的工具节点。
编辑工具#
您只需打开源文件进行编辑即可修改工具。例如,我们提供了以下一个简单的 Python 工具代码。
from promptflow.core import tool
# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need
@tool
def my_python_tool(input1: str) -> str:
return 'hello ' + input1
我们还提供了以下 LLM 工具提示。
Please summarize the following text in one paragraph. 100 words.
Do not add any information that is not in the text.
Text: {{text}}
Summary:
当新的工具节点添加到流中时,默认情况下它会以随机名称附加在扁平视图的底部。在每个工具节点卡片顶部都有一个工具栏,用于调整工具节点。你可以向上或向下移动它,也可以删除或重命名它。对于 Python 工具节点,你可以通过点击代码文件来编辑工具代码。对于 LLM 工具节点,你可以通过点击提示文件来编辑工具提示并调整连接、API 等输入参数。
创建连接#
详情请参阅创建必要的连接。
链接你的流 - 将节点连接在一起#
在将节点连接在一起之前,你需要定义并公开一个接口。
定义 LLM 节点接口#
LLM 节点只有一个输出,即 LLM 提供者给出的补全。
至于输入,我们提供了一种模板策略,可以帮助您创建接受不同输入值的参数化提示。您可以使用 {{}}
将输入名称括起来,而不是使用固定文本,以便在运行时替换。我们使用 Jinja 作为模板语言。例如:
Your task is to classify a given url into one of the following types:
Movie, App, Academic, Channel, Profile, PDF or None based on the text content information.
The classification will be based on the url, the webpage text content summary, or both.
Here are a few examples:
{% for ex in examples %}
URL: {{ex.url}}
Text content: {{ex.text_content}}
OUTPUT:
{"category": "{{ex.category}}", "evidence": "{{ex.evidence}}"}
{% endfor %}
For a given URL : {{url}}, and text content: {{text_content}}.
Classify above url to complete the category and indicate evidence.
OUTPUT:
定义 Python 节点接口#
Python 节点可能包含多个输入和输出。定义输入和输出,如下图所示。如果您有多个输出,请记住将其设为字典,以便下游节点可以单独调用每个键。例如:
import json
from promptflow.core import tool
@tool
def convert_to_dict(input_str: str, input_str2: str) -> dict:
try:
print(input_str2)
return json.loads(input_str)
except Exception as e:
print("input is not valid, error: {}".format(e))
return {"category": "None", "evidence": "None"}
链接节点#
接口定义后,您可以使用
${inputs.key} 用于与流输入链接。
${upstream_node_name.output} 用于与单输出上游节点链接。
${upstream_node_name.output.key} 用于与多输出上游节点链接。
下面是将节点链接在一起的常见场景。
场景 1 - 将 LLM 节点与流输入和单输出上游节点链接#
在添加新的 LLM 节点并像定义 LLM 节点接口一样编辑提示文件后,输入部分会创建三个名为url
、examples
和text_content
的输入。
您可以使用${inputs.url}
将 LLM 节点输入与流输入链接。您还可以使用${prepare_examples.output}
和${summarize_text_content.output}
将examples
链接到上游的prepare_examples
节点,将text_content
链接到summarize_text_content
节点。
- name: classify_with_llm
type: llm
source:
type: code
path: classify_with_llm.jinja2
inputs:
deployment_name: text-davinci-003
suffix: ""
max_tokens: 128
temperature: 0.2
top_p: 1
echo: false
presence_penalty: 0
frequency_penalty: 0
best_of: 1
url: ${inputs.url} # Link with flow input
examples: ${prepare_examples.output} # Link LLM node with single-output upstream node
text_content: ${summarize_text_content.output} # Link LLM node with single-output upstream node
在值下拉菜单中,选择${inputs.url}
、${prepare_examples.output}
和${summarize_text_content.output}
,然后在图视图中,您将看到新创建的 LLM 节点已链接到流输入、上游prepare_examples
和summarize_text_content
节点。
当运行流时,节点的url
输入将即时被流输入替换,节点的examples
和text_content
输入将即时被prepare_examples
和summarize_text_content
节点输出替换。
场景 2 - 将 LLM 节点与多输出上游节点链接#
假设我们希望将新创建的 LLM 节点与covert_to_dict
Python 节点链接,该节点的输出是一个包含两个键:category
和evidence
的字典。
您可以将examples
链接到上游covert_to_dict
节点的evidence
输出,方法是${convert_to_dict.output.evidence}
,如下所示:
- name: classify_with_llm
type: llm
source:
type: code
path: classify_with_llm.jinja2
inputs:
deployment_name: text-davinci-003
suffix: ""
max_tokens: 128
temperature: 0.2
top_p: 1
echo: false
presence_penalty: 0
frequency_penalty: 0
best_of: 1
text_content: ${convert_to_dict.output.evidence} # Link LLM node with multi-output upstream node
在值下拉菜单中,选择${convert_to_dict.output}
,然后手动追加evidence
,然后您将在图视图中看到新创建的 LLM 节点已链接到上游convert_to_dict node
。
当运行流时,节点的text_content
输入将即时被来自convert_to_dict node
输出字典的evidence
值替换。
场景 3 - 将 Python 节点与上游节点/流输入链接#
在添加新的 Python 节点并像定义 Python 节点接口一样编辑代码文件后,输入部分会创建两个名为input_str
和input_str2
的输入。链接方式与 LLM 节点相同,使用${flow.input_name}
链接到流输入,或使用${upstream_node_name.output}
链接到上游节点。
- name: prepare_examples
type: python
source:
type: code
path: prepare_examples.py
inputs:
input_str: ${inputs.url} # Link Python node with flow input
input_str2: ${fetch_text_content_from_url.output} # Link Python node with single-output upstream node
当运行流时,节点的input_str
输入将即时被流输入替换,节点的input_str2
输入将即时被fetch_text_content_from_url
节点输出字典替换。
设置流输出#
当流很复杂时,您可以设置流输出并在一个地方检查多个节点的输出,而不是检查每个节点的输出。此外,流输出有助于:
在一个表格中检查批量测试结果。
定义评估接口映射。
设置部署响应架构。
您可以在流 yaml 的 outputs 部分添加流输出。链接方式与 LLM 节点相同,使用${convert_to_dict.output.category}
将category
流输出与上游节点convert_to_dict
的category
值链接。
outputs:
category:
type: string
reference: ${convert_to_dict.output.category}
evidence:
type: string
reference: ${convert_to_dict.output.evidence}
首先定义流输出架构,然后从下拉列表中选择您想要设置为流输出的节点。由于convert_to_dict
的输出是一个带有两个键:category
和evidence
的字典,您需要手动将category
和evidence
分别追加到每个键。然后运行流,过一段时间后,您可以在表格中查看流输出。