Abhlash commited on
Commit
9938cf0
·
verified ·
1 Parent(s): d83eafd

updated chat

Browse files
Files changed (1) hide show
  1. bot/chat.py +69 -3
bot/chat.py CHANGED
@@ -1,9 +1,75 @@
 
 
 
1
  import logging
 
 
 
 
 
 
2
 
3
  # Set up logging
4
  logging.basicConfig(level=logging.INFO)
5
  logger = logging.getLogger(__name__)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  async def bot(history):
8
  try:
9
  user_message = history[-1][0].lower()
@@ -48,12 +114,12 @@ async def bot(history):
48
  logger.info(f"Corrected message: {corrected_message}")
49
 
50
  # Update the history correctly
51
- history[-1] = (history[-1][0], corrected_message)
52
- return history
53
  except Exception as e:
54
  logger.error(f"Error in bot function: {str(e)}")
55
  burner_error_message = (
56
  "Yikes! The playa winds are strong today, and it seems our connection got a bit dusty. "
57
  "But don't worry, let's give it another go! Radical self-expression, right?"
58
  )
59
- return history + [(history[-1][0], burner_error_message)]
 
1
+ from datetime import datetime
2
+ import re
3
+ import random
4
  import logging
5
+ from groq import AsyncGroq, InternalServerError, APIError
6
+ from config import GROQ_API_KEY, MODEL_NAME
7
+ from .data import burning_man_principles, faq, survival_tips
8
+ from utils.weather import get_weather
9
+ from utils.packing import generate_packing_list
10
+ from utils.events import get_burning_man_dates
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
+
18
+ async def chat_with_groq(message, history):
19
+ formatted_messages = [
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.
26
+ 3. Be inclusive and welcoming to all, whether they're virgin burners or old-timers.
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
+ ]
33
+
34
+ for human, ai in history:
35
+ formatted_messages.append({"role": "user", "content": str(human)})
36
+ if ai:
37
+ formatted_messages.append({"role": "assistant", "content": str(ai)})
38
+
39
+ formatted_messages.append({"role": "user", "content": str(message)})
40
+
41
+ try:
42
+ chat_completion = await client.chat.completions.create(
43
+ messages=formatted_messages,
44
+ model=MODEL_NAME,
45
+ max_tokens=1024
46
+ )
47
+ return chat_completion.choices[0].message.content
48
+ except (InternalServerError, APIError):
49
+ fallback_responses = [
50
+ "Whoa there, fellow Burner! It seems like the playa dust has clogged my circuits. The connection is a bit wobbly right now. How about we chat about your favorite art installation instead?",
51
+ "Ah, the winds of the playa are blowing a bit too strong! Our connection is having a moment. While we wait for it to pass, share with me what inspires your inner Burner!",
52
+ "Looks like a sandstorm has rolled through and interrupted our connection! While we wait for the skies to clear, tell me about your most memorable moment on the playa!",
53
+ "Just like on the playa, sometimes the universe throws us a curveball. Our connection is a bit shaky right now. In the spirit of radical self-reliance, what are you doing to prepare for your burn?",
54
+ "It seems the Man is playing tricks on us! Our connection is a bit spotty. While we sort that out, what's one thing you're most excited to bring to the playa this year?"
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()
 
114
  logger.info(f"Corrected message: {corrected_message}")
115
 
116
  # Update the history correctly
117
+ new_history = history[:-1] + [(history[-1][0], corrected_message)]
118
+ return new_history
119
  except Exception as e:
120
  logger.error(f"Error in bot function: {str(e)}")
121
  burner_error_message = (
122
  "Yikes! The playa winds are strong today, and it seems our connection got a bit dusty. "
123
  "But don't worry, let's give it another go! Radical self-expression, right?"
124
  )
125
+ return history + [(history[-1][0], burner_error_message)]