|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +from galileo import galileo_context, GalileoMetrics |
| 5 | +from galileo.experiments import create_experiment, get_experiment |
| 6 | +from galileo.projects import get_project, create_project |
| 7 | +from galileo.search import get_sessions |
| 8 | +from galileo.utils.metrics import create_metric_configs |
| 9 | +from galileo.resources.models import MetricSuccess |
| 10 | + |
| 11 | +# Provide the name of a session-level metric |
| 12 | +METRIC_NAME = GalileoMetrics.conversation_quality |
| 13 | + |
| 14 | +# example custom metric name (must be set up in advance) |
| 15 | +# METRIC_NAME = "multi-turn-session-test-metric-apples" |
| 16 | + |
| 17 | +# Load environment variables from the .env file |
| 18 | +from dotenv import load_dotenv |
| 19 | + |
| 20 | +load_dotenv() |
| 21 | + |
| 22 | +# Get the Galileo project |
| 23 | + |
| 24 | +project_name = os.getenv("GALILEO_PROJECT") |
| 25 | +project_obj = get_project(name=project_name) |
| 26 | +if not project_obj: |
| 27 | + project_obj = create_project(project_name) |
| 28 | + |
| 29 | +print(f"Project name: {project_obj.name}, Project ID: {project_obj.id}") |
| 30 | + |
| 31 | +# Create a unique experiment |
| 32 | + |
| 33 | +time_suffix = time.strftime("%m%d-%H%M") |
| 34 | + |
| 35 | +experiment = create_experiment(experiment_name=f"multi-turn-experiment-{time_suffix}", experiment_group="multi-turn examples") |
| 36 | +print(f"Experiment name: {experiment.name}") |
| 37 | + |
| 38 | +galileo_context.init(project=project_obj.name, experiment_id=experiment.id) |
| 39 | + |
| 40 | +# Enable a session-level metric in the created experiment, and get the metric ID |
| 41 | + |
| 42 | +metric_configs, _ = create_metric_configs( |
| 43 | + project_id=project_obj.id, |
| 44 | + run_id=experiment.id, |
| 45 | + metrics=[METRIC_NAME], |
| 46 | +) |
| 47 | +assert len(metric_configs) == 1 |
| 48 | +metric_name = metric_configs[0].name |
| 49 | +metric_id = metric_configs[0].id |
| 50 | +print(f"Metric Name: {metric_name}") |
| 51 | +print(f"Metric ID: {metric_id}") |
| 52 | + |
| 53 | +# Log a multi-turn convo using Galileo context and logger |
| 54 | + |
| 55 | +multi_turn_convo = [ |
| 56 | + {"user": "What is your favorite fruit?", "assistant": "I like blueberries. What about you?"}, |
| 57 | + {"user": "I like strawberries.", "assistant": "Strawberries are great! Do you like blueberries too?"}, |
| 58 | + {"user": "Yes, I do!", "assistant": "Awesome! Blueberries are delicious and packed with nutrients."}, |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +logger = galileo_context.get_logger_instance(project=project_obj.name, experiment_id=experiment.id) |
| 63 | + |
| 64 | +# Create a session and log traces for each turn in the conversation |
| 65 | + |
| 66 | +logger.start_session() |
| 67 | + |
| 68 | +for turn in multi_turn_convo: |
| 69 | + |
| 70 | + logger.start_trace(input=turn["user"], name="User turn") |
| 71 | + logger.add_llm_span( |
| 72 | + input=turn["user"], |
| 73 | + output=turn["assistant"], |
| 74 | + model="gpt-5.4-mini", |
| 75 | + ) |
| 76 | + logger.conclude(output=turn["assistant"]) |
| 77 | + |
| 78 | + |
| 79 | +galileo_context.flush() |
| 80 | + |
| 81 | +# Poll the session-level metric until it's computed |
| 82 | + |
| 83 | +status = "unknown" |
| 84 | +while True: |
| 85 | + |
| 86 | + sessions = get_sessions(project_id=project_obj.id, experiment_id=experiment.id) |
| 87 | + assert len(sessions.records) > 0, "No sessions found for the experiment" |
| 88 | + |
| 89 | + session = sessions.records[0] |
| 90 | + metric = session.metric_info[metric_id] |
| 91 | + |
| 92 | + if isinstance(metric, MetricSuccess): |
| 93 | + print(f"Metric {METRIC_NAME} computed successfully with value: {metric.value}") |
| 94 | + break |
| 95 | + print(f"Metric is not computed yet, retrying in 10 seconds...") |
| 96 | + |
| 97 | + time.sleep(10) |
0 commit comments