Technologic101 commited on
Commit
78927d3
·
1 Parent(s): ac1e0aa

task: working chainlit RAG app

Browse files
Files changed (2) hide show
  1. chainlit.md +13 -9
  2. src/app.py +49 -21
chainlit.md CHANGED
@@ -1,12 +1,16 @@
1
- # ImagineUI
2
 
3
- Welcome! I'm here to help you imagine a new design.
4
 
5
- ## How to use:
6
- 1. Describe the style you're looking for
7
- 2. We'll discuss the design elements and requirements
8
- 3. Add your html structure
9
- 3. When ready, type "!generate" to create the CSS
10
- 4. I'll provide the CSS and we can make adjustments
11
 
12
- Let's create something beautiful!
 
 
 
 
 
 
 
 
1
+ # Welcome to ImagineUI Design Assistant! 👋
2
 
3
+ I'm here to help you find the perfect design style for your website. I can:
4
 
5
+ 1. 🎨 Discuss your design preferences and requirements
6
+ 2. 🔍 Find similar designs from CSS Zen Garden
7
+ 3. 💡 Provide design inspiration and suggestions
 
 
 
8
 
9
+ ## How to use
10
+
11
+ 1. Describe your desired design style
12
+ 2. I'll ask follow-up questions to understand your needs
13
+ 3. When ready, I'll show you matching designs
14
+ 4. We can refine the search based on your feedback
15
+
16
+ Just start chatting and tell me what kind of design you're looking for!
src/app.py CHANGED
@@ -1,33 +1,61 @@
1
  import chainlit as cl
 
 
2
  from chains.design_rag import DesignRAG
3
- from agents.designer import DesignerAgent
4
 
5
- # Initialize these once at module level
6
  design_rag = DesignRAG()
7
- designer = DesignerAgent(rag=design_rag)
 
 
 
 
 
 
 
 
 
 
8
 
9
  @cl.on_chat_start
10
- async def start() -> None:
11
- """Initialize the chat session"""
12
- # Store in user session
13
- cl.user_session.set("designer", designer)
 
 
 
 
 
 
 
 
14
 
15
  # Send welcome message
16
- await cl.Message(
17
- content="Welcome! I'm here to help you imagine a unique design. What style are you looking for?"
18
- ).send()
19
 
20
  @cl.on_message
21
- async def main(message: cl.Message) -> None:
22
- """Handle incoming messages"""
23
- designer = cl.user_session.get("designer")
24
- if designer is None:
25
- # Reinitialize if missing
26
- designer = DesignerAgent(rag=design_rag)
27
- cl.user_session.set("designer", designer)
28
 
29
- # Process message through designer agent
30
- response = await designer.process(message.content)
 
 
 
31
 
32
- # Send response
33
- await cl.Message(content=response).send()
 
 
 
 
 
 
 
 
 
 
1
  import chainlit as cl
2
+ from langchain_openai import ChatOpenAI
3
+ from langchain_core.messages import HumanMessage, SystemMessage
4
  from chains.design_rag import DesignRAG
 
5
 
6
+ # Initialize components
7
  design_rag = DesignRAG()
8
+ conversation_history = []
9
+
10
+ # System message focused on design analysis
11
+ SYSTEM_MESSAGE = """You are a helpful design assistant that finds and explains design examples.
12
+ For every user message, analyze their design preferences and requirements, considering:
13
+ 1. Visual style and aesthetics
14
+ 2. Color preferences and mood
15
+ 3. Layout and structural needs
16
+ 4. Key visual elements
17
+
18
+ First explain how you understand their requirements, then show relevant design examples."""
19
 
20
  @cl.on_chat_start
21
+ async def init():
22
+ # Initialize LLM with streaming
23
+ global llm
24
+ llm = ChatOpenAI(
25
+ model="gpt-4",
26
+ temperature=0,
27
+ streaming=True,
28
+ callbacks=[cl.LangchainCallbackHandler()]
29
+ )
30
+
31
+ # Store system message
32
+ conversation_history.append(SystemMessage(content=SYSTEM_MESSAGE))
33
 
34
  # Send welcome message
35
+ await cl.Message(content="Hello! What kind of design are you looking for?").send()
 
 
36
 
37
  @cl.on_message
38
+ async def main(message: cl.Message):
39
+ # Add user message to history
40
+ conversation_history.append(HumanMessage(content=message.content))
41
+
42
+ # Get LLM's analysis of requirements
43
+ analysis = await llm.ainvoke(conversation_history)
 
44
 
45
+ # Get design examples based on full conversation
46
+ designs = await design_rag.query_similar_designs(
47
+ [msg.content for msg in conversation_history],
48
+ num_examples=3
49
+ )
50
 
51
+ # Combine analysis with designs
52
+ response = f"{analysis.content}\n\nHere are some relevant designs:\n\n{designs}"
53
+
54
+ # Add assistant's response to history
55
+ conversation_history.append(SystemMessage(content=response))
56
+
57
+ # Send response to user
58
+ await cl.Message(content=response).send()
59
+
60
+ if __name__ == "__main__":
61
+ cl.run()