import osimport randomfrom datetime import datetimefrom uuid import uuid4from scale_gp import SGPClientfrom scale_gp.lib.dataset_builder import DatasetBuilderfrom scale_gp.lib.external_applications import ExternalApplication, ExternalApplicationOutputFlexiblefrom scale_gp.lib.types import data_locatorfrom scale_gp.lib.types.multiturn import MultiturnTestCaseSchemaaccount_id = os.environ["SGP_ACCOUNT_ID"]spec_id = os.environ["SGP_APPLICATION_SPEC_ID"]variant_id = os.environ["SGP_APPLICATION_VARIANT_ID"]def timestamp(): return f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {uuid4()}"client = SGPClient()message_data = [ { "init_messages": [{"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}], }, { "init_messages": [{"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}], }, { "init_messages": [{"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}], },]test_cases = [MultiturnTestCaseSchema(messages=data["init_messages"]) for data in message_data * 10]dataset = DatasetBuilder(client).initialize( account_id=account_id, name=f"Multiturn Dataset {timestamp()}", test_cases=test_cases)print(dataset)conversation_data = [ { "conversation": [ {"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}, {"role": "assistant", "content": "The French Revolution of 1789 was the result of a complex interplay of social, economic, and political factors. Some key elements include...\n"} ], }, { "conversation": [ {"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}, {"role": "assistant", "content": "Napoleon centralized administration and introduced the Civil Code, while consolidating authority and restricting political opposition."}, ], }, { "conversation": [ {"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}, {"role": "assistant", "content": "French colonial rule redirected land and resources toward colonial interests and created unequal political and economic conditions, with lasting effects on local communities."}, ], },]def my_multiturn_app(prompt, test_case): output = None for c in conversation_data: if c["conversation"][0] == prompt['messages'][0]: output = c["conversation"] break if output is None: raise ValueError(f"No entry in conversation_data for prompt: {prompt['messages'][0]}") start = datetime.now().replace(microsecond=5000) traces = [] global_turn_counter = 1 for i in range(1, len(output), 2): prev_convo = output[:i] assistant_message = output[i]["content"] trace = { "node_id": f"Model Call #{global_turn_counter}", "start_timestamp": str(start.isoformat()), "operation_input": {"conversation_history": prev_convo}, "operation_output": {"response": assistant_message}, "duration_ms": random.randint(200, 600), } traces.append(trace) global_turn_counter += 1 return ExternalApplicationOutputFlexible( generation_output={"generated_conversation": output}, trace_spans=traces, metrics={"grammar": round(random.random(), 3), "memory": round(random.random(), 3), "content": round(random.random(), 3)} )app = ExternalApplication(client)app.initialize(application_variant_id=variant_id, application=my_multiturn_app)app.generate_outputs(evaluation_dataset_id=dataset.id, evaluation_dataset_version=1)question_requests = [ { "type": "categorical", "title": "Question 1", "prompt": "Does the conversation make sense", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "categorical", "title": "Question 2", "prompt": "Is the user query answered correctly?", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "free_text", "title": "Question 3", "prompt": "If any messages are incorrect, list their turn number", "account_id": account_id, },]question_ids = [client.questions.create(**question).id for question in question_requests]q_set = client.question_sets.create( name="multiturn question set", question_ids=question_ids, account_id=account_id,)print(q_set)config = client.evaluation_configs.create( account_id=account_id, question_set_id=q_set.id, evaluation_type='human',)print(config)annotation_config_dict = { "messages_loc": data_locator.test_case_output.output["generated_conversation"]}evaluation = client.evaluations.create( account_id=account_id, application_variant_id=variant_id, application_spec_id=spec_id, description="Demo Multiturn Evaluation", name="Multiturn Evaluation", evaluation_config_id=config.id, annotation_config=annotation_config_dict, evaluation_dataset_id=dataset.id, type="builder")print(evaluation)
import osimport randomfrom datetime import datetimefrom uuid import uuid4from scale_gp import SGPClientfrom scale_gp.lib.dataset_builder import DatasetBuilderfrom scale_gp.lib.external_applications import ExternalApplication, ExternalApplicationOutputFlexiblefrom scale_gp.lib.types import data_locatorfrom scale_gp.lib.types.multiturn import MultiturnTestCaseSchemaaccount_id = os.environ["SGP_ACCOUNT_ID"]spec_id = os.environ["SGP_APPLICATION_SPEC_ID"]variant_id = os.environ["SGP_APPLICATION_VARIANT_ID"]def timestamp(): return f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {uuid4()}"client = SGPClient()message_data = [ { "init_messages": [{"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}], }, { "init_messages": [{"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}], }, { "init_messages": [{"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}], },]test_cases = [MultiturnTestCaseSchema(messages=data["init_messages"]) for data in message_data * 10]dataset = DatasetBuilder(client).initialize( account_id=account_id, name=f"Multiturn Dataset {timestamp()}", test_cases=test_cases)print(dataset)conversation_data = [ { "conversation": [ {"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}, {"role": "assistant", "content": "The French Revolution of 1789 was the result of a complex interplay of social, economic, and political factors. Some key elements include...\n"} ], }, { "conversation": [ {"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}, {"role": "assistant", "content": "Napoleon centralized administration and introduced the Civil Code, while consolidating authority and restricting political opposition."}, ], }, { "conversation": [ {"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}, {"role": "assistant", "content": "French colonial rule redirected land and resources toward colonial interests and created unequal political and economic conditions, with lasting effects on local communities."}, ], },]def my_multiturn_app(prompt, test_case): output = None for c in conversation_data: if c["conversation"][0] == prompt['messages'][0]: output = c["conversation"] break if output is None: raise ValueError(f"No entry in conversation_data for prompt: {prompt['messages'][0]}") start = datetime.now().replace(microsecond=5000) traces = [] global_turn_counter = 1 for i in range(1, len(output), 2): prev_convo = output[:i] assistant_message = output[i]["content"] trace = { "node_id": f"Model Call #{global_turn_counter}", "start_timestamp": str(start.isoformat()), "operation_input": {"conversation_history": prev_convo}, "operation_output": {"response": assistant_message}, "duration_ms": random.randint(200, 600), } traces.append(trace) global_turn_counter += 1 return ExternalApplicationOutputFlexible( generation_output={"generated_conversation": output}, trace_spans=traces, metrics={"grammar": round(random.random(), 3), "memory": round(random.random(), 3), "content": round(random.random(), 3)} )app = ExternalApplication(client)app.initialize(application_variant_id=variant_id, application=my_multiturn_app)app.generate_outputs(evaluation_dataset_id=dataset.id, evaluation_dataset_version=1)question_requests = [ { "type": "categorical", "title": "Question 1", "prompt": "Does the conversation make sense", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "categorical", "title": "Question 2", "prompt": "Is the user query answered correctly?", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "free_text", "title": "Question 3", "prompt": "If any messages are incorrect, list their turn number", "account_id": account_id, },]question_ids = [client.questions.create(**question).id for question in question_requests]q_set = client.question_sets.create( name="multiturn question set", question_ids=question_ids, account_id=account_id,)print(q_set)config = client.evaluation_configs.create( account_id=account_id, question_set_id=q_set.id, evaluation_type='human',)print(config)annotation_config_dict = { "messages_loc": data_locator.test_case_output.output["generated_conversation"]}evaluation = client.evaluations.create( account_id=account_id, application_variant_id=variant_id, application_spec_id=spec_id, description="Demo Multiturn Evaluation", name="Multiturn Evaluation", evaluation_config_id=config.id, annotation_config=annotation_config_dict, evaluation_dataset_id=dataset.id, type="builder")print(evaluation)
This recipe’s ExternalApplication helper requires scale-gp==0.1.0a59; see Quick Start. Set SGP_API_KEY and SGP_ACCOUNT_ID. For a private deployment, also set SGP_CLIENT_BASE_URL to its API base URL; otherwise SGPClient() uses the production API. Set SGP_APPLICATION_SPEC_ID and SGP_APPLICATION_VARIANT_ID to an existing application specification and its OFFLINE variant, which receives the locally generated outputs in this recipe.
1. Instantiate Clients
Set up the SGP Client for communication with the API:
Build and initialize a multiturn dataset with predefined messages:
message_data = [ { "init_messages": [{"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}], }, { "init_messages": [{"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}], }, { "init_messages": [{"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}], },]test_cases = [MultiturnTestCaseSchema(messages=data["init_messages"]) for data in message_data]dataset = DatasetBuilder(client).initialize( account_id=account_id, name=f"Multiturn Dataset {timestamp()}", test_cases=test_cases)print(dataset)
3. Define a Multiturn Application
Define the multiturn application with a simulated conversation for each dataset prompt, then initialize it. Each dataset prompt needs a matching entry in conversation_data; otherwise the callback raises an error.
import randomfrom scale_gp.lib.external_applications import ExternalApplication, ExternalApplicationOutputFlexibleconversation_data = [ { "conversation": [ {"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}, {"role": "assistant", "content": "The French Revolution of 1789 was the result of a complex interplay of social, economic, and political factors. Some key elements include...\n"} ], }, { "conversation": [ {"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}, {"role": "assistant", "content": "Napoleon centralized administration and introduced the Civil Code, while consolidating authority and restricting political opposition."}, ], }, { "conversation": [ {"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}, {"role": "assistant", "content": "French colonial rule redirected land and resources toward colonial interests and created unequal political and economic conditions, with lasting effects on local communities."}, ], },]def my_multiturn_app(prompt, test_case): output = None for c in conversation_data: if c["conversation"][0] == prompt['messages'][0]: output = c["conversation"] break if output is None: raise ValueError(f"No entry in conversation_data for prompt: {prompt['messages'][0]}") start = datetime.now().replace(microsecond=5000) traces = [] global_turn_counter = 1 for i in range(1, len(output), 2): prev_convo = output[:i] assistant_message = output[i]["content"] trace = { "node_id": f"Model Call #{global_turn_counter}", "start_timestamp": str(start.isoformat()), "operation_input": {"conversation_history": prev_convo}, "operation_output": {"response": assistant_message}, "duration_ms": random.randint(200, 600), } traces.append(trace) global_turn_counter += 1 return ExternalApplicationOutputFlexible( generation_output={"generated_conversation": output}, trace_spans=traces, metrics={"grammar": round(random.random(), 3), "memory": round(random.random(), 3), "content": round(random.random(), 3)} )app = ExternalApplication(client)app.initialize(application_variant_id=variant_id, application=my_multiturn_app)app.generate_outputs(evaluation_dataset_id=dataset.id, evaluation_dataset_version=1)
4. Create Questions and Question Set
Prepare questions for evaluating the multiturn conversation:
question_requests = [ { "type": "categorical", "title": "Question 1", "prompt": "Does the conversation make sense", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "categorical", "title": "Question 2", "prompt": "Is the user query answered correctly?", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "free_text", "title": "Question 3", "prompt": "If any messages are incorrect, list their turn number", "account_id": account_id, },]question_ids = [client.questions.create(**question).id for question in question_requests]q_set = client.question_sets.create( name="multiturn question set", question_ids=question_ids, account_id=account_id,)print(q_set)
5. Set up Evaluation Configuration and Start Evaluation
import osimport randomfrom datetime import datetimefrom uuid import uuid4from scale_gp import SGPClientfrom scale_gp.lib.dataset_builder import DatasetBuilderfrom scale_gp.lib.external_applications import ExternalApplication, ExternalApplicationOutputFlexiblefrom scale_gp.lib.types import data_locatorfrom scale_gp.lib.types.multiturn import MultiturnTestCaseSchemaaccount_id = os.environ["SGP_ACCOUNT_ID"]spec_id = os.environ["SGP_APPLICATION_SPEC_ID"]variant_id = os.environ["SGP_APPLICATION_VARIANT_ID"]def timestamp(): return f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {uuid4()}"client = SGPClient()message_data = [ { "init_messages": [{"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}], }, { "init_messages": [{"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}], }, { "init_messages": [{"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}], },]test_cases = [MultiturnTestCaseSchema(messages=data["init_messages"]) for data in message_data * 10]dataset = DatasetBuilder(client).initialize( account_id=account_id, name=f"Multiturn Dataset {timestamp()}", test_cases=test_cases)print(dataset)conversation_data = [ { "conversation": [ {"role": "user", "content": "What were the key factors that led to the French Revolution of 1789?"}, {"role": "assistant", "content": "The French Revolution of 1789 was the result of a complex interplay of social, economic, and political factors. Some key elements include...\n"} ], }, { "conversation": [ {"role": "user", "content": "How did Napoleon Bonaparte's rise to power impact French society and politics in the early 19th century?"}, {"role": "assistant", "content": "Napoleon centralized administration and introduced the Civil Code, while consolidating authority and restricting political opposition."}, ], }, { "conversation": [ {"role": "user", "content": "Analyze the economic and social consequences of French colonialism in North Africa during the 19th and early 20th centuries."}, {"role": "assistant", "content": "French colonial rule redirected land and resources toward colonial interests and created unequal political and economic conditions, with lasting effects on local communities."}, ], },]def my_multiturn_app(prompt, test_case): output = None for c in conversation_data: if c["conversation"][0] == prompt['messages'][0]: output = c["conversation"] break if output is None: raise ValueError(f"No entry in conversation_data for prompt: {prompt['messages'][0]}") start = datetime.now().replace(microsecond=5000) traces = [] global_turn_counter = 1 for i in range(1, len(output), 2): prev_convo = output[:i] assistant_message = output[i]["content"] trace = { "node_id": f"Model Call #{global_turn_counter}", "start_timestamp": str(start.isoformat()), "operation_input": {"conversation_history": prev_convo}, "operation_output": {"response": assistant_message}, "duration_ms": random.randint(200, 600), } traces.append(trace) global_turn_counter += 1 return ExternalApplicationOutputFlexible( generation_output={"generated_conversation": output}, trace_spans=traces, metrics={"grammar": round(random.random(), 3), "memory": round(random.random(), 3), "content": round(random.random(), 3)} )app = ExternalApplication(client)app.initialize(application_variant_id=variant_id, application=my_multiturn_app)app.generate_outputs(evaluation_dataset_id=dataset.id, evaluation_dataset_version=1)question_requests = [ { "type": "categorical", "title": "Question 1", "prompt": "Does the conversation make sense", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "categorical", "title": "Question 2", "prompt": "Is the user query answered correctly?", "choices": [{"label": "No", "value": 0}, {"label": "Yes", "value": 1}], "account_id": account_id, }, { "type": "free_text", "title": "Question 3", "prompt": "If any messages are incorrect, list their turn number", "account_id": account_id, },]question_ids = [client.questions.create(**question).id for question in question_requests]q_set = client.question_sets.create( name="multiturn question set", question_ids=question_ids, account_id=account_id,)print(q_set)config = client.evaluation_configs.create( account_id=account_id, question_set_id=q_set.id, evaluation_type='human',)print(config)annotation_config_dict = { "messages_loc": data_locator.test_case_output.output["generated_conversation"]}evaluation = client.evaluations.create( account_id=account_id, application_variant_id=variant_id, application_spec_id=spec_id, description="Demo Multiturn Evaluation", name="Multiturn Evaluation", evaluation_config_id=config.id, annotation_config=annotation_config_dict, evaluation_dataset_id=dataset.id, type="builder")print(evaluation)