Spaces:
Runtime error
Runtime error
File size: 12,355 Bytes
e64fe22 bb136f1 e64fe22 fc083dc e64fe22 bb136f1 caba535 e64fe22 bb136f1 e64fe22 bb136f1 caba535 e64fe22 bb136f1 caba535 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 bb136f1 e64fe22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add tools later"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 82 design documents\n",
"Testing RAG retriever with requirements:\n",
"\n",
"Retrieved Designs:\n",
"----------------------------------------\n",
"Generated query: \"vintage classic easy to use grandmother love design\"\n",
"Design 180:\n",
"Description: This design employs a vintage newspaper aesthetic with a classic serif typography that evokes an old-world charm, utilizing sepia-toned paper backgrounds to enhance its nostalgic feel. The layout is text-heavy with a deliberate obfuscation, reflecting a layered collage effect. Its balanced placement keeps the focus central, inviting closer inspection and interaction.\n",
"Categories: Vintage, Nostalgic, Typography, Collage, Editorial\n",
"Visual Characteristics: Sepia tone, Serif typography, Textured background, Layered elements, Central focus\n",
"URL: https://csszengarden.com/180\n",
"\n",
"Design 182:\n",
"Description: The design creatively utilizes a retro theme with vinyl records as the prominent visual element to evoke a sense of nostalgia and classic style, complemented by a muted green color palette that brings harmony and balance. Handwritten and vintage-style typography enhance the retro aesthetic, while background illustrations and decorative elements like stars add whimsy and depth to the composition.\n",
"Categories: Retro, Nostalgic, Music-themed, Decorative, Vintage\n",
"Visual Characteristics: Vinyl Records, Muted Green Palette, Handwritten Typography, Background Illustrations, Decorative Elements\n",
"URL: https://csszengarden.com/182\n",
"\n",
"Design 194:\n",
"Description: This design exudes a minimalist elegance with a muted, earthy color palette and a clean layout, embodying a sense of calm and sophistication. The subtle use of textures and classic serif typography enhances the refined aesthetic, while the centered alignment and generous spacing contribute to a relaxed readability. The incorporation of a delicate floral illustration adds a touch of organic charm, making the design feel both timeless and inviting.\n",
"Categories: Minimalism, Elegant, Organic, Sophisticated, Classic\n",
"Visual Characteristics: Muted Color Palette, Serif Typography, Centered Layout, Generous Spacing, Floral Illustration\n",
"URL: https://csszengarden.com/194\n",
"\n",
"Design 212:\n",
"Description: The design features a retro aesthetic using a muted color palette of browns and creams, creating a nostalgic and vintage feel. The asymmetrical layout and bold typography contribute to the visual hierarchy, guiding the viewer through the content effortlessly. Illustrations with a mid-century modern style add character, merging traditional design elements with contemporary functionality.\n",
"Categories: Retro, Typography, Illustration, Vintage Style, Educational\n",
"Visual Characteristics: Muted Color Palette, Asymmetrical Layout, Bold Typography, Retro Illustrations, Functional Design\n",
"URL: https://csszengarden.com/212\n"
]
}
],
"source": [
"#from tools.design_retriever import DesignRetrieverTool\n",
"from chains.design_rag import DesignRAG\n",
"\n",
"# Initialize DesignRAG and create the tool\n",
"design_rag = DesignRAG()\n",
"#design_retriever = DesignRetrieverTool(rag=design_rag)\n",
"\n",
"test_requirements = {\n",
" \"I want a design that is vintage and classic, something easy to use that a grandmother would love\"\n",
" }\n",
"\n",
"# Test the retriever\n",
"async def test_rag():\n",
" print(\"Testing RAG retriever with requirements:\")\n",
" print(\"\\nRetrieved Designs:\")\n",
" print(\"----------------------------------------\")\n",
" \n",
" results = await design_rag.query_similar_designs(test_requirements, 2)\n",
" print(results)\n",
"\n",
"# Run the test\n",
"await test_rag()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pick a model good for chat and tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RunnableBinding(bound=ChatOpenAI(client=<openai.resources.chat.completions.completions.Completions object at 0x1245518d0>, async_client=<openai.resources.chat.completions.completions.AsyncCompletions object at 0x124548e50>, root_client=<openai.OpenAI object at 0x1108f9310>, root_async_client=<openai.AsyncOpenAI object at 0x115d92090>, model_name='gpt-4o', temperature=0.0, model_kwargs={}, openai_api_key=SecretStr('**********'), streaming=True), kwargs={'tools': [{'type': 'function', 'function': {'name': 'design_retriever', 'description': 'Retrieves similar designs based on style requirements', 'parameters': {'properties': {'requirements': {'type': 'object'}, 'num_examples': {'default': 3, 'type': 'integer'}}, 'required': ['requirements'], 'type': 'object'}}}]}, config={}, config_factories=[])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"model = ChatOpenAI(\n",
" model=\"gpt-4o\", \n",
" temperature=0,\n",
" streaming=True\n",
")\n",
"\n",
"model.bind_tools(tool_belt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize state\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from typing import TypedDict, Annotated\n",
"from langgraph.graph.message import add_messages\n",
"\n",
"class AgentState(TypedDict):\n",
" messages: Annotated[list, add_messages]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set up the nodes and graph\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.prebuilt import ToolNode\n",
"from langgraph.graph import StateGraph, END\n",
"from langchain_core.messages import HumanMessage, SystemMessage\n",
"\n",
"system_message = SystemMessage(content=\"\"\"You are a helpful design assistant that can retrieve and analyze design examples. \n",
"When a user describes their design preferences or requirements, use the design_retriever tool to find relevant examples.\n",
"\n",
"Always use the design_retriever tool when:\n",
"- A user describes specific design requirements\n",
"- A user asks to see similar designs\n",
"- You need to find design inspiration based on user preferences\n",
"\n",
"Format the requirements as a dictionary with these keys:\n",
"- style_description: Brief description of desired visual style\n",
"- key_elements: List of important visual elements\n",
"- color_scheme: Description of colors\n",
"- layout_preferences: Layout requirements\n",
"- mood: Desired emotional impact\n",
"\"\"\")\n",
"\n",
"def call_model(state):\n",
" messages = [system_message] + state[\"messages\"]\n",
" response = model.invoke(messages)\n",
" return {\"messages\" : [response]}\n",
"\n",
"tool_node = ToolNode(tool_belt)\n",
"\n",
"uncompiled_graph = StateGraph(AgentState)\n",
"\n",
"uncompiled_graph.add_node(\"agent\", call_model)\n",
"uncompiled_graph.add_node(\"action\", tool_node)\n",
"uncompiled_graph.set_entry_point(\"agent\")\n",
"\n",
"\n",
"def should_continue(state):\n",
" last_message = state[\"messages\"][-1]\n",
"\n",
" if last_message.tool_calls:\n",
" return \"action\"\n",
"\n",
" return END\n",
"\n",
"uncompiled_graph.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue\n",
")\n",
"uncompiled_graph.add_edge(\"action\", \"agent\")\n",
"\n",
"graph = uncompiled_graph.compile()\n",
"\n",
"#formatted chain\n",
"\n",
"def convert_inputs(input_object):\n",
" return {\"messages\" : [HumanMessage(content=input_object[\"question\"])]}\n",
"\n",
"def parse_output(input_state):\n",
" return input_state[\"messages\"][-1].content\n",
"\n",
"graph_chain = convert_inputs | graph | parse_output\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try it out!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Receiving update from node: 'agent'\n",
"[AIMessage(content=\"Hello! I'm here and ready to help you with any design needs or questions you might have. How can I assist you today?\", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_f9f4fb6dbf'}, id='run-4edce0b5-fdec-4d5d-a4a6-92430faca51a-0')]\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"from langchain_core.messages import HumanMessage\n",
"\n",
"async for chunk in graph.astream({\"messages\" : [HumanMessage(content=\"Hello, how are you?\")]}, stream_mode=\"updates\"):\n",
" for node, values in chunk.items():\n",
" print(f\"Receiving update from node: '{node}'\")\n",
" print(values[\"messages\"])\n",
" print(\"\\n\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see if the RAG tool works."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Receiving update from node: 'agent'\n",
"[AIMessage(content=\"To find a design that matches your description, I'll use the design_retriever tool. Here are the requirements based on your description:\\n\\n- style_description: Monochromatic with subtle accents\\n- key_elements: Grid-based layout, clear hierarchy\\n- color_scheme: Monochromatic with subtle accent colors\\n- layout_preferences: Grid-based\\n- mood: Professional and sophisticated\\n\\nLet's find some examples for you.\", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_f9f4fb6dbf'}, id='run-8fa2e4af-671c-4c75-82fd-a7b3d6237e54-0')]\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"# Create a test message\n",
"from langchain_core.messages import HumanMessage\n",
"\n",
"test_message = HumanMessage(\n",
" content=\"\"\"I want to see a design matching this description: \n",
" I want it to use a monochromatic color scheme with subtle accent colors. \n",
" The layout should be grid-based with clear hierarchy. \n",
" The overall mood should be professional and sophisticated.\"\"\"\n",
")\n",
"\n",
"async for chunk in graph.astream({\"messages\" : [test_message]}, stream_mode=\"updates\"):\n",
" for node, values in chunk.items():\n",
" print(f\"Receiving update from node: '{node}'\")\n",
" print(values[\"messages\"])\n",
" print(\"\\n\\n\")"
]
}
],
"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
}
|