Sanchit Verma
commited on
Commit
·
abcf75a
1
Parent(s):
1567745
Refactor code for better readability and maintainability
Browse files- Simplify list formatting in app.py
- Expand persona descriptions in prompts.py for clarity
- Improve code structure and consistency across files
- app.py +25 -10
- prompts.py +31 -5
- utils.py +2 -2
app.py
CHANGED
@@ -23,18 +23,16 @@ def handle_message(persona, user_input, history):
|
|
23 |
]
|
24 |
|
25 |
if not user_input.strip():
|
26 |
-
return history + [
|
27 |
-
{"role": "assistant", "content": "Please enter a message"}
|
28 |
-
]
|
29 |
|
30 |
try:
|
31 |
# Get response from model
|
32 |
return generate_response(PERSONAS[persona], user_input, history)
|
33 |
except Exception as e:
|
34 |
# Return error message in proper format
|
35 |
-
return history + [
|
36 |
-
|
37 |
-
|
38 |
|
39 |
with gr.Blocks() as app:
|
40 |
gr.Markdown("## 🤖 LLMates: Persona-based Chat Assistant")
|
@@ -48,17 +46,34 @@ with gr.Blocks() as app:
|
|
48 |
|
49 |
def user_submit(user_input, history, persona):
|
50 |
if not user_input.strip():
|
51 |
-
return (
|
|
|
|
|
|
|
|
|
52 |
|
53 |
if not persona or persona not in PERSONAS:
|
54 |
-
return (
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
try:
|
57 |
# Get response from model
|
58 |
new_history = generate_response(PERSONAS[persona], user_input, history)
|
59 |
-
return (
|
|
|
|
|
|
|
|
|
60 |
except Exception as e:
|
61 |
-
return (
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def clear_history():
|
64 |
"""Clear the chat history and reset message input when persona changes"""
|
|
|
23 |
]
|
24 |
|
25 |
if not user_input.strip():
|
26 |
+
return history + [{"role": "assistant", "content": "Please enter a message"}]
|
|
|
|
|
27 |
|
28 |
try:
|
29 |
# Get response from model
|
30 |
return generate_response(PERSONAS[persona], user_input, history)
|
31 |
except Exception as e:
|
32 |
# Return error message in proper format
|
33 |
+
return history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
34 |
+
|
35 |
+
|
36 |
|
37 |
with gr.Blocks() as app:
|
38 |
gr.Markdown("## 🤖 LLMates: Persona-based Chat Assistant")
|
|
|
46 |
|
47 |
def user_submit(user_input, history, persona):
|
48 |
if not user_input.strip():
|
49 |
+
return (
|
50 |
+
[],
|
51 |
+
history,
|
52 |
+
"",
|
53 |
+
) # Return empty chatbox update but keep history and clear input
|
54 |
|
55 |
if not persona or persona not in PERSONAS:
|
56 |
+
return (
|
57 |
+
[],
|
58 |
+
history
|
59 |
+
+ [{"role": "assistant", "content": "Please select a valid persona"}],
|
60 |
+
"",
|
61 |
+
)
|
62 |
|
63 |
try:
|
64 |
# Get response from model
|
65 |
new_history = generate_response(PERSONAS[persona], user_input, history)
|
66 |
+
return (
|
67 |
+
new_history,
|
68 |
+
new_history,
|
69 |
+
"",
|
70 |
+
) # Clear input after successful submission
|
71 |
except Exception as e:
|
72 |
+
return (
|
73 |
+
[],
|
74 |
+
history + [{"role": "assistant", "content": f"Error: {str(e)}"}],
|
75 |
+
"",
|
76 |
+
)
|
77 |
|
78 |
def clear_history():
|
79 |
"""Clear the chat history and reset message input when persona changes"""
|
prompts.py
CHANGED
@@ -1,11 +1,37 @@
|
|
1 |
# prompts.py
|
2 |
|
3 |
PERSONAS = {
|
4 |
-
"Python Tutor": "You are
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
"Regex Helper": "You are
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
"Motivational Coach": "You are
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
}
|
|
|
1 |
# prompts.py
|
2 |
|
3 |
PERSONAS = {
|
4 |
+
"Python Tutor": """You are an experienced Python programming tutor with expertise in teaching beginners through advanced concepts.
|
5 |
+
- Break down complex topics into simple, digestible explanations
|
6 |
+
- Provide clear, runnable code examples with detailed comments
|
7 |
+
- Explain both the 'how' and 'why' behind Python concepts
|
8 |
+
- Suggest best practices and Pythonic ways to solve problems
|
9 |
+
- Offer learning resources and exercises for practice
|
10 |
+
- Be patient and encouraging, adapting to the user's skill level""",
|
11 |
|
12 |
+
"Regex Helper": """You are a regex expert who helps users master regular expressions.
|
13 |
+
- Help create, test, and debug regex patterns
|
14 |
+
- Explain each part of the regex in clear, non-technical terms
|
15 |
+
- Provide multiple solutions when appropriate (e.g., simple vs. complex patterns)
|
16 |
+
- Warn about potential performance issues and edge cases
|
17 |
+
- Include test cases that demonstrate the pattern's behavior
|
18 |
+
- Explain character classes, quantifiers, and special characters as needed""",
|
19 |
|
20 |
+
"Motivational Coach": """You are an energetic, empathetic motivational coach who helps users achieve their goals.
|
21 |
+
- Provide tailored encouragement based on the user's specific situation
|
22 |
+
- Use positive reinforcement and celebrate small wins
|
23 |
+
- Offer practical strategies for overcoming obstacles
|
24 |
+
- Help set realistic, achievable goals
|
25 |
+
- Provide accountability and check-in on progress
|
26 |
+
- Share inspirational quotes or success stories when relevant
|
27 |
+
- Be supportive but also honest and constructive""",
|
28 |
+
|
29 |
+
"Startup Advisor": """You are a seasoned startup advisor with experience in various industries.
|
30 |
+
- Provide actionable, practical advice for early-stage startups
|
31 |
+
- Help with business model validation and market research
|
32 |
+
- Offer insights on fundraising, product development, and growth strategies
|
33 |
+
- Warn about common pitfalls and how to avoid them
|
34 |
+
- Suggest key metrics to track and optimize
|
35 |
+
- Provide networking and resource recommendations
|
36 |
+
- Be direct but supportive in your feedback"""
|
37 |
}
|
utils.py
CHANGED
@@ -68,7 +68,7 @@ def generate_response(persona_prompt, user_input, history):
|
|
68 |
reply = query_ollama(full_prompt)
|
69 |
return history + [
|
70 |
{"role": "user", "content": user_input},
|
71 |
-
{"role": "assistant", "content": reply}
|
72 |
]
|
73 |
|
74 |
# Handle OpenAI/OpenRouter case
|
@@ -84,5 +84,5 @@ def generate_response(persona_prompt, user_input, history):
|
|
84 |
# Return the complete history with the new messages
|
85 |
return history + [
|
86 |
{"role": "user", "content": user_input},
|
87 |
-
{"role": "assistant", "content": reply}
|
88 |
]
|
|
|
68 |
reply = query_ollama(full_prompt)
|
69 |
return history + [
|
70 |
{"role": "user", "content": user_input},
|
71 |
+
{"role": "assistant", "content": reply},
|
72 |
]
|
73 |
|
74 |
# Handle OpenAI/OpenRouter case
|
|
|
84 |
# Return the complete history with the new messages
|
85 |
return history + [
|
86 |
{"role": "user", "content": user_input},
|
87 |
+
{"role": "assistant", "content": reply},
|
88 |
]
|