To run experiments in a notebook, use PromptTemplateExperimentationHarness to manage prompt templates and user inputs. You can define a custom evaluation function (eval_fn) to score results, run the harness, and then use .evaluate() to apply your metric and .visualize() to display the results as a table in the notebook.
For built-in evaluation functions, you can use prompttools.utils.similarity for semantic similarity comparisons.
from prompttools.harness import PromptTemplateExperimentationHarness
from typing import Dict
def eval_fn(prompt: str, results: Dict, metadata: Dict) -> float:
# Your logic here, or use a built-in one such as `prompttools.utils.similarity`.
pass
prompt_templates = [
"Answer the following question: {{input}}",
"Respond to the following query: {{input}}"
]
user_inputs = [
{"input": "Who was the first president?"},
{"input": "Who was the first president of India?"}
]
harness = PromptTemplateExperimentationHarness("text-davinci-003",
prompt_templates,
user_inputs)
harness.run()
harness.evaluate("metric_name", eval_fn)
harness.visualize() # The results will be displayed as a table in your notebook