Prompty Ausgabeformat#
Lernziele - Nach Abschluss dieses Tutorials sollten Sie in der Lage sein:
Zu verstehen, wie das Ausgabeformat von Prompty wie folgt behandelt wird: Text, json_object.
Zu verstehen, wie die Stream-Ausgabe von Prompty konsumiert wird.
0. Abhängige Pakete installieren#
%%capture --no-stderr
%pip install promptflow-devkit
1. Notwendige Verbindungen erstellen#
Verbindungen helfen bei der sicheren Speicherung und Verwaltung von geheimen Schlüsseln oder anderen sensiblen Anmeldeinformationen, die für die Interaktion mit LLMs und anderen externen Tools wie Azure Content Safety erforderlich sind.
Obiges Prompty verwendet die Verbindung open_ai_connection. Wir müssen die Verbindung einrichten, wenn wir sie noch nicht hinzugefügt haben. Nach der Erstellung wird sie in der lokalen Datenbank gespeichert und kann in jedem Flow verwendet werden.
Bereiten Sie Ihre Azure OpenAI-Ressource gemäß dieser Anleitung vor und besorgen Sie sich Ihren api_key, falls Sie noch keinen haben.
from promptflow.client import PFClient
from promptflow.connections import AzureOpenAIConnection, OpenAIConnection
# client can help manage your runs and connections.
pf = PFClient()
try:
conn_name = "open_ai_connection"
conn = pf.connections.get(name=conn_name)
print("using existing connection")
except:
# 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="<your_AOAI_key>",
api_base="<your_AOAI_endpoint>",
api_type="azure",
)
# use this if you have an existing OpenAI account
# connection = OpenAIConnection(
# name=conn_name,
# api_key="<user-input>",
# )
conn = pf.connections.create_or_update(connection)
print("successfully created connection")
print(conn)
2. Prompty-Ausgabe formatieren#
Textausgabe#
Standardmäßig gibt das Prompty die Nachricht der ersten Wahl zurück.
with open("text_format.prompty") as fin:
print(fin.read())
from promptflow.core import Prompty
# load prompty as a flow
f = Prompty.load("text_format.prompty")
# execute the flow as function
question = "What is the capital of France?"
result = f(first_name="John", last_name="Doe", question=question)
# note: the result is a string
result
JSON-Objekt-Ausgabe#
Wenn der Benutzer die folgenden Bedingungen erfüllt, gibt Prompty den Inhalt der ersten Wahl als Wörterbuch zurück.
Definieren Sie
response_formatalstype: json_objectin den Parametern.Geben Sie das JSON-Format der Rückgabe in der Vorlage an.
Hinweis: response_format ist kompatibel mit GPT-4 Turbo und allen GPT-3.5 Turbo-Modellen neuer als gpt-3.5-turbo-1106. Weitere Details finden Sie in diesem Dokument.
with open("json_format.prompty") as fin:
print(fin.read())
from promptflow.core import Prompty
# load prompty as a flow
f = Prompty.load("json_format.prompty")
# execute the flow as function
question = "What is the capital of France?"
result = f(first_name="John", last_name="Doe", question=question)
# note: the result is a dict
result
Alle Auswahlmöglichkeiten#
Wenn der Benutzer die Antwort als all konfiguriert, gibt Prompty die rohe LLM-Antwort zurück, die alle Auswahlmöglichkeiten enthält.
with open("all_response.prompty") as fin:
print(fin.read())
from promptflow.core import Prompty
# load prompty as a flow
f = Prompty.load("all_response.prompty")
# execute the flow as function
question = "What is the capital of France?"
result = f(first_name="John", last_name="Doe", question=question)
# note: the result is a ChatCompletion object
print(result.choices[0])
Streaming-Ausgabe#
Wenn stream=true in den Parametern eines Prompts konfiguriert ist, dessen Ausgabeformat Text ist, gibt das Promptflow SDK einen Generator-Typ zurück, dessen Elemente der Inhalt jedes Chunks sind.
with open("stream_output.prompty") as fin:
print(fin.read())
from promptflow.core import Prompty
# load prompty as a flow
f = Prompty.load("stream_output.prompty")
# execute the flow as function
question = "What's the steps to get rich?"
result = f(question=question)
for item in result:
print(item, end="")
Hinweise: Wenn stream=True ist, und das Ausgabeformat json_object ist oder die Antwort all ist, wird die LLM-Antwort direkt zurückgegeben. Weitere Details zur Behandlung von Stream-Antworten finden Sie in diesem Dokument.
Batch-Lauf mit Textausgabe#
from promptflow.client import PFClient
data = "./data.jsonl" # path to the data file
# create run with the flow and data
pf = PFClient()
base_run = pf.run(
flow="text_format.prompty",
data=data,
column_mapping={
"question": "${data.question}",
},
stream=True,
)
details = pf.get_details(base_run)
details.head(10)
Batch-Lauf mit Stream-Ausgabe#
from promptflow.client import PFClient
data = "./data.jsonl" # path to the data file
# create run with the flow and data
pf = PFClient()
base_run = pf.run(
flow="stream_output.prompty",
data=data,
column_mapping={
"question": "${data.question}",
},
stream=True,
)
details = pf.get_details(base_run)
details.head(10)

