Technologic101 commited on
Commit
fc083dc
·
1 Parent(s): bb136f1

task: get better results from RAG

Browse files
Files changed (2) hide show
  1. src/chains/design_rag.py +25 -12
  2. src/graph.ipynb +12 -59
src/chains/design_rag.py CHANGED
@@ -86,31 +86,44 @@ class DesignRAG:
86
  print(f"Error creating vector store: {str(e)}")
87
  raise
88
 
89
- async def query_similar_designs(self, requirements: Dict, num_examples: int = 5) -> str:
90
- """Find similar designs based on requirements"""
 
 
 
 
 
91
  # Create query generation prompt
92
- query_prompt = ChatPromptTemplate.from_template("""Given these design requirements:
93
 
94
- {requirements}
95
 
96
- Create a search query that will find similar designs. Focus on:
97
- 1. Visual style and aesthetics
98
- 2. Design categories and themes
99
- 3. Key visual characteristics
100
- 4. Overall mood and impact
 
 
101
 
102
- Return only the search query text, no additional explanation.""")
 
 
 
 
 
 
103
 
104
  # Generate optimized search query
105
  query_response = await self.llm.ainvoke(
106
  query_prompt.format(
107
- requirements=json.dumps(requirements, indent=2)
108
  )
109
  )
110
 
111
  print(f"Generated query: {query_response.content}")
112
 
113
- # Get similar documents using the correct method name
114
  docs = self.retriever.get_relevant_documents(
115
  query_response.content,
116
  k=num_examples
 
86
  print(f"Error creating vector store: {str(e)}")
87
  raise
88
 
89
+ async def query_similar_designs(self, conversation_history: List[str], num_examples: int = 5) -> str:
90
+ """Find similar designs based on conversation history
91
+
92
+ Args:
93
+ conversation_history: List of conversation messages
94
+ num_examples: Number of examples to retrieve
95
+ """
96
  # Create query generation prompt
97
+ query_prompt = ChatPromptTemplate.from_template("""Based on this conversation history:
98
 
99
+ {conversation}
100
 
101
+ Extract the key design requirements and create a search query to find similar designs.
102
+ Focus on:
103
+ 1. Visual style and aesthetics mentioned
104
+ 2. Design categories and themes discussed
105
+ 3. Key visual characteristics requested
106
+ 4. Overall mood and impact desired
107
+ 5. Any specific preferences or constraints
108
 
109
+ Return only the search query text, no additional explanation or analysis.""")
110
+
111
+ # Format conversation history
112
+ conversation_text = "\n".join([
113
+ f"{'User' if i % 2 == 0 else 'Assistant'}: {msg}"
114
+ for i, msg in enumerate(conversation_history)
115
+ ])
116
 
117
  # Generate optimized search query
118
  query_response = await self.llm.ainvoke(
119
  query_prompt.format(
120
+ conversation=conversation_text
121
  )
122
  )
123
 
124
  print(f"Generated query: {query_response.content}")
125
 
126
+ # Get similar documents
127
  docs = self.retriever.get_relevant_documents(
128
  query_response.content,
129
  k=num_examples
src/graph.ipynb CHANGED
@@ -22,7 +22,7 @@
22
  },
23
  {
24
  "cell_type": "code",
25
- "execution_count": 1,
26
  "metadata": {},
27
  "outputs": [
28
  {
@@ -31,59 +31,19 @@
31
  "text": [
32
  "Loaded 82 design documents\n",
33
  "Testing RAG retriever with requirements:\n",
34
- "----------------------------------------\n",
35
- "style_description: Modern minimalist design with clean lines\n",
36
- "key_elements: ['clean typography', 'white space', 'grid layout']\n",
37
- "color_scheme: Monochromatic with blue accents\n",
38
- "layout_preferences: Grid-based with clear hierarchy\n",
39
- "mood: Professional and sophisticated\n",
40
- "\n",
41
- "Retrieved Designs:\n",
42
- "----------------------------------------\n",
43
- "Generated query: Modern minimalist design with clean lines AND clean typography AND white space AND grid layout AND Monochromatic with blue accents AND Grid-based layout AND Professional AND sophisticated mood\n"
44
  ]
45
  },
46
  {
47
- "name": "stderr",
48
- "output_type": "stream",
49
- "text": [
50
- "/Users/owner/Desktop/Projects/ai-maker-space/code/ImagineUI/src/chains/design_rag.py:114: LangChainDeprecationWarning: The method `BaseRetriever.get_relevant_documents` was deprecated in langchain-core 0.1.46 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
51
- " docs = self.retriever.get_relevant_documents(\n"
52
- ]
53
- },
54
- {
55
- "name": "stdout",
56
- "output_type": "stream",
57
- "text": [
58
- "Design 003:\n",
59
- "Design 003:\n",
60
- "Description: This design features a serene and calming aesthetic, employing a monochromatic blue-gray color palette that evokes a sense of tranquility. The layout is structured and organized, with a notable emphasis on clean typography and spaciousness. Each section is clearly delineated, contributing to an overall coherent and harmonious visual experience, while the soft textures in the background enhance the serene mood.\n",
61
- "Categories: minimalism, typography, print design, web interface, aesthetic design\n",
62
- "Visual Characteristics: monochromatic palette, serene mood, structured layout, clean typography, spaciousness\n",
63
- "\n",
64
- "Design 020:\n",
65
- "Design 020:\n",
66
- "Description: The design features a serene and professional layout with a muted color palette, emphasizing clean lines and minimalist aesthetics. It balances a centered, text-focused main content area with a sidebar offering functional navigation and supplementary information. The harmonious integration of serene imagery and subtle blue accents promotes a calm and reflective visual experience.\n",
67
- "Categories: minimalist design, web aesthetics, informational layout, professional tone, navigation focus\n",
68
- "Visual Characteristics: muted color palette, serene imagery, clean typography, balanced layout, subtle blue accents\n",
69
- "\n",
70
- "Design 217:\n",
71
- "Design 217:\n",
72
- "Description: The design embraces a minimalist and modern aesthetic with a refreshing color palette of soft blues, creams, and reds, highlighted by a structured layout that organizes content effectively into distinct sections. The typography is bold and clear, drawing attention to key headings while maintaining readability. The use of contrasting colors for different sections adds a dynamic feel to the design, and the sidebar provides easy navigation, enhancing user experience.\n",
73
- "Categories: Modern, Minimalist, Educational, Structured, Interactive\n",
74
- "Visual Characteristics: Bold typography, Contrasting colors, Organized layout, Clean lines, Effective use of white space\n",
75
- "\n",
76
- "Design 004:\n",
77
- "Design 004:\n",
78
- "Description: The design features a structured layout with a calming blue and white color scheme that conveys clarity and modernity. The large, fluid background image adds a touch of elegance, while the clear typography and organized sections contribute to the aesthetic appeal. Highlighted text links in green and orange draw attention, creating a dynamic yet harmonious look.\n",
79
- "Categories: Modern, Web Design, Instructional, Clean, Professional\n",
80
- "Visual Characteristics: Blue and White Palette, Fluid Background Image, Clear Typography, Highlighted Text Links, Structured Layout\n",
81
- "\n",
82
- "Design 203:\n",
83
- "Design 203:\n",
84
- "Description: This design offers a serene and minimalist aesthetic with a focus on clarity and enlightenment. Featuring a calming blue color palette highlighted by a water droplet image, it prioritizes clean typography and well-structured sections. The layout is vertical and spaced, creating a smooth flow from one section to the next, while the consistent use of blue tones reinforces a peaceful mood.\n",
85
- "Categories: Minimalism, Typography, Calm Aesthetic, Water Theme, Serenity\n",
86
- "Visual Characteristics: Blue Color Palette, Vertical Layout, Calming Imagery, Structured Sections, Clean Typography\n"
87
  ]
88
  }
89
  ],
@@ -96,19 +56,12 @@
96
  "#design_retriever = DesignRetrieverTool(rag=design_rag)\n",
97
  "\n",
98
  "test_requirements = {\n",
99
- " \"style_description\": \"Modern minimalist design with clean lines\",\n",
100
- " \"key_elements\": [\"clean typography\", \"white space\", \"grid layout\"],\n",
101
- " \"color_scheme\": \"Monochromatic with blue accents\",\n",
102
- " \"layout_preferences\": \"Grid-based with clear hierarchy\",\n",
103
- " \"mood\": \"Professional and sophisticated\"\n",
104
  "}\n",
105
  "\n",
106
  "# Test the retriever\n",
107
  "async def test_rag():\n",
108
  " print(\"Testing RAG retriever with requirements:\")\n",
109
- " print(\"----------------------------------------\")\n",
110
- " for key, value in test_requirements.items():\n",
111
- " print(f\"{key}: {value}\")\n",
112
  " print(\"\\nRetrieved Designs:\")\n",
113
  " print(\"----------------------------------------\")\n",
114
  " \n",
 
22
  },
23
  {
24
  "cell_type": "code",
25
+ "execution_count": 2,
26
  "metadata": {},
27
  "outputs": [
28
  {
 
31
  "text": [
32
  "Loaded 82 design documents\n",
33
  "Testing RAG retriever with requirements:\n",
34
+ "----------------------------------------\n"
 
 
 
 
 
 
 
 
 
35
  ]
36
  },
37
  {
38
+ "ename": "AttributeError",
39
+ "evalue": "'set' object has no attribute 'items'",
40
+ "output_type": "error",
41
+ "traceback": [
42
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
43
+ "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
44
+ "Cell \u001b[0;32mIn[2], line 25\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28mprint\u001b[39m(results)\n\u001b[1;32m 24\u001b[0m \u001b[38;5;66;03m# Run the test\u001b[39;00m\n\u001b[0;32m---> 25\u001b[0m \u001b[38;5;28;01mawait\u001b[39;00m test_rag()\n",
45
+ "Cell \u001b[0;32mIn[2], line 16\u001b[0m, in \u001b[0;36mtest_rag\u001b[0;34m()\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTesting RAG retriever with requirements:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m----------------------------------------\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key, value \u001b[38;5;129;01min\u001b[39;00m \u001b[43mtest_requirements\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m():\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvalue\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mRetrieved Designs:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
46
+ "\u001b[0;31mAttributeError\u001b[0m: 'set' object has no attribute 'items'"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  ]
48
  }
49
  ],
 
56
  "#design_retriever = DesignRetrieverTool(rag=design_rag)\n",
57
  "\n",
58
  "test_requirements = {\n",
59
+ " \"I'm looking for something modern and space-themed, designed to appeal to adventurers, young people, and science enthusiasts\"\n",
 
 
 
 
60
  "}\n",
61
  "\n",
62
  "# Test the retriever\n",
63
  "async def test_rag():\n",
64
  " print(\"Testing RAG retriever with requirements:\")\n",
 
 
 
65
  " print(\"\\nRetrieved Designs:\")\n",
66
  " print(\"----------------------------------------\")\n",
67
  " \n",