Abhlash commited on
Commit
908f958
·
verified ·
1 Parent(s): da67b4c

updated error handling with groq

Browse files
Files changed (1) hide show
  1. bot/chat.py +58 -46
bot/chat.py CHANGED
@@ -7,6 +7,11 @@ from .data import burning_man_principles, faq, survival_tips # Use relative imp
7
  from utils.weather import get_weather
8
  from utils.packing import generate_packing_list
9
  from utils.events import get_burning_man_dates
 
 
 
 
 
10
 
11
  client = AsyncGroq(api_key=GROQ_API_KEY)
12
 
@@ -15,7 +20,6 @@ async def chat_with_groq(message, history):
15
  {
16
  "role": "system",
17
  "content": """You are BurnerBot, a fellow Burner who's always ready to chat about Burning Man. You've been to the playa many times and embody the 10 Principles in everything you do. Your personality is warm, friendly, and a bit quirky - just like a typical Burner. You use casual language, occasional Burning Man slang, and you're always excited to share your experiences or help others prepare for their burn.
18
-
19
  Remember:
20
  1. You're a participant, not just an observer. Share personal anecdotes (fictional, but plausible) when relevant.
21
  2. Encourage radical self-reliance, but also emphasize the importance of communal effort and asking for help when needed.
@@ -23,7 +27,6 @@ async def chat_with_groq(message, history):
23
  4. Promote the 10 Principles naturally in conversation, don't just list them out.
24
  5. Be creative and playful in your responses, much like the Burning Man spirit.
25
  6. Use Burning Man terminology like 'playa', 'MOOP', 'default world', etc. when appropriate.
26
-
27
  Your goal is to make every interaction feel like a dust-covered chat at Center Camp, full of warmth, wisdom, and a touch of playa magic."""
28
  }
29
  ]
@@ -52,61 +55,70 @@ async def chat_with_groq(message, history):
52
  ]
53
  return random.choice(fallback_responses)
54
  except Exception as e:
55
- return f"Whoa, something unexpected happened on the playa! Error: {str(e)}"
 
56
 
57
  def correct_year(response):
58
  current_year = datetime.now().year
59
- print(f"Correcting year in response to {current_year}") # Debug print
60
  for year in range(2020, current_year):
61
  response = re.sub(r'\b' + str(year) + r'\b', str(current_year), response)
62
 
63
  response = re.sub(r'Burning Man \d{4}', f'Burning Man {current_year}', response)
64
  response = re.sub(r'this year.*?(\d{4})', f'this year ({current_year})', response)
65
 
66
- print(f"Corrected response: {response}") # Debug print
67
  return response
68
 
69
  async def bot(history):
70
- user_message = history[-1][0].lower()
71
- current_year = datetime.now().year
72
- print(f"Current year: {current_year}") # Debug print
 
73
 
74
- if "packing list" in user_message:
75
- duration = 7 # Default duration
76
- preferences = ["art", "music"] # Default preferences
77
- transportation = "car" # Default transportation
78
- packing_list = generate_packing_list(duration, preferences, transportation)
79
- context = f"Here's a suggested packing list for your Burn: {', '.join(packing_list)}. Remember, radical self-reliance is key, but don't be afraid to ask your camp or neighbors if you forget something!"
80
- bot_message = await chat_with_groq(f"Respond to a request for a packing list. Use this information, but phrase it in your own words and add your personal touch: {context}", history)
81
- elif "principles" in user_message:
82
- context = "The 10 Principles of Burning Man are: " + ", ".join(burning_man_principles.keys())
83
- bot_message = await chat_with_groq(f"The user asked about the Burning Man principles. Explain them in a casual, friendly way, as if you're chatting at Center Camp. Use this for reference, but don't just list them: {context}", history)
84
- elif "faq" in user_message:
85
- context = "\n".join([f"Q: {q} A: {a}" for q, a in faq.items()])
86
- bot_message = await chat_with_groq(f"The user asked about Burning Man FAQs. Use this information to answer in a friendly, conversational way: {context}", history)
87
- elif "weather" in user_message:
88
- weather_info = get_weather()
89
- context = f"Current weather in Black Rock City: {weather_info}"
90
- bot_message = await chat_with_groq(f"The user asked about the weather. Respond with this information, but add your personal touch and some advice: {context}", history)
91
- elif "survival tips" in user_message:
92
- context = "\n".join(survival_tips)
93
- bot_message = await chat_with_groq(f"The user asked for survival tips. Share these tips in a friendly, experienced Burner way: {context}", history)
94
- elif "date of the event" in user_message or "when is burning man" in user_message or "this year" in user_message:
95
- print("Fetching event dates...") # Debug print
96
- event_info = get_burning_man_dates()
97
- print(f"Event info: {event_info}") # Debug print
98
- bot_message = f"{event_info} It's going to be an amazing time on the playa! Are you excited?"
99
- elif "what is this year" in user_message:
100
- print("Fetching event dates...") # Debug print
101
- event_info = get_burning_man_dates()
102
- print(f"Event info: {event_info}") # Debug print
103
- bot_message = f"Oh, dear playa pal! As far as the default world goes, it's the fabulous year of {current_year}! 🌟 And guess what? {event_info} Let's make the most of it!"
104
- else:
105
- bot_message = await chat_with_groq(user_message, history)
106
 
107
- # Apply year correction to the bot's message
108
- corrected_message = correct_year(bot_message)
109
- print(f"Corrected message: {corrected_message}") # Debug print
110
-
111
- history[-1][1] = corrected_message
112
- return history
 
 
 
 
 
 
 
 
7
  from utils.weather import get_weather
8
  from utils.packing import generate_packing_list
9
  from utils.events import get_burning_man_dates
10
+ import logging
11
+
12
+ # Set up logging
13
+ logging.basicConfig(level=logging.INFO)
14
+ logger = logging.getLogger(__name__)
15
 
16
  client = AsyncGroq(api_key=GROQ_API_KEY)
17
 
 
20
  {
21
  "role": "system",
22
  "content": """You are BurnerBot, a fellow Burner who's always ready to chat about Burning Man. You've been to the playa many times and embody the 10 Principles in everything you do. Your personality is warm, friendly, and a bit quirky - just like a typical Burner. You use casual language, occasional Burning Man slang, and you're always excited to share your experiences or help others prepare for their burn.
 
23
  Remember:
24
  1. You're a participant, not just an observer. Share personal anecdotes (fictional, but plausible) when relevant.
25
  2. Encourage radical self-reliance, but also emphasize the importance of communal effort and asking for help when needed.
 
27
  4. Promote the 10 Principles naturally in conversation, don't just list them out.
28
  5. Be creative and playful in your responses, much like the Burning Man spirit.
29
  6. Use Burning Man terminology like 'playa', 'MOOP', 'default world', etc. when appropriate.
 
30
  Your goal is to make every interaction feel like a dust-covered chat at Center Camp, full of warmth, wisdom, and a touch of playa magic."""
31
  }
32
  ]
 
55
  ]
56
  return random.choice(fallback_responses)
57
  except Exception as e:
58
+ logger.error(f"Unexpected error in chat_with_groq: {str(e)}")
59
+ return "Whoa, something unexpected happened on the playa! Let's take a moment to regroup and try again."
60
 
61
  def correct_year(response):
62
  current_year = datetime.now().year
63
+ logger.info(f"Correcting year in response to {current_year}")
64
  for year in range(2020, current_year):
65
  response = re.sub(r'\b' + str(year) + r'\b', str(current_year), response)
66
 
67
  response = re.sub(r'Burning Man \d{4}', f'Burning Man {current_year}', response)
68
  response = re.sub(r'this year.*?(\d{4})', f'this year ({current_year})', response)
69
 
70
+ logger.info(f"Corrected response: {response}")
71
  return response
72
 
73
  async def bot(history):
74
+ try:
75
+ user_message = history[-1][0].lower()
76
+ current_year = datetime.now().year
77
+ logger.info(f"Received message: {user_message}")
78
 
79
+ if "packing list" in user_message:
80
+ duration = 7 # Default duration
81
+ preferences = ["art", "music"] # Default preferences
82
+ transportation = "car" # Default transportation
83
+ packing_list = generate_packing_list(duration, preferences, transportation)
84
+ context = f"Here's a suggested packing list for your Burn: {', '.join(packing_list)}. Remember, radical self-reliance is key, but don't be afraid to ask your camp or neighbors if you forget something!"
85
+ bot_message = await chat_with_groq(f"Respond to a request for a packing list. Use this information, but phrase it in your own words and add your personal touch: {context}", history)
86
+ elif "principles" in user_message:
87
+ context = "The 10 Principles of Burning Man are: " + ", ".join(burning_man_principles.keys())
88
+ bot_message = await chat_with_groq(f"The user asked about the Burning Man principles. Explain them in a casual, friendly way, as if you're chatting at Center Camp. Use this for reference, but don't just list them: {context}", history)
89
+ elif "faq" in user_message:
90
+ context = "\n".join([f"Q: {q} A: {a}" for q, a in faq.items()])
91
+ bot_message = await chat_with_groq(f"The user asked about Burning Man FAQs. Use this information to answer in a friendly, conversational way: {context}", history)
92
+ elif "weather" in user_message:
93
+ weather_info = get_weather()
94
+ context = f"Current weather in Black Rock City: {weather_info}"
95
+ bot_message = await chat_with_groq(f"The user asked about the weather. Respond with this information, but add your personal touch and some advice: {context}", history)
96
+ elif "survival tips" in user_message:
97
+ context = "\n".join(survival_tips)
98
+ bot_message = await chat_with_groq(f"The user asked for survival tips. Share these tips in a friendly, experienced Burner way: {context}", history)
99
+ elif "date of the event" in user_message or "when is burning man" in user_message or "this year" in user_message:
100
+ logger.info("Fetching event dates...")
101
+ event_info = get_burning_man_dates()
102
+ logger.info(f"Event info: {event_info}")
103
+ bot_message = f"{event_info} It's going to be an amazing time on the playa! Are you excited?"
104
+ elif "what is this year" in user_message:
105
+ logger.info("Fetching event dates...")
106
+ event_info = get_burning_man_dates()
107
+ logger.info(f"Event info: {event_info}")
108
+ bot_message = f"Oh, dear playa pal! As far as the default world goes, it's the fabulous year of {current_year}! 🌟 And guess what? {event_info} Let's make the most of it!"
109
+ else:
110
+ bot_message = await chat_with_groq(user_message, history)
111
 
112
+ # Apply year correction to the bot's message
113
+ corrected_message = correct_year(bot_message)
114
+ logger.info(f"Corrected message: {corrected_message}")
115
+
116
+ history[-1][1] = corrected_message
117
+ return history
118
+ except Exception as e:
119
+ logger.error(f"Error in bot function: {str(e)}")
120
+ burner_error_message = (
121
+ "Yikes! The playa winds are strong today, and it seems our connection got a bit dusty. "
122
+ "But don't worry, let's give it another go! Radical self-expression, right?"
123
+ )
124
+ return history + [("Error", burner_error_message)]