{ "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\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
user_inputreference_contextsreferencesynthesizer_name
0How do ornate flourishes contribute to the ove...[{\"id\": \"135\", \"url\": \"https://www.csszengarde...Ornate flourishes in web design add a touch of...single_hop_specifc_query_synthesizer
1How does the nature-inspired design in the pro...[{\"id\": \"132\", \"url\": \"https://www.csszengarde...The nature-inspired design features a minimali...single_hop_specifc_query_synthesizer
2Can you describe the design principles and vis...[{\"id\": \"104\", \"url\": \"https://www.csszengarde...The CSS Zen Garden design emphasizes a harmoni...single_hop_specifc_query_synthesizer
3How does the use of burgundy in web design con...[{\"id\": \"103\", \"url\": \"https://www.csszengarde...The design features a dark burgundy background...single_hop_specifc_query_synthesizer
4How vintage aesthetic show in this design?[{\"id\": \"168\", \"url\": \"https://www.csszengarde...The design cleverly combines a vintage aesthet...single_hop_specifc_query_synthesizer
5How do the designs at CSS Zen Garden utilize a...[<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www....The designs at CSS Zen Garden utilize a light ...multi_hop_specific_query_synthesizer
6How do the typography-focused designs in the d...[<1-hop>\\n\\n{\"id\": \"155\", \"url\": \"https://www....The typography-focused design in the dark them...multi_hop_specific_query_synthesizer
7What are the illustrative elements used in the...[<1-hop>\\n\\n{\"id\": \"218\", \"url\": \"https://www....The illustrative elements used in the designs ...multi_hop_specific_query_synthesizer
8What are the key visual characteristics of the...[<1-hop>\\n\\n{\"id\": \"208\", \"url\": \"https://www....The design at https://www.csszengarden.com/208...multi_hop_specific_query_synthesizer
9Wht are the key visual characteristics of the ...[<1-hop>\\n\\n{\"id\": \"001\", \"url\": \"https://www....The key visual characteristics of the Zen Gard...multi_hop_specific_query_synthesizer
\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\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
user_inputreference_contextsreferencesynthesizer_name
0Can you tell me more about what makes this des...[{\"id\": \"135\", \"url\": \"https://www.csszengarde...This design is classic because it employs an e...single_hop_specifc_query_synthesizer
1What makes this design typography-focused?[{\"id\": \"132\", \"url\": \"https://www.csszengarde...This design is typography-focused because it u...single_hop_specifc_query_synthesizer
2How does the web design concept illustrated in...[{\"id\": \"104\", \"url\": \"https://www.csszengarde...The web design concept illustrated in the CSS ...single_hop_specifc_query_synthesizer
3What make this design vintage?[{\"id\": \"103\", \"url\": \"https://www.csszengarde...This design features a vintage theme through i...single_hop_specifc_query_synthesizer
4How does the vintage aesthetic influence the d...[{\"id\": \"168\", \"url\": \"https://www.csszengarde...The vintage aesthetic in the design is cleverl...single_hop_specifc_query_synthesizer
5What are the key visual characteristics that m...[<1-hop>\\n\\n{\"id\": \"201\", \"url\": \"https://www....The design at CSS Zen Garden employs a strong ...multi_hop_specific_query_synthesizer
6What are the visual characteristics of designs...[<1-hop>\\n\\n{\"id\": \"008\", \"url\": \"https://www....The designs that effectively utilize bold typo...multi_hop_specific_query_synthesizer
7What are the key visual characteristics of the...[<1-hop>\\n\\n{\"id\": \"151\", \"url\": \"https://www....The key visual characteristics of the minimali...multi_hop_specific_query_synthesizer
8How do the subtle background colors and subtle...[<1-hop>\\n\\n{\"id\": \"164\", \"url\": \"https://www....The design featuring a minimalist layout with ...multi_hop_specific_query_synthesizer
9In what ways do the designs from CSS Zen Garde...[<1-hop>\\n\\n{\"id\": \"109\", \"url\": \"https://www....The designs from CSS Zen Garden exemplify info...multi_hop_specific_query_synthesizer
\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 }