Verwendung von prompty in Flow#

Experimentelles Feature

Dies ist ein experimentelles Feature und kann sich jederzeit ändern. Erfahren Sie mehr.

Da Prompty als Funktion aufgerufen werden kann, kann ein Benutzer prompty in einem Flow verwenden, der eine Python-Funktion oder -Klasse sein kann. Dies ermöglicht dem Benutzer, weitere Anpassungslogik mit prompty durchzuführen.

Prompty im Code verwenden#

Beispiel für 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}}

Beispiel-Python-Code

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="")

Als normale Python-Datei ausführen#

Der Benutzer kann den obigen Code als normale Python-Datei ausführen.

python path/to/entry.py

Die Klasse als Flow testen#

Der Benutzer kann auch Promptflow nutzen, um die Klasse als Flow zu testen.

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

Mit dem Konzept des Flows kann der Benutzer eine breite Palette von Aufgaben ausführen, wie z. B.

Im nächsten Abschnitt erfahren Sie mehr über Flows.