Abhlash commited on
Commit
c659fd2
·
verified ·
1 Parent(s): f0f44a8

updated chat

Browse files
Files changed (1) hide show
  1. bot/chat.py +3 -68
bot/chat.py CHANGED
@@ -1,75 +1,9 @@
1
- from datetime import datetime
2
- import re
3
- import random
4
- from groq import AsyncGroq, InternalServerError, APIError
5
- from config import GROQ_API_KEY, MODEL_NAME
6
- from .data import burning_man_principles, faq, survival_tips # Use relative import
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
-
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()
@@ -113,7 +47,8 @@ async def bot(history):
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)}")
@@ -121,4 +56,4 @@ async def bot(history):
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)]
 
 
 
 
 
 
 
 
 
 
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()
 
47
  corrected_message = correct_year(bot_message)
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)}")
 
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)]