在流程中使用 Prompty#

实验性功能

这是一个实验性功能,随时可能更改。了解更多

由于 Prompty 可以作为函数调用,用户可以在 流程 中使用 Prompty,该流程可以是 Python 函数或类。这使用户可以利用 Prompty 进行更多的自定义逻辑。

在代码中消费 Prompty#

Prompty 示例

---
name: Stream Chat
description: Chat with stream enabled.
model:
  api: chat
  configuration:
    type: azure_openai
    azure_deployment: gpt-35-turbo
  parameters:
    temperature: 0.2
    stream: true

inputs:
  first_name:
    type: string
  last_name:
    type: string
  question:
    type: string
  chat_history:
    type: list
sample:
  first_name: John
  last_name: Doe
  question: What is Prompt flow?
  chat_history: [ { "role": "user", "content": "what's the capital of France?" }, { "role": "assistant", "content": "Paris" } ]
---
system:
You are a helpful assistant.
Here is a chat history you had with the user:
{% for item in chat_history %}
{{item.role}}:
{{item.content}}
{% endfor %}

user:
{{question}}

Python 代码示例

from promptflow.tracing import trace
from promptflow.core import AzureOpenAIModelConfiguration, Prompty


class ChatFlow:
    def __init__(self, model_config: AzureOpenAIModelConfiguration):
        self.model_config = model_config

    @trace
    def __call__(
        self, question: str = "What is ChatGPT?", chat_history: list = None
    ) -> str:
        """Flow entry function."""

        chat_history = chat_history or []

        prompty = Prompty.load(
            source="path/to/chat.prompty",
            model={"configuration": self.model_config},
        )

        # output is a generator of string as prompty enabled stream parameter
        output = prompty(question=question, chat_history=chat_history)

        return output


if __name__ == "__main__":
    from promptflow.tracing import start_trace

    start_trace()
    config = AzureOpenAIModelConfiguration(
        connection="open_ai_connection", azure_deployment="gpt-35-turbo"
    )
    flow = ChatFlow(model_config=config)
    result = flow("What's Azure Machine Learning?", [])

    # print result in stream manner
    for r in result:
        print(r, end="")

作为普通 Python 文件运行#

用户可以将上述代码作为普通 Python 文件运行。

python path/to/entry.py

将类作为流程进行测试#

用户还可以利用 promptflow 将该类作为 流程 进行测试。

pf flow test --flow file:ChatFlow --init init.json --inputs question="What is ChatGPT?"

借助 流程 概念,用户可以进一步执行一系列丰富的任务,例如:

查看下一节以了解有关流程的更多信息。