import asyncio
from turnwise import TurnWiseClient, Metric, EvaluationLevel, OutputType
async def main():
# Initialize client
client = TurnWiseClient(
turnwise_api_key="tw_your_key",
openrouter_api_key="sk-or-your_key"
)
await client.verify()
# List datasets
datasets = await client.list_datasets()
if not datasets:
print("No datasets found")
return
dataset_id = datasets[0].id
print(f"Using dataset: {datasets[0].name}")
# Define metric
metric = Metric(
name="Helpfulness",
prompt="""Rate how helpful this response is.
GOAL: @GOAL
RESPONSE: @CURRENT_MESSAGE.output
Score 0-1.""",
evaluation_level=EvaluationLevel.MESSAGE,
output_type=OutputType.PROGRESS,
)
# Run evaluation
def on_progress(progress):
print(f"Progress: {progress.completed}/{progress.total}")
results = await client.evaluate(
dataset_id=dataset_id,
metric=metric,
progress_callback=on_progress
)
# Calculate statistics
scores = [r.get_score() for r in results if r.get_score() is not None]
if scores:
print(f"\nAverage: {sum(scores) / len(scores):.3f}")
print(f"Min: {min(scores):.3f}")
print(f"Max: {max(scores):.3f}")
print(f"\nResults synced to TurnWise!")
print(f"View at: https://app.turnwise.io/datasets/{dataset_id}")
await client.close()
asyncio.run(main())