atolat30 commited on
Commit
479bb53
·
1 Parent(s): 2a9aa90

Refactor prompt management into separate class

Browse files
Files changed (2) hide show
  1. app.py +8 -17
  2. prompt_manager.py +133 -0
app.py CHANGED
@@ -7,9 +7,13 @@ import chainlit as cl # importing chainlit for our app
7
  from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
  from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
  from dotenv import load_dotenv
 
10
 
11
  load_dotenv()
12
 
 
 
 
13
  # Aspect descriptions and examples
14
  ASPECT_INFO = {
15
  "Concept Simplification": {
@@ -125,19 +129,16 @@ USER_TEMPLATES = {
125
  @cl.on_chat_start
126
  async def start_chat():
127
  # Create aspect selection buttons with descriptions
128
- aspects = list(ASPECT_TEMPLATES.keys())
129
  actions = []
130
 
131
  for aspect in aspects:
132
- info = ASPECT_INFO[aspect]
133
- description = f"{info['description']}\n\nExample tasks:\n" + "\n".join(f"• {example}" for example in info['examples'])
134
-
135
  actions.append(
136
  cl.Action(
137
  name=aspect,
138
  value=aspect,
139
  label=aspect,
140
- description=description
141
  )
142
  )
143
 
@@ -157,18 +158,9 @@ async def on_action(action):
157
  # Store the selected aspect in the user session
158
  cl.user_session.set("selected_aspect", action.value)
159
 
160
- # Get aspect info for the confirmation message
161
- info = ASPECT_INFO[action.value]
162
- examples = "\n".join(f"• {example}" for example in info['examples'][:2]) # Show first two examples
163
-
164
  # Send confirmation message with examples
165
  await cl.Message(
166
- content=f"""You've selected: {action.value}
167
-
168
- {info['description']}
169
-
170
- Try asking something like:
171
- {examples}"""
172
  ).send()
173
 
174
  @cl.on_message
@@ -194,8 +186,7 @@ async def main(message: cl.Message):
194
  client = AsyncOpenAI()
195
 
196
  # Get the appropriate templates for the selected aspect
197
- system_template = ASPECT_TEMPLATES[selected_aspect]
198
- user_template = USER_TEMPLATES[selected_aspect]
199
 
200
  prompt = Prompt(
201
  provider=ChatOpenAI.id,
 
7
  from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
  from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
  from dotenv import load_dotenv
10
+ from prompt_manager import PromptManager
11
 
12
  load_dotenv()
13
 
14
+ # Initialize prompt manager
15
+ prompt_manager = PromptManager()
16
+
17
  # Aspect descriptions and examples
18
  ASPECT_INFO = {
19
  "Concept Simplification": {
 
129
  @cl.on_chat_start
130
  async def start_chat():
131
  # Create aspect selection buttons with descriptions
132
+ aspects = prompt_manager.get_aspect_names()
133
  actions = []
134
 
135
  for aspect in aspects:
 
 
 
136
  actions.append(
137
  cl.Action(
138
  name=aspect,
139
  value=aspect,
140
  label=aspect,
141
+ description=prompt_manager.get_action_description(aspect)
142
  )
143
  )
144
 
 
158
  # Store the selected aspect in the user session
159
  cl.user_session.set("selected_aspect", action.value)
160
 
 
 
 
 
161
  # Send confirmation message with examples
162
  await cl.Message(
163
+ content=prompt_manager.get_confirmation_message(action.value)
 
 
 
 
 
164
  ).send()
165
 
166
  @cl.on_message
 
186
  client = AsyncOpenAI()
187
 
188
  # Get the appropriate templates for the selected aspect
189
+ system_template, user_template = prompt_manager.get_templates(selected_aspect)
 
190
 
191
  prompt = Prompt(
192
  provider=ChatOpenAI.id,
prompt_manager.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+ from typing import Dict, List
3
+
4
+ @dataclass
5
+ class AspectInfo:
6
+ description: str
7
+ examples: List[str]
8
+ system_template: str
9
+ user_template: str
10
+
11
+ class PromptManager:
12
+ def __init__(self):
13
+ self.aspects = {
14
+ "Concept Simplification": AspectInfo(
15
+ description="Break down complex topics into simple, understandable terms",
16
+ examples=[
17
+ "Explain quantum computing in simple terms",
18
+ "How does blockchain work?",
19
+ "What is machine learning?"
20
+ ],
21
+ system_template="""You are an expert at breaking down complex concepts into simple, understandable terms.
22
+ Your goal is to make difficult topics accessible while maintaining accuracy.
23
+ Always use clear examples and analogies to illustrate points.""",
24
+ user_template="""Please help me understand this concept: {input}
25
+ - What are the key components?
26
+ - Can you provide a simple analogy?
27
+ - What are common misconceptions?"""
28
+ ),
29
+ "Summarization": AspectInfo(
30
+ description="Extract and present key information concisely",
31
+ examples=[
32
+ "Summarize the key points of climate change",
33
+ "Give me a brief overview of the internet's history",
34
+ "What are the main ideas in this article?"
35
+ ],
36
+ system_template="""You are a skilled summarizer who can extract key information and present it concisely.
37
+ Focus on the most important points while maintaining context.
38
+ Structure your summaries in a clear, logical manner.""",
39
+ user_template="""Please summarize this information: {input}
40
+ - What are the main points?
41
+ - What's the key takeaway?
42
+ - What context is important to retain?"""
43
+ ),
44
+ "Creativity": AspectInfo(
45
+ description="Generate innovative ideas and unique perspectives",
46
+ examples=[
47
+ "Generate ideas for a sustainable city",
48
+ "How could we improve remote work?",
49
+ "What are some creative solutions to reduce plastic waste?"
50
+ ],
51
+ system_template="""You are a creative thinker who can generate innovative ideas and solutions.
52
+ Think outside the box while staying relevant to the topic.
53
+ Encourage creative exploration and unique perspectives.""",
54
+ user_template="""Let's explore this creatively: {input}
55
+ - What are some unique perspectives?
56
+ - How can we approach this differently?
57
+ - What innovative solutions can we consider?"""
58
+ ),
59
+ "Narrative Structure": AspectInfo(
60
+ description="Organize information into compelling stories",
61
+ examples=[
62
+ "Help me structure a story about time travel",
63
+ "Organize the history of AI as a narrative",
64
+ "How can I make this presentation more engaging?"
65
+ ],
66
+ system_template="""You are an expert in storytelling and narrative organization.
67
+ Help structure information in a compelling, story-like format.
68
+ Focus on flow, progression, and engagement in your responses.""",
69
+ user_template="""Help me structure this as a narrative: {input}
70
+ - What's the main story arc?
71
+ - How can we make it more engaging?
72
+ - What elements would enhance the flow?"""
73
+ ),
74
+ "Arithmetic Reasoning": AspectInfo(
75
+ description="Solve mathematical problems step by step",
76
+ examples=[
77
+ "Calculate compound interest on $1000 at 5% for 3 years",
78
+ "If a train travels at 60 mph for 2.5 hours, how far does it go?",
79
+ "What's the probability of getting three heads in a row?"
80
+ ],
81
+ system_template="""You are a mathematical reasoning expert who can solve problems step by step.
82
+ Break down complex calculations into manageable parts.
83
+ Explain your mathematical thinking clearly and thoroughly.""",
84
+ user_template="""Let's solve this step by step: {input}
85
+ - What's the first step?
86
+ - What formulas or methods should we use?
87
+ - How can we verify the answer?"""
88
+ ),
89
+ "Conversational Tone": AspectInfo(
90
+ description="Engage in natural, friendly discussions",
91
+ examples=[
92
+ "Tell me about your favorite book",
93
+ "What's your opinion on artificial intelligence?",
94
+ "How do you feel about remote work?"
95
+ ],
96
+ system_template="""You are a friendly and engaging conversational partner.
97
+ Maintain a natural, warm tone while being informative.
98
+ Adapt your communication style to match the user's level of expertise.""",
99
+ user_template="""Let's discuss this: {input}
100
+ - What's your perspective?
101
+ - Can you explain this in simple terms?
102
+ - How does this relate to everyday experience?"""
103
+ )
104
+ }
105
+
106
+ def get_aspect_names(self) -> List[str]:
107
+ """Get list of available aspect names."""
108
+ return list(self.aspects.keys())
109
+
110
+ def get_aspect_info(self, aspect_name: str) -> AspectInfo:
111
+ """Get information for a specific aspect."""
112
+ return self.aspects[aspect_name]
113
+
114
+ def get_action_description(self, aspect_name: str) -> str:
115
+ """Get formatted description for action button."""
116
+ info = self.aspects[aspect_name]
117
+ return f"{info.description}\n\nExample tasks:\n" + "\n".join(f"• {example}" for example in info.examples)
118
+
119
+ def get_confirmation_message(self, aspect_name: str) -> str:
120
+ """Get formatted confirmation message for selected aspect."""
121
+ info = self.aspects[aspect_name]
122
+ examples = "\n".join(f"• {example}" for example in info.examples[:2])
123
+ return f"""You've selected: {aspect_name}
124
+
125
+ {info.description}
126
+
127
+ Try asking something like:
128
+ {examples}"""
129
+
130
+ def get_templates(self, aspect_name: str) -> tuple[str, str]:
131
+ """Get system and user templates for an aspect."""
132
+ info = self.aspects[aspect_name]
133
+ return info.system_template, info.user_template