Advanced Prompt Specs
Prerequisites
1
import json
import pandas as pd
import requests
FIDDLER_BASE_URL = "https://your_company.fiddler.ai"
PROMPT_SPEC_URL = f"{FIDDLER_BASE_URL}/v3/llm-as-a-judge/prompt-spec"
FIDDLER_HEADERS = {
"Authorization": f"Bearer {FIDDLER_TOKEN}",
"Content-Type": "application/json",
}2
# Load sample news data (using AG News dataset)
df_news = pd.read_parquet(
"hf://datasets/fancyzhx/ag_news/data/test-00000-of-00001.parquet"
).sample(20, random_state=25)
# Map labels to topic names
df_news["original_topic"] = df_news["label"].map({
0: "World", 1: "Sports", 2: "Business", 3: "Sci/Tech"
})
# Summarize the count of each unique topic
print(df_news["original_topic"].value_counts())3
basic_prompt_spec = {
"input_fields": {
"news_summary": {"type": "string"}
},
"output_fields": {
"topic": {
"type": "string",
"choices": ["World", "Sports", "Business", "Sci/Tech"]
},
"reasoning": {"type": "string"}
}
}4
validate_response = requests.post(
f"{PROMPT_SPEC_URL}/validate",
headers=FIDDLER_HEADERS,
json={"prompt_spec": prompt_spec_basic},
)
validate_response.raise_for_status()
print("Status Code:", validate_response.status_code)
print(json.dumps(validate_response.json(), indent=2))5
def get_prediction(prompt_spec, input_data):
predict_response = requests.post(
f"{PROMPT_SPEC_URL}/predict",
headers=FIDDLER_HEADERS,
json={"prompt_spec": prompt_spec, "input_data": input_data},
)
if predict_response.status_code != 200:
print(f"Error ({predict_response.status_code}): {predict_response.text}")
return {"topic": None, "reasoning": None}
return predict_response.json()["prediction"]
print(
json.dumps(
get_prediction(
prompt_spec_basic, {"news_summary": "Wimbledon 2025 is under way!"}
),
indent=2,
)
)6
df_ag_news[["topic", "reasoning"]] = df_ag_news.apply(
lambda row: get_prediction(prompt_spec_basic, {"news_summary": row["text"]}),
axis=1,
result_type="expand",
)7
accuracy = (df_ag_news["original_topic"] == df_ag_news["topic"]).mean()
print(f"Accuracy: {accuracy:.0%}")
df_ag_news.value_counts(subset=["original_topic", "topic"])
for r in df_ag_news[
(df_ag_news["original_topic"] == "Sci/Tech") & (df_ag_news["topic"] != "Sci/Tech")
]["reasoning"]:
print(r)8
prompt_spec_rich = {
"instruction": "Determine the topic of the given news summary.",
"input_fields": {
"news_summary": {
"type": "string",
}
},
"output_fields": {
"topic": {
"type": "string",
"choices": df_ag_news["original_topic"].unique().tolist(),
"description": """Use topic 'Sci/Tech' if the news summary is about a company or business in the tech industry, or if the news summary is about a scientific discovery or research, including health and medicine.
Use topic 'Sports' if the news summary is about a sports event or athlete.
Use topic 'Business' if the news summary is about a company or industry outside of science, technology, or sports.
Use topic 'World' if the news summary is about a global event or issue.
""",
},
"reasoning": {
"type": "string",
"description": "The reasoning behind the predicted topic.",
},
},
}
print(
json.dumps(
get_prediction(prompt_spec_rich, {"news_summary": df_ag_news.loc[267, "text"]}),
indent=2,
)
)9
df_ag_news[["topic", "reasoning"]] = df_ag_news.apply(
lambda row: get_prediction(prompt_spec_rich, {"news_summary": row["text"]}),
axis=1,
result_type="expand",
)
accuracy = (df_ag_news["original_topic"] == df_ag_news["topic"]).mean()
print(f"Accuracy: {accuracy:.0%}")
df_ag_news.value_counts(subset=["original_topic", "topic"])Deploying Your Evaluation to Production
1
from datetime import datetime
import fiddler as fdl
fdl.init(url=FIDDLER_BASE_URL, token=FIDDLER_TOKEN)
PROJECT_NAME = "quickstart_examples" # If the project already exists, the notebook will create the model under the existing project.
MODEL_NAME = "fiddler_news_classifier"
project = fdl.Project.get_or_create(name=PROJECT_NAME)
2
platform_df = df_ag_news.rename(columns={"text": "news_summary"})
platform_df["id"] = platform_df.index3
fiddler_llm_enrichments = [
fdl.Enrichment(
name="news_topic",
enrichment="llm_as_a_judge",
columns=["news_summary"],
config={"prompt_spec": prompt_spec_rich},
)
]
model_spec = fdl.ModelSpec(
inputs=["news_summary"],
metadata=["id", "original_topic"],
custom_features=fiddler_llm_enrichments,
)
llm_application = fdl.Model.from_data(
source=platform_df,
name=MODEL_NAME,
project_id=project.id,
spec=model_spec,
task=fdl.ModelTask.LLM,
max_cardinality=5,
)
llm_application.create()4
production_publish_job = llm_application.publish(platform_df)
# wait for the job to complete
production_publish_job.wait(interval=20)5
llm_application.download_data(
output_dir="test_download",
env_type=fdl.EnvType.PRODUCTION,
start_time=start_time,
end_time=datetime.now(),
columns=[
"id",
"news_summary",
"original_topic",
"FDL news_topic (topic)",
"FDL news_topic (reasoning)",
],
)
# See the original data and the results of LLM-as-a-Judge
fdl_data = pd.read_parquet("test_download/output.parquet")
fdl_data.sample(15)Advanced Prompt Specs Configuration
Schema Design Patterns
Multi-Output Evaluation
Performance Optimization Techniques
Bring-Your-Own-Prompt
Free-Form Output
Guided Choice Output
Guided JSON Output
Additional Documentation
Last updated
Was this helpful?