updated chat
Browse files- bot/chat.py +17 -46
bot/chat.py
CHANGED
|
@@ -1,59 +1,35 @@
|
|
|
|
|
| 1 |
from datetime import datetime
|
| 2 |
import re
|
| 3 |
import random
|
| 4 |
import logging
|
| 5 |
from groq import AsyncGroq, InternalServerError, APIError
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
# Prepare the conversation history
|
| 21 |
-
conversation = []
|
| 22 |
-
for h in history:
|
| 23 |
-
conversation.append({"role": "user", "content": h[0]})
|
| 24 |
-
conversation.append({"role": "assistant", "content": h[1]})
|
| 25 |
-
conversation.append({"role": "user", "content": message})
|
| 26 |
-
|
| 27 |
-
# Call the Groq API
|
| 28 |
-
chat_completion = await client.chat.completions.create(
|
| 29 |
-
messages=conversation,
|
| 30 |
-
model="mixtral-8x7b-32768",
|
| 31 |
-
temperature=0.7,
|
| 32 |
-
max_tokens=1000,
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# Extract the response
|
| 36 |
-
response = chat_completion.choices[0].message.content
|
| 37 |
-
|
| 38 |
-
return response
|
| 39 |
-
except Exception as e:
|
| 40 |
-
logger.error(f"Error in chat_interface: {str(e)}")
|
| 41 |
-
return "Oops! Something went wrong. Please try again later."
|
| 42 |
|
|
|
|
| 43 |
|
| 44 |
async def chat_with_groq(message, history):
|
| 45 |
formatted_messages = [
|
| 46 |
{
|
| 47 |
"role": "system",
|
| 48 |
-
"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.
|
| 49 |
-
Remember:
|
| 50 |
-
1. You're a participant, not just an observer. Share personal anecdotes (fictional, but plausible) when relevant.
|
| 51 |
-
2. Encourage radical self-reliance, but also emphasize the importance of communal effort and asking for help when needed.
|
| 52 |
-
3. Be inclusive and welcoming to all, whether they're virgin burners or old-timers.
|
| 53 |
-
4. Promote the 10 Principles naturally in conversation, don't just list them out.
|
| 54 |
-
5. Be creative and playful in your responses, much like the Burning Man spirit.
|
| 55 |
-
6. Use Burning Man terminology like 'playa', 'MOOP', 'default world', etc. when appropriate.
|
| 56 |
-
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."""
|
| 57 |
}
|
| 58 |
]
|
| 59 |
|
|
@@ -75,9 +51,6 @@ async def chat_with_groq(message, history):
|
|
| 75 |
fallback_responses = [
|
| 76 |
"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?",
|
| 77 |
"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!",
|
| 78 |
-
"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!",
|
| 79 |
-
"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?",
|
| 80 |
-
"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?"
|
| 81 |
]
|
| 82 |
return random.choice(fallback_responses)
|
| 83 |
except Exception as e:
|
|
@@ -96,9 +69,9 @@ def correct_year(response):
|
|
| 96 |
logger.info(f"Corrected response: {response}")
|
| 97 |
return response
|
| 98 |
|
| 99 |
-
async def
|
| 100 |
try:
|
| 101 |
-
user_message =
|
| 102 |
current_year = datetime.now().year
|
| 103 |
logger.info(f"Received message: {user_message}")
|
| 104 |
|
|
@@ -139,13 +112,11 @@ async def bot(history):
|
|
| 139 |
corrected_message = correct_year(bot_message)
|
| 140 |
logger.info(f"Corrected message: {corrected_message}")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
new_history = history[:-1] + [(history[-1][0], corrected_message)]
|
| 144 |
-
return new_history
|
| 145 |
except Exception as e:
|
| 146 |
-
logger.error(f"Error in
|
| 147 |
burner_error_message = (
|
| 148 |
"Yikes! The playa winds are strong today, and it seems our connection got a bit dusty. "
|
| 149 |
"But don't worry, let's give it another go! Radical self-expression, right?"
|
| 150 |
)
|
| 151 |
-
return
|
|
|
|
| 1 |
+
import os
|
| 2 |
from datetime import datetime
|
| 3 |
import re
|
| 4 |
import random
|
| 5 |
import logging
|
| 6 |
from groq import AsyncGroq, InternalServerError, APIError
|
| 7 |
+
|
| 8 |
+
# Import utility functions
|
| 9 |
from utils.weather import get_weather
|
| 10 |
from utils.packing import generate_packing_list
|
| 11 |
from utils.events import get_burning_man_dates
|
| 12 |
+
from .data import burning_man_principles, faq, survival_tips
|
| 13 |
+
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
+
# Load API key from environment variable
|
| 20 |
+
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
| 21 |
+
if not GROQ_API_KEY:
|
| 22 |
+
raise EnvironmentError("GROQ_API_KEY is not set in the environment variables")
|
| 23 |
|
| 24 |
+
MODEL_NAME = "llama3-70b-8192"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
client = AsyncGroq(api_key=GROQ_API_KEY)
|
| 27 |
|
| 28 |
async def chat_with_groq(message, history):
|
| 29 |
formatted_messages = [
|
| 30 |
{
|
| 31 |
"role": "system",
|
| 32 |
+
"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."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
]
|
| 35 |
|
|
|
|
| 51 |
fallback_responses = [
|
| 52 |
"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?",
|
| 53 |
"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!",
|
|
|
|
|
|
|
|
|
|
| 54 |
]
|
| 55 |
return random.choice(fallback_responses)
|
| 56 |
except Exception as e:
|
|
|
|
| 69 |
logger.info(f"Corrected response: {response}")
|
| 70 |
return response
|
| 71 |
|
| 72 |
+
async def chat_interface(message, history):
|
| 73 |
try:
|
| 74 |
+
user_message = message.lower()
|
| 75 |
current_year = datetime.now().year
|
| 76 |
logger.info(f"Received message: {user_message}")
|
| 77 |
|
|
|
|
| 112 |
corrected_message = correct_year(bot_message)
|
| 113 |
logger.info(f"Corrected message: {corrected_message}")
|
| 114 |
|
| 115 |
+
return corrected_message
|
|
|
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
+
logger.error(f"Error in chat_interface: {str(e)}")
|
| 118 |
burner_error_message = (
|
| 119 |
"Yikes! The playa winds are strong today, and it seems our connection got a bit dusty. "
|
| 120 |
"But don't worry, let's give it another go! Radical self-expression, right?"
|
| 121 |
)
|
| 122 |
+
return burner_error_message
|