Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
In diesem Lernprogrammbeispiel wird die einfache Aufforderung „Klassifiziere diese Abfrage“ mithilfe der MLflow-Prompt-Optimierung, GEPA und GPT-OSS 20B für Klassifizierungsaufgaben optimiert.
Installieren von Abhängigkeiten
%pip install --upgrade mlflow databricks-sdk dspy openai
dbutils.library.restartPython()
Stellen Sie sicher, dass Sie Zugriff auf die Databricks Foundation Model-APIs haben, um dies erfolgreich auszuführen.
import mlflow
import openai
from mlflow.genai.optimize import GepaPromptOptimizer
from mlflow.genai.scorers import Correctness
from databricks_openai import DatabricksOpenAI
# Change the catalog and schema to your catalog and schema
catalog = ""
schema = ""
prompt_registry_name = "qa"
prompt_location = f"{catalog}.{schema}.{prompt_registry_name}"
openai_client = DatabricksOpenAI()
# Register initial prompt
prompt = mlflow.genai.register_prompt(
name=prompt_location,
template="classify this: {{query}}",
)
# Define your prediction function
def predict_fn(query: str) -> str:
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/1")
completion = openai_client.chat.completions.create(
model="databricks-gpt-oss-20b",
# load prompt template using PromptVersion.format()
messages=[{"role": "user", "content": prompt.format(question=query)}],
)
return completion.choices[0].message.content
Testen Sie Ihre Funktion
Beobachten Sie, wie genau das Modell die Eingabe mit einer minimalistischen Eingabeaufforderung klassifizieren kann. Obwohl es genau ist, ist es jedoch nicht auf eine Aufgabe oder einen Anwendungsfall ausgerichtet, den Sie benötigen.
from IPython.display import Markdown
output = predict_fn("The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments.")
Markdown(output[1]['text'])
Optimieren mit Daten
Stellen Sie Daten mit erwarteten Antworten und Fakten bereit, um das Modellverhalten und die Ausgabe auf eine Weise zu optimieren, die Ihren Anwendungsfällen entspricht.
In diesem Fall soll das Modell ein Wort aus einer Auswahl von fünf Wörtern ausgeben. Es sollte nur dieses Wort ohne weitere Erläuterung ausgegeben werden.
# Training data with inputs and expected outputs
dataset = [
{
"inputs": {"query": "The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments."},
"outputs": {"response": "BACKGROUND"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "This paper describes the design and evaluation of Positive Outlook , an online program aiming to enhance the self-management skills of gay men living with HIV ."},
"outputs": {"response": "BACKGROUND"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "This study is designed as a randomised controlled trial in which men living with HIV in Australia will be assigned to either an intervention group or usual care control group ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "The intervention group will participate in the online group program ` Positive Outlook ' ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "The program is based on self-efficacy theory and uses a self-management approach to enhance skills , confidence and abilities to manage the psychosocial issues associated with HIV in daily life ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
},
{
"inputs": {"query": "Participants will access the program for a minimum of 90 minutes per week over seven weeks ."},
"outputs": {"response": "METHODS"},
"expectations": {"expected_facts": ["Classification label must be 'CONCLUSIONS', 'RESULTS', 'METHODS', 'OBJECTIVE', 'BACKGROUND'"]}
}
]
# Optimize the prompt
result = mlflow.genai.optimize_prompts(
predict_fn=predict_fn,
train_data=dataset,
prompt_uris=[prompt.uri],
optimizer=GepaPromptOptimizer(reflection_model="databricks:/databricks-claude-sonnet-4-5"),
scorers=[Correctness(model="databricks:/databricks-gpt-5")],
)
# Use the optimized prompt
optimized_prompt = result.optimized_prompts[0]
print(f"Optimized template: {optimized_prompt.template}")
Überprüfen Sie die Eingabeaufforderung
Öffnen Sie den Link zu Ihrem MLflow-Experiment, und führen Sie die folgenden Schritte aus, damit die Eingabeaufforderungen in Ihrem Experiment angezeigt werden:
- Stellen Sie sicher, dass Ihr Experimenttyp auf KI-Apps und -Agents festgelegt ist.
- Wechseln Sie zur Registerkarte "Eingabeaufforderung"
- Klicken Sie oben rechts auf ein Schema auswählen , und geben Sie dasselbe Schema ein, das Sie oben festgelegt haben, um Ihre Eingabeaufforderung anzuzeigen.
Laden Sie die neue Eingabeaufforderung, und testen Sie es erneut.
Sehen Sie sich an, wie die Eingabeaufforderung aussieht, und laden Sie sie in Ihre vorhergesagte Funktion, um zu sehen, wie unterschiedlich das Modell ausgeführt wird.
from IPython.display import Markdown
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/34")
Markdown(prompt.template)
from IPython.display import Markdown
def predict_fn(query: str) -> str:
prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}/34")
completion = openai_client.chat.completions.create(
model="databricks-gpt-oss-20b",
# load prompt template using PromptVersion.format()
messages=[{"role": "user", "content": prompt.format(query=query)}],
)
return completion.choices[0].message.content
output = predict_fn("The emergence of HIV as a chronic condition means that people living with HIV are required to take more responsibility for the self-management of their condition , including making physical , emotional and social adjustments.")
Markdown(output[1]['text'])
Beispiel-Notebook
Im Folgenden sehen Sie ein runnables Notizbuch, das Eingabeaufforderungen mithilfe von MLflow GenAI Prompt Optimization mit GEPA optimiert und Klassifizierungsaufgaben mit GPT-OSS 20B veranschaulicht.