🚀 Gretel to Opik Integration: Creating Q&A Datasets for Model Evaluation

The Story: You need high-quality Q&A datasets to evaluate your AI models, but creating them manually is time-consuming and expensive. This cookbook shows you how to use Gretel’s synthetic data generation to create diverse, realistic Q&A datasets and import them into Opik for model evaluation and optimization.

What you’ll accomplish:

  1. Generate synthetic Q&A data using Gretel Data Designer
  2. Convert it to Opik format
  3. Import into Opik for model evaluation
  4. See your dataset in the Opik UI

📋 Prerequisites

  • Gretel Account: Sign up at gretel.ai and get your API key
  • Comet Account: Sign up at comet.com for Opik access

Let’s get started! 🎯

🛠️ Two Approaches Available

This cookbook demonstrates two methods for generating synthetic data with Gretel:

  1. Data Designer (recommended for custom datasets): Create datasets from scratch with precise control
  2. Safe Synthetics (recommended for existing data): Generate synthetic versions of existing datasets

We’ll start with Data Designer, then show Safe Synthetics as an alternative.

💾 Step 1: Install Required Packages

We’ll install the Gretel client and Opik SDK:

1%pip install gretel-client opik pandas --upgrade --quiet

🔐 Step 2: Authentication Setup

Let’s authenticate with both Gretel and Opik:

1import os
2import getpass
3import opik
4import pandas as pd
5
6print("🔐 Setting up authentication...")
7
8# Set up Gretel API key
9if "GRETEL_API_KEY" not in os.environ:
10 os.environ["GRETEL_API_KEY"] = getpass.getpass("Enter your Gretel API key: ")
11
12# Set up Opik (will prompt for API key if not configured)
13opik.configure()
14
15print("✅ Authentication completed!")

📊 Step 3: Generate Q&A Dataset with Gretel Data Designer

Now we’ll use Gretel Data Designer to generate synthetic Q&A data. We’ll create questions and answers about AI and machine learning:

1from gretel_client.navigator_client import Gretel # Use navigator_client instead!
2from gretel_client.data_designer import columns as C
3from gretel_client.data_designer import params as P
4
5print("🤖 Setting up Q&A dataset generation with Gretel Data Designer...")
6
7# Initialize Data Designer using the navigator_client and factory method
8gretel_navigator = Gretel() # This creates the navigator client
9dd = gretel_navigator.data_designer.new(model_suite="apache-2.0")
10
11# Add topic column (categorical sampler)
12dd.add_column(
13 C.SamplerColumn(
14 name="topic",
15 type=P.SamplerType.CATEGORY,
16 params=P.CategorySamplerParams(
17 values=[
18 "neural networks", "deep learning", "machine learning", "NLP",
19 "computer vision", "reinforcement learning", "AI ethics", "data science"
20 ]
21 )
22 )
23)
24
25# Add difficulty column
26dd.add_column(
27 C.SamplerColumn(
28 name="difficulty",
29 type=P.SamplerType.CATEGORY,
30 params=P.CategorySamplerParams(
31 values=["beginner", "intermediate", "advanced"]
32 )
33 )
34)
35
36# Add question column (LLM-generated)
37dd.add_column(
38 C.LLMTextColumn(
39 name="question",
40 prompt=(
41 "Generate a challenging, specific question about {{ topic }} "
42 "at {{ difficulty }} level. The question should be clear, focused, "
43 "and something a student or practitioner might actually ask."
44 )
45 )
46)
47
48# Add answer column (LLM-generated)
49dd.add_column(
50 C.LLMTextColumn(
51 name="answer",
52 prompt=(
53 "Provide a clear, accurate, and comprehensive answer to this {{ difficulty }}-level "
54 "question about {{ topic }}: '{{ question }}'. The answer should be educational "
55 "and directly address all aspects of the question."
56 )
57 )
58)
59
60print("📊 Generating Q&A dataset...")
61
62# Generate the dataset
63workflow_run = dd.create(num_records=20, wait_until_done=True)
64synthetic_df = workflow_run.dataset.df
65
66print(f"✅ Generated {len(synthetic_df)} Q&A pairs!")
67print(f"\n📊 Dataset shape: {synthetic_df.shape}")
68print(f"📋 Columns: {list(synthetic_df.columns)}")
69
70# Display first few rows
71print("\n📄 Sample data:")
72synthetic_df.head(3)

🔄 Step 4: Convert to Opik Format

Let’s convert our Gretel-generated data to the format Opik expects:

1def convert_to_opik_format(df):
2 """Convert Gretel Q&A data to Opik dataset format"""
3 opik_items = []
4
5 for _, row in df.iterrows():
6 # Create Opik dataset item
7 item = {
8 "input": {
9 "question": row["question"]
10 },
11 "expected_output": row["answer"],
12 "metadata": {
13 "topic": row.get("topic", "AI/ML"),
14 "difficulty": row.get("difficulty", "unknown"),
15 "source": "gretel_navigator"
16 }
17 }
18 opik_items.append(item)
19
20 return opik_items
21
22print("🔄 Converting to Opik format...")
23
24opik_data = convert_to_opik_format(synthetic_df)
25
26print(f"✅ Converted {len(opik_data)} items to Opik format!")
27print("\n📋 Sample converted item:")
28import json
29print(json.dumps(opik_data[0], indent=2))

📤 Step 5: Push Dataset to Opik

Now let’s upload our dataset to Opik where it can be used for model evaluation:

1print("📤 Pushing dataset to Opik...")
2
3# Initialize Opik client
4opik_client = opik.Opik()
5
6# Create the dataset
7dataset_name = "gretel-ai-qa-dataset"
8dataset = opik_client.get_or_create_dataset(
9 name=dataset_name,
10 description="Synthetic Q&A dataset generated using Gretel Data Designer for AI/ML evaluation"
11)
12
13# Insert the data
14dataset.insert(opik_data)
15
16print(f"✅ Successfully created dataset: {dataset.name}")
17print(f"🆔 Dataset ID: {dataset.id}")
18print(f"📊 Total items: {len(opik_data)}")

The trace can now be viewed in the UI:

gretel_opik_integration

✅ Step 6: Verify Your Dataset

Let’s confirm the dataset was created successfully and see how to use it:

1print("🔍 Verifying dataset creation...")
2
3# Try to retrieve the dataset
4try:
5 retrieved_dataset = opik_client.get_dataset(dataset_name)
6 print(f"✅ Dataset verified: {retrieved_dataset.name}")
7 print(f"🆔 Dataset ID: {retrieved_dataset.id}")
8
9 print(f"\n🎯 Next steps:")
10 print(f"1. Go to https://www.comet.com")
11 print(f"2. Navigate to Opik → Datasets")
12 print(f"3. Find your dataset: {dataset_name}")
13 print(f"4. Use it to evaluate your AI models!")
14
15except Exception as e:
16 print(f"❌ Could not verify dataset: {e}")
17 print("Please check your Opik configuration and try again.")

🧪 Step 7: Example Model Evaluation

Here’s how you can use your new dataset to evaluate a model with Opik:

1# Example: Simple Q&A model evaluation
2@opik.track
3def simple_qa_model(input_data):
4 """A simple example model that generates responses to questions"""
5 question = input_data.get('question', '')
6
7 # This is just an example - replace with your actual model
8 if 'neural network' in question.lower():
9 return "A neural network is a computational model inspired by biological neural networks."
10 elif 'machine learning' in question.lower():
11 return "Machine learning is a subset of AI that enables systems to learn from data."
12 else:
13 return "This is a complex AI/ML topic that requires detailed explanation."
14
15print("🧪 Example model evaluation setup:")
16print(f"Dataset: {dataset_name}")
17print("Model: simple_qa_model (replace with your actual model)")
18print("\n💡 To run evaluation, uncomment and run the following code:")
19print("\n🎉 Integration complete! Your Gretel-generated dataset is ready for model evaluation in Opik.")

Congratulations! 🎉 You’ve successfully:

  1. Generated synthetic Q&A data using Gretel Data Designer’s advanced column types
  2. Converted the data to Opik’s expected format
  3. Created a dataset in Opik for model evaluation
  4. Set up the foundation for AI model testing and optimization

The key advantage of using Gretel Data Designer is its modular approach - you can define exactly what data you want using samplers (for categories) and LLM columns (for generated text), giving you precise control over your synthetic dataset.


🔗 Next Steps

  • View your dataset: Go to your Comet workspace → Opik → Datasets
  • Evaluate models: Use the dataset to test your Q&A models
  • Optimize prompts: Use Opik’s Agent Optimizer with your synthetic data
  • Scale up: Generate larger datasets for more comprehensive testing

📚 Resources

Happy evaluating! 🚀

🔄 Alternative: Using Gretel Safe Synthetics

If you have an existing Q&A dataset and want to create a synthetic version, you can use Gretel Safe Synthetics instead:

1%%capture
2%pip install -U gretel-client

Step A: Prepare Sample Data

1import pandas as pd
2from gretel_client.navigator_client import Gretel
3
4# Initialize Gretel client
5gretel = Gretel(api_key="prompt")
6
7# Option 1: Use Gretel's sample ecommerce dataset (has 200+ records)
8my_data_source = "https://gretel-datasets.s3.us-west-2.amazonaws.com/ecommerce_customers.csv"
9
10# Option 2: Create your own Q&A dataset (needs 200+ records for holdout)
11# For demonstration, we'll create a larger dataset
12sample_questions = [
13 'What is machine learning?',
14 'How do neural networks work?',
15 'What is the difference between AI and ML?',
16 'Explain deep learning concepts',
17 'What are the applications of NLP?'
18] * 50 # Repeat to get 250 records
19
20sample_answers = [
21 'Machine learning is a subset of AI that enables systems to learn from data.',
22 'Neural networks are computational models inspired by biological neural networks.',
23 'AI is the broader concept while ML is a specific approach to achieve AI.',
24 'Deep learning uses multi-layer neural networks to model complex patterns.',
25 'NLP applications include chatbots, translation, sentiment analysis, and text generation.'
26] * 50 # Repeat to get 250 records
27
28sample_data = {
29 'question': sample_questions,
30 'answer': sample_answers,
31 'topic': (['ML', 'Neural Networks', 'AI/ML', 'Deep Learning', 'NLP'] * 50),
32 'difficulty': (['beginner', 'intermediate', 'beginner', 'advanced', 'intermediate'] * 50)
33}
34
35original_df = pd.DataFrame(sample_data)
36print(f"📄 Original dataset: {len(original_df)} records")
37print(original_df.head())
38
39# Important: Gretel requires at least 200 records to use holdout
40if len(original_df) < 200:
41 print("⚠️ Warning: Dataset has less than 200 records. Holdout will be disabled.")

Step B: Generate Synthetic Version

1# For quick demo with small dataset - disable holdout and transform
2synthetic_dataset = gretel.safe_synthetic_dataset \
3 .from_data_source(original_df, holdout=None) \
4 .synthesize(num_records=5) \
5 .create()
6
7# Wait for completion and get results
8synthetic_dataset.wait_until_done()
9synthetic_df_safe = synthetic_dataset.dataset.df
10
11print(f"✅ Generated {len(synthetic_df_safe)} synthetic Q&A pairs using Safe Synthetics!")
12print(synthetic_df_safe.head())

Step C: View Results and Quality Report

1# Preview synthetic data
2print("🔍 Synthetic dataset preview:")
3print(synthetic_dataset.dataset.df.head())
4
5# View quality report table
6print("📊 Quality Report Summary:")
7print(synthetic_dataset.report.table)
8
9# View detailed HTML report in notebook
10# synthetic_dataset.report.display_in_notebook()
11
12# Access workflow details
13print("\n🔧 Workflow Configuration:")
14print(synthetic_dataset.config_yaml)
15
16# List all workflow steps
17print("\n📋 Workflow Steps:")
18for step in synthetic_dataset.steps:
19 print(f"- {step.name}")

Step D: Convert to Opik and Upload

1def convert_to_opik_format(df):
2 """Convert Gretel Q&A data to Opik dataset format"""
3 opik_items = []
4
5 for _, row in df.iterrows():
6 # Create Opik dataset item
7 item = {
8 "input": {
9 "question": row["question"]
10 },
11 "expected_output": row["answer"],
12 "metadata": {
13 "topic": row.get("topic", "AI/ML"),
14 "difficulty": row.get("difficulty", "unknown"),
15 "source": "gretel_navigator"
16 }
17 }
18 opik_items.append(item)
19
20 return opik_items
21
22# Initialize Opik client if not already defined
23opik_client = opik.Opik()
24# Convert and upload to Opik (same process as before)
25opik_data_safe = convert_to_opik_format(synthetic_df_safe)
26
27# Create dataset in Opik
28dataset_safe = opik_client.get_or_create_dataset(
29 name="gretel-safe-synthetics-qa-dataset",
30 description="Synthetic Q&A dataset generated using Gretel Safe Synthetics"
31)
32
33dataset_safe.insert(opik_data_safe)
34print(f"✅ Safe Synthetics dataset created: {dataset_safe.name}")

The trace can now be viewed in the UI:

gretel opik integration synthetics

🚨 Important: Dataset Size Requirements

Dataset SizeHoldout SettingExample
< 200 recordsholdout=Nonefrom_data_source(df, holdout=None)
200+ recordsDefault (5%) or customfrom_data_source(df) or from_data_source(df, holdout=0.1)
Large datasetsCustom percentage/countfrom_data_source(df, holdout=250)

🤔 When to Use Which Approach?

Use CaseRecommended ApproachWhy
Creating new datasets from scratchData DesignerMore control, custom column types, guided generation
Synthesizing existing datasetsSafe SyntheticsPreserves statistical relationships, privacy-safe
Custom data structuresData DesignerFlexible column definitions, template system
Production data replicationSafe SyntheticsMaintains data utility while ensuring privacy

Both approaches integrate seamlessly with Opik for model evaluation! 🎯