{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Let's create a golden test set with Synthetic Data Generation!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 141 documents\n",
"\n",
"Example document:\n",
"Content: {\"id\": \"135\", \"url\": \"https://www.csszengarden.com/135\", \"css_url\": \"https://www.csszengarden.com/135/135.css\", \"description\": \"This design uses an elegant and traditional aesthetic, with a muted color palette featuring cream and terracotta tones. The use of a patterned background adds texture, while the vertical layout and serif typography provide a classic and sophisticated feel. The design balances text-heavy content with organized sections, making information easily accessible, and its ornate flourishes add a touch of refinement.\", \"categories\": [\"Traditional\", \"Elegant\", \"Text-Heavy\", \"Classic\"], \"visual_characteristics\": [\"Muted Color Palette\", \"Vertical Layout\", \"Serif Typography\", \"Textured Background\", \"Ornate Flourishes\"]}\n",
"Metadata: {'source': '/Users/owner/Desktop/Projects/ai-maker-space/code/ImagineUI/src/data/designs/135/metadata.json', 'seq_num': 1, 'title': '', 'tags': []}\n"
]
}
],
"source": [
"from langchain_community.document_loaders import DirectoryLoader\n",
"from langchain_community.document_loaders import JSONLoader\n",
"import json\n",
"\n",
"def metadata_func(record: dict, metadata: dict) -> dict:\n",
" metadata[\"title\"] = record.get(\"title\", \"\")\n",
" metadata[\"tags\"] = record.get(\"tags\", [])\n",
" return metadata\n",
"\n",
"loader = DirectoryLoader(\n",
" path=\"data/designs\",\n",
" glob=\"**/*.json\",\n",
" loader_cls=JSONLoader,\n",
" loader_kwargs={\n",
" \"jq_schema\": \".\", # This will select the entire JSON object\n",
" \"content_key\": None, # We're not selecting a specific content key\n",
" \"metadata_func\": metadata_func,\n",
" \"text_content\": False\n",
" }\n",
")\n",
"\n",
"documents = loader.load()\n",
"\n",
"print(f\"Loaded {len(documents)} documents\")\n",
"if documents:\n",
" print(\"\\nExample document:\")\n",
" print(f\"Content: {documents[0].page_content}\")\n",
" print(f\"Metadata: {documents[0].metadata}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's create a knowledge graph based on our design metadata."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from ragas.llms import LangchainLLMWrapper\n",
"from ragas.embeddings import LangchainEmbeddingsWrapper\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"# using 4o-mini due to rate limits\n",
"generator_llm = LangchainLLMWrapper(ChatOpenAI(model=\"gpt-4o-mini\"))\n",
"generator_embeddings = LangchainEmbeddingsWrapper(OpenAIEmbeddings())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"KnowledgeGraph(nodes: 141, relationships: 0)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ragas.testset.graph import KnowledgeGraph, Node, NodeType\n",
"\n",
"kg = KnowledgeGraph()\n",
"\n",
"for doc in documents:\n",
" kg.nodes.append(\n",
" Node(\n",
" type=NodeType.DOCUMENT,\n",
" properties={\"page_content\": doc.page_content, \"document_metadata\": doc.metadata}\n",
" )\n",
" )\n",
"\n",
"kg"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "27552c622ca64d2abe4190ce6fb12d1d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying SummaryExtractor: 0%| | 0/141 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7e73db5f494c4db581b14baa2c23d12a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying CustomNodeFilter: 0%| | 0/141 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5e525eaa7bda4c52a876ae87835606a9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying [EmbeddingExtractor, ThemesExtractor, NERExtractor]: 0%| | 0/423 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74e2eae6725142e8922955012318f4d9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying OverlapScoreBuilder: 0%| | 0/1 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"KnowledgeGraph(nodes: 141, relationships: 795)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ragas.testset.transforms import default_transforms, apply_transforms\n",
"\n",
"# why use these new variables?\n",
"transformer_llm = generator_llm\n",
"embedding_model = generator_embeddings\n",
"\n",
"default_transforms = default_transforms(documents=documents, llm=transformer_llm, embedding_model=embedding_model)\n",
"apply_transforms(kg, default_transforms)\n",
"kg"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"kg.save(\"css_zen_garden_design_data\")\n",
"design_kg = KnowledgeGraph.load(\"css_zen_garden_design_data\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay! With out knowledge graph of relationships, we can now generate a test set."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"from ragas.testset import TestsetGenerator\n",
"\n",
"testset_generator = TestsetGenerator(\n",
" knowledge_graph=design_kg,\n",
" llm=generator_llm,\n",
" embedding_model=generator_embeddings\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "82e3c2beea444035a84257bf881f44bd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating personas: 0%| | 0/3 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f2c3a53c3df34620a4b17baec2c74c5a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating Scenarios: 0%| | 0/2 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "18bd0fdb023045f78c6251dec02e9a54",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating Samples: 0%| | 0/10 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_input | \n",
" reference_contexts | \n",
" reference | \n",
" synthesizer_name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" How do ornate flourishes contribute to the ove... | \n",
" [{\"id\": \"135\", \"url\": \"https://www.csszengarde... | \n",
" Ornate flourishes in web design add a touch of... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 1 | \n",
" How does the nature-inspired design in the pro... | \n",
" [{\"id\": \"132\", \"url\": \"https://www.csszengarde... | \n",
" The nature-inspired design features a minimali... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 2 | \n",
" Can you describe the design principles and vis... | \n",
" [{\"id\": \"104\", \"url\": \"https://www.csszengarde... | \n",
" The CSS Zen Garden design emphasizes a harmoni... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 3 | \n",
" How does the use of burgundy in web design con... | \n",
" [{\"id\": \"103\", \"url\": \"https://www.csszengarde... | \n",
" The design features a dark burgundy background... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 4 | \n",
" How vintage aesthetic show in this design? | \n",
" [{\"id\": \"168\", \"url\": \"https://www.csszengarde... | \n",
" The design cleverly combines a vintage aesthet... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 5 | \n",
" How do the designs at CSS Zen Garden utilize a... | \n",
" [<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www.... | \n",
" The designs at CSS Zen Garden utilize a light ... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 6 | \n",
" How do the typography-focused designs in the d... | \n",
" [<1-hop>\\n\\n{\"id\": \"155\", \"url\": \"https://www.... | \n",
" The typography-focused design in the dark them... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 7 | \n",
" What are the illustrative elements used in the... | \n",
" [<1-hop>\\n\\n{\"id\": \"218\", \"url\": \"https://www.... | \n",
" The illustrative elements used in the designs ... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 8 | \n",
" What are the key visual characteristics of the... | \n",
" [<1-hop>\\n\\n{\"id\": \"208\", \"url\": \"https://www.... | \n",
" The design at https://www.csszengarden.com/208... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 9 | \n",
" Wht are the key visual characteristics of the ... | \n",
" [<1-hop>\\n\\n{\"id\": \"001\", \"url\": \"https://www.... | \n",
" The key visual characteristics of the Zen Gard... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_input \\\n",
"0 How do ornate flourishes contribute to the ove... \n",
"1 How does the nature-inspired design in the pro... \n",
"2 Can you describe the design principles and vis... \n",
"3 How does the use of burgundy in web design con... \n",
"4 How vintage aesthetic show in this design? \n",
"5 How do the designs at CSS Zen Garden utilize a... \n",
"6 How do the typography-focused designs in the d... \n",
"7 What are the illustrative elements used in the... \n",
"8 What are the key visual characteristics of the... \n",
"9 Wht are the key visual characteristics of the ... \n",
"\n",
" reference_contexts \\\n",
"0 [{\"id\": \"135\", \"url\": \"https://www.csszengarde... \n",
"1 [{\"id\": \"132\", \"url\": \"https://www.csszengarde... \n",
"2 [{\"id\": \"104\", \"url\": \"https://www.csszengarde... \n",
"3 [{\"id\": \"103\", \"url\": \"https://www.csszengarde... \n",
"4 [{\"id\": \"168\", \"url\": \"https://www.csszengarde... \n",
"5 [<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www.... \n",
"6 [<1-hop>\\n\\n{\"id\": \"155\", \"url\": \"https://www.... \n",
"7 [<1-hop>\\n\\n{\"id\": \"218\", \"url\": \"https://www.... \n",
"8 [<1-hop>\\n\\n{\"id\": \"208\", \"url\": \"https://www.... \n",
"9 [<1-hop>\\n\\n{\"id\": \"001\", \"url\": \"https://www.... \n",
"\n",
" reference \\\n",
"0 Ornate flourishes in web design add a touch of... \n",
"1 The nature-inspired design features a minimali... \n",
"2 The CSS Zen Garden design emphasizes a harmoni... \n",
"3 The design features a dark burgundy background... \n",
"4 The design cleverly combines a vintage aesthet... \n",
"5 The designs at CSS Zen Garden utilize a light ... \n",
"6 The typography-focused design in the dark them... \n",
"7 The illustrative elements used in the designs ... \n",
"8 The design at https://www.csszengarden.com/208... \n",
"9 The key visual characteristics of the Zen Gard... \n",
"\n",
" synthesizer_name \n",
"0 single_hop_specifc_query_synthesizer \n",
"1 single_hop_specifc_query_synthesizer \n",
"2 single_hop_specifc_query_synthesizer \n",
"3 single_hop_specifc_query_synthesizer \n",
"4 single_hop_specifc_query_synthesizer \n",
"5 multi_hop_specific_query_synthesizer \n",
"6 multi_hop_specific_query_synthesizer \n",
"7 multi_hop_specific_query_synthesizer \n",
"8 multi_hop_specific_query_synthesizer \n",
"9 multi_hop_specific_query_synthesizer "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ragas.testset.synthesizers import default_query_distribution, \n",
"\n",
"query_distribution = default_query_distribution(\n",
" kg=design_kg,\n",
" llm=generator_llm\n",
")\n",
"\n",
"testset = testset_generator.generate(testset_size=10, query_distribution=query_distribution)\n",
"testset.to_pandas()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just for comparison, let's see what the simplified version with langchain docs looks like.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "70a315ea06e3444ca84fbac63e51b678",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying SummaryExtractor: 0%| | 0/141 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f1446a35386844e1b414feda2399b2df",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying CustomNodeFilter: 0%| | 0/141 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6247060a50a7484ba9895280782e2235",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying [EmbeddingExtractor, ThemesExtractor, NERExtractor]: 0%| | 0/423 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "32df2719024e4fa395957525ab57abaa",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Applying OverlapScoreBuilder: 0%| | 0/1 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6522481d960e4129aa6f0e0b0b5b2187",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating personas: 0%| | 0/3 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4cc4b04da15d44ef9c76cc7afceb9a00",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating Scenarios: 0%| | 0/2 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "25c28f8088004e398f56cf22495f6714",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating Samples: 0%| | 0/10 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_input | \n",
" reference_contexts | \n",
" reference | \n",
" synthesizer_name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Can you tell me more about what makes this des... | \n",
" [{\"id\": \"135\", \"url\": \"https://www.csszengarde... | \n",
" This design is classic because it employs an e... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 1 | \n",
" What makes this design typography-focused? | \n",
" [{\"id\": \"132\", \"url\": \"https://www.csszengarde... | \n",
" This design is typography-focused because it u... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 2 | \n",
" How does the web design concept illustrated in... | \n",
" [{\"id\": \"104\", \"url\": \"https://www.csszengarde... | \n",
" The web design concept illustrated in the CSS ... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 3 | \n",
" What make this design vintage? | \n",
" [{\"id\": \"103\", \"url\": \"https://www.csszengarde... | \n",
" This design features a vintage theme through i... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 4 | \n",
" How does the vintage aesthetic influence the d... | \n",
" [{\"id\": \"168\", \"url\": \"https://www.csszengarde... | \n",
" The vintage aesthetic in the design is cleverl... | \n",
" single_hop_specifc_query_synthesizer | \n",
"
\n",
" \n",
" 5 | \n",
" What are the key visual characteristics that m... | \n",
" [<1-hop>\\n\\n{\"id\": \"201\", \"url\": \"https://www.... | \n",
" The design at CSS Zen Garden employs a strong ... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 6 | \n",
" What are the visual characteristics of designs... | \n",
" [<1-hop>\\n\\n{\"id\": \"008\", \"url\": \"https://www.... | \n",
" The designs that effectively utilize bold typo... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 7 | \n",
" What are the key visual characteristics of the... | \n",
" [<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www.... | \n",
" The key visual characteristics of the minimali... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 8 | \n",
" How do the subtle background colors and subtle... | \n",
" [<1-hop>\\n\\n{\"id\": \"164\", \"url\": \"https://www.... | \n",
" The design featuring a minimalist layout with ... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
" 9 | \n",
" In what ways do the designs from CSS Zen Garde... | \n",
" [<1-hop>\\n\\n{\"id\": \"109\", \"url\": \"https://www.... | \n",
" The designs from CSS Zen Garden exemplify info... | \n",
" multi_hop_specific_query_synthesizer | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_input \\\n",
"0 Can you tell me more about what makes this des... \n",
"1 What makes this design typography-focused? \n",
"2 How does the web design concept illustrated in... \n",
"3 What make this design vintage? \n",
"4 How does the vintage aesthetic influence the d... \n",
"5 What are the key visual characteristics that m... \n",
"6 What are the visual characteristics of designs... \n",
"7 What are the key visual characteristics of the... \n",
"8 How do the subtle background colors and subtle... \n",
"9 In what ways do the designs from CSS Zen Garde... \n",
"\n",
" reference_contexts \\\n",
"0 [{\"id\": \"135\", \"url\": \"https://www.csszengarde... \n",
"1 [{\"id\": \"132\", \"url\": \"https://www.csszengarde... \n",
"2 [{\"id\": \"104\", \"url\": \"https://www.csszengarde... \n",
"3 [{\"id\": \"103\", \"url\": \"https://www.csszengarde... \n",
"4 [{\"id\": \"168\", \"url\": \"https://www.csszengarde... \n",
"5 [<1-hop>\\n\\n{\"id\": \"201\", \"url\": \"https://www.... \n",
"6 [<1-hop>\\n\\n{\"id\": \"008\", \"url\": \"https://www.... \n",
"7 [<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www.... \n",
"8 [<1-hop>\\n\\n{\"id\": \"164\", \"url\": \"https://www.... \n",
"9 [<1-hop>\\n\\n{\"id\": \"109\", \"url\": \"https://www.... \n",
"\n",
" reference \\\n",
"0 This design is classic because it employs an e... \n",
"1 This design is typography-focused because it u... \n",
"2 The web design concept illustrated in the CSS ... \n",
"3 This design features a vintage theme through i... \n",
"4 The vintage aesthetic in the design is cleverl... \n",
"5 The design at CSS Zen Garden employs a strong ... \n",
"6 The designs that effectively utilize bold typo... \n",
"7 The key visual characteristics of the minimali... \n",
"8 The design featuring a minimalist layout with ... \n",
"9 The designs from CSS Zen Garden exemplify info... \n",
"\n",
" synthesizer_name \n",
"0 single_hop_specifc_query_synthesizer \n",
"1 single_hop_specifc_query_synthesizer \n",
"2 single_hop_specifc_query_synthesizer \n",
"3 single_hop_specifc_query_synthesizer \n",
"4 single_hop_specifc_query_synthesizer \n",
"5 multi_hop_specific_query_synthesizer \n",
"6 multi_hop_specific_query_synthesizer \n",
"7 multi_hop_specific_query_synthesizer \n",
"8 multi_hop_specific_query_synthesizer \n",
"9 multi_hop_specific_query_synthesizer "
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generator = TestsetGenerator(\n",
" llm=generator_llm,\n",
" embedding_model=generator_embeddings\n",
")\n",
"\n",
"testset = generator.generate_with_langchain_docs(\n",
" documents=documents,\n",
" testset_size=10,\n",
" query_distribution=query_distribution\n",
")\n",
"\n",
"testset.to_pandas()\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This isn't ideal, since our RAG use case is to subjectively show examples of good design. Our users won't be asking questions or looking for specific answers.\n",
"\n",
"We may still be able to use evaluators to illustrate direction changes, but what we really need is a test data set showing retrievals of designs that fit the description. This likely needs to be manually curated to ensure the returned design fits and represents a good example."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}