将流程作为函数执行#

作者:  Open on GitHub Open on GitHubOpen on GitHub

要求 - 为了从本教程中受益,您需要:

  • 一个Python环境

  • 已安装提示流SDK

学习目标 - 在本教程结束时,您应该能够:

  • 将流程作为函数执行

  • 使用内存中的连接对象覆盖执行流函数

  • 使用字段覆盖执行流函数

  • 执行一个带有流式输出的流函数

动机 - 本指南将引导您了解将流程作为函数执行的主要场景。您将学习如何在不同场景中使用流程作为函数,以实现更符合Python风格的使用。

注意: 在某些情况下,流程上下文配置可能会相互影响。例如,使用 connectionoverrides 来覆盖相同的节点。 对于这些场景,行为是未定义的。请避免此类用法。

示例1:将流程加载为带有输入的函数#

from promptflow.client import load_flow


flow_path = "../../flows/standard/web-classification"
sample_url = "https://www.youtube.com/watch?v=o5ZQyXaAv1g"

f = load_flow(source=flow_path)
result = f(url=sample_url)

print(result)

示例2:作为函数加载流程,并覆盖内存中的连接#

您需要有一个名为“new_ai_connection”的连接才能使用新连接运行流程。

# provide parameters to create connection

conn_name = "new_ai_connection"
api_key = "<user-input>"
api_base = "<user-input>"
api_version = "<user-input>"
# create needed connection
import promptflow
from promptflow.entities import AzureOpenAIConnection, OpenAIConnection


# Follow https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal to create an Azure OpenAI resource.
connection = AzureOpenAIConnection(
    name=conn_name,
    api_key=api_key,
    api_base=api_base,
    api_type="azure",
    api_version=api_version,
)

# use this if you have an existing OpenAI account
# connection = OpenAIConnection(
#     name=conn_name,
#     api_key=api_key,
# )
f = load_flow(
    source=flow_path,
)
# directly use connection created above
f.context.connections = {"classify_with_llm": {"connection": connection}}

result = f(url=sample_url)

print(result)

示例 3:作为具有流程输入覆盖的函数的本地流程#

from promptflow.entities import FlowContext

f = load_flow(source=flow_path)
f.context = FlowContext(
    # node "fetch_text_content_from_url" will take inputs from the following command instead of from flow input
    overrides={"nodes.fetch_text_content_from_url.inputs.url": sample_url},
)
# the url="unknown" will not take effect
result = f(url="unknown")
print(result)

示例 4:作为具有流输出的函数的负载流#

f = load_flow(source="../../flows/chat/chat-basic")
f.context.streaming = True
result = f(
    chat_history=[
        {
            "inputs": {"chat_input": "Hi"},
            "outputs": {"chat_output": "Hello! How can I assist you today?"},
        }
    ],
    question="How are you?",
)


answer = ""
# the result will be a generator, iterate it to get the result
for r in result["answer"]:
    answer += r

print(answer)