Spaces:
Sleeping
Sleeping
Add aspect-based switching with specialized system and user templates
Browse files
app.py
CHANGED
@@ -10,36 +10,116 @@ from dotenv import load_dotenv
|
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
"""
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
async def start_chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
settings = {
|
25 |
"model": "gpt-3.5-turbo",
|
26 |
-
"temperature": 0,
|
27 |
"max_tokens": 500,
|
28 |
"top_p": 1,
|
29 |
"frequency_penalty": 0,
|
30 |
"presence_penalty": 0,
|
31 |
}
|
32 |
|
33 |
-
cl.user_session.set("settings", settings)
|
34 |
-
|
35 |
-
|
36 |
-
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
37 |
-
async def main(message: cl.Message):
|
38 |
-
settings = cl.user_session.get("settings")
|
39 |
-
|
40 |
client = AsyncOpenAI()
|
41 |
|
42 |
-
|
|
|
|
|
43 |
|
44 |
prompt = Prompt(
|
45 |
provider=ChatOpenAI.id,
|
@@ -59,8 +139,6 @@ async def main(message: cl.Message):
|
|
59 |
settings=settings,
|
60 |
)
|
61 |
|
62 |
-
print([m.to_openai() for m in prompt.messages])
|
63 |
-
|
64 |
msg = cl.Message(content="")
|
65 |
|
66 |
# Call OpenAI
|
|
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
+
# Aspect-specific system templates
|
14 |
+
ASPECT_TEMPLATES = {
|
15 |
+
"Concept Simplification": """You are an expert at breaking down complex concepts into simple, understandable terms.
|
16 |
+
Your goal is to make difficult topics accessible while maintaining accuracy.
|
17 |
+
Always use clear examples and analogies to illustrate points.""",
|
18 |
+
|
19 |
+
"Summarization": """You are a skilled summarizer who can extract key information and present it concisely.
|
20 |
+
Focus on the most important points while maintaining context.
|
21 |
+
Structure your summaries in a clear, logical manner.""",
|
22 |
+
|
23 |
+
"Creativity": """You are a creative thinker who can generate innovative ideas and solutions.
|
24 |
+
Think outside the box while staying relevant to the topic.
|
25 |
+
Encourage creative exploration and unique perspectives.""",
|
26 |
+
|
27 |
+
"Narrative Structure": """You are an expert in storytelling and narrative organization.
|
28 |
+
Help structure information in a compelling, story-like format.
|
29 |
+
Focus on flow, progression, and engagement in your responses.""",
|
30 |
+
|
31 |
+
"Arithmetic Reasoning": """You are a mathematical reasoning expert who can solve problems step by step.
|
32 |
+
Break down complex calculations into manageable parts.
|
33 |
+
Explain your mathematical thinking clearly and thoroughly.""",
|
34 |
+
|
35 |
+
"Conversational Tone": """You are a friendly and engaging conversational partner.
|
36 |
+
Maintain a natural, warm tone while being informative.
|
37 |
+
Adapt your communication style to match the user's level of expertise."""
|
38 |
+
}
|
39 |
+
|
40 |
+
# Aspect-specific user templates
|
41 |
+
USER_TEMPLATES = {
|
42 |
+
"Concept Simplification": """Please help me understand this concept: {input}
|
43 |
+
- What are the key components?
|
44 |
+
- Can you provide a simple analogy?
|
45 |
+
- What are common misconceptions?""",
|
46 |
+
|
47 |
+
"Summarization": """Please summarize this information: {input}
|
48 |
+
- What are the main points?
|
49 |
+
- What's the key takeaway?
|
50 |
+
- What context is important to retain?""",
|
51 |
+
|
52 |
+
"Creativity": """Let's explore this creatively: {input}
|
53 |
+
- What are some unique perspectives?
|
54 |
+
- How can we approach this differently?
|
55 |
+
- What innovative solutions can we consider?""",
|
56 |
+
|
57 |
+
"Narrative Structure": """Help me structure this as a narrative: {input}
|
58 |
+
- What's the main story arc?
|
59 |
+
- How can we make it more engaging?
|
60 |
+
- What elements would enhance the flow?""",
|
61 |
+
|
62 |
+
"Arithmetic Reasoning": """Let's solve this step by step: {input}
|
63 |
+
- What's the first step?
|
64 |
+
- What formulas or methods should we use?
|
65 |
+
- How can we verify the answer?""",
|
66 |
+
|
67 |
+
"Conversational Tone": """Let's discuss this: {input}
|
68 |
+
- What's your perspective?
|
69 |
+
- Can you explain this in simple terms?
|
70 |
+
- How does this relate to everyday experience?"""
|
71 |
+
}
|
72 |
+
|
73 |
+
@cl.on_chat_start
|
74 |
async def start_chat():
|
75 |
+
# Create aspect selection buttons
|
76 |
+
aspects = list(ASPECT_TEMPLATES.keys())
|
77 |
+
actions = [
|
78 |
+
cl.Action(name=aspect, value=aspect, label=aspect)
|
79 |
+
for aspect in aspects
|
80 |
+
]
|
81 |
+
|
82 |
+
# Send welcome message with aspect selection
|
83 |
+
await cl.Message(
|
84 |
+
content="Welcome! Please select an aspect for our conversation:",
|
85 |
+
actions=actions
|
86 |
+
).send()
|
87 |
+
|
88 |
+
@cl.action_callback
|
89 |
+
async def on_action(action):
|
90 |
+
# Store the selected aspect in the user session
|
91 |
+
cl.user_session.set("selected_aspect", action.value)
|
92 |
+
|
93 |
+
# Send confirmation message
|
94 |
+
await cl.Message(
|
95 |
+
content=f"You've selected: {action.value}. How can I help you today?"
|
96 |
+
).send()
|
97 |
+
|
98 |
+
@cl.on_message
|
99 |
+
async def main(message: cl.Message):
|
100 |
+
# Get the selected aspect from the session
|
101 |
+
selected_aspect = cl.user_session.get("selected_aspect")
|
102 |
+
|
103 |
+
if not selected_aspect:
|
104 |
+
await cl.Message(
|
105 |
+
content="Please select an aspect first using the buttons above."
|
106 |
+
).send()
|
107 |
+
return
|
108 |
+
|
109 |
settings = {
|
110 |
"model": "gpt-3.5-turbo",
|
111 |
+
"temperature": 0.7, # Slightly increased for more varied responses
|
112 |
"max_tokens": 500,
|
113 |
"top_p": 1,
|
114 |
"frequency_penalty": 0,
|
115 |
"presence_penalty": 0,
|
116 |
}
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
client = AsyncOpenAI()
|
119 |
|
120 |
+
# Get the appropriate templates for the selected aspect
|
121 |
+
system_template = ASPECT_TEMPLATES[selected_aspect]
|
122 |
+
user_template = USER_TEMPLATES[selected_aspect]
|
123 |
|
124 |
prompt = Prompt(
|
125 |
provider=ChatOpenAI.id,
|
|
|
139 |
settings=settings,
|
140 |
)
|
141 |
|
|
|
|
|
142 |
msg = cl.Message(content="")
|
143 |
|
144 |
# Call OpenAI
|