在流程中使用prompty#

实验性功能

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

由于Prompty可以作为函数调用,用户可以在flow中使用prompty,这可以是一个python函数或类。 这使得用户可以使用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来测试该类作为一个flow

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

通过flow概念,用户可以进一步执行一系列丰富的任务,例如:

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