updated chat
Browse files- bot/chat.py +50 -40
bot/chat.py
CHANGED
@@ -1,15 +1,20 @@
|
|
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 |
-
#
|
|
|
|
|
|
|
|
|
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
|
@@ -21,10 +26,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 = "
|
25 |
|
26 |
client = AsyncGroq(api_key=GROQ_API_KEY)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
async def chat_with_groq(message, history):
|
29 |
formatted_messages = [
|
30 |
{
|
@@ -59,56 +74,41 @@ async def chat_with_groq(message, history):
|
|
59 |
|
60 |
def correct_year(response):
|
61 |
current_year = datetime.now().year
|
|
|
62 |
logger.info(f"Correcting year in response to {current_year}")
|
|
|
|
|
63 |
for year in range(2020, current_year):
|
64 |
response = re.sub(r'\b' + str(year) + r'\b', str(current_year), response)
|
65 |
|
66 |
-
|
|
|
|
|
|
|
67 |
response = re.sub(r'this year.*?(\d{4})', f'this year ({current_year})', response)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
76 |
logger.info(f"Received message: {user_message}")
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
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)
|
85 |
-
elif "principles" in user_message:
|
86 |
-
context = "The 10 Principles of Burning Man are: " + ", ".join(burning_man_principles.keys())
|
87 |
-
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)
|
88 |
-
elif "faq" in user_message:
|
89 |
-
context = "\n".join([f"Q: {q} A: {a}" for q, a in faq.items()])
|
90 |
-
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)
|
91 |
-
elif "weather" in user_message:
|
92 |
-
weather_info = get_weather()
|
93 |
-
context = f"Current weather in Black Rock City: {weather_info}"
|
94 |
-
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)
|
95 |
-
elif "survival tips" in user_message:
|
96 |
-
context = "\n".join(survival_tips)
|
97 |
-
bot_message = await chat_with_groq(f"The user asked for survival tips. Share these tips in a friendly, experienced Burner way: {context}", history)
|
98 |
-
elif "date of the event" in user_message or "when is burning man" in user_message or "this year" in user_message:
|
99 |
-
logger.info("Fetching event dates...")
|
100 |
-
event_info = get_burning_man_dates()
|
101 |
-
logger.info(f"Event info: {event_info}")
|
102 |
-
bot_message = f"{event_info} It's going to be an amazing time on the playa! Are you excited?"
|
103 |
-
elif "what is this year" in user_message:
|
104 |
-
logger.info("Fetching event dates...")
|
105 |
-
event_info = get_burning_man_dates()
|
106 |
-
logger.info(f"Event info: {event_info}")
|
107 |
-
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!"
|
108 |
-
else:
|
109 |
-
bot_message = await chat_with_groq(user_message, history)
|
110 |
-
|
111 |
-
# Apply year correction to the bot's message
|
112 |
corrected_message = correct_year(bot_message)
|
113 |
logger.info(f"Corrected message: {corrected_message}")
|
114 |
|
@@ -119,4 +119,14 @@ async def chat_interface(message, history):
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
from datetime import datetime
|
4 |
import re
|
5 |
import random
|
6 |
import logging
|
7 |
from groq import AsyncGroq, InternalServerError, APIError
|
8 |
|
9 |
+
# Add the project root directory to sys.path
|
10 |
+
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
11 |
+
sys.path.insert(0, project_root)
|
12 |
+
|
13 |
+
# Now we can use absolute imports
|
14 |
from utils.weather import get_weather
|
15 |
from utils.packing import generate_packing_list
|
16 |
from utils.events import get_burning_man_dates
|
17 |
+
from bot.data import burning_man_principles, faq, survival_tips
|
18 |
|
19 |
|
20 |
# Set up logging
|
|
|
26 |
if not GROQ_API_KEY:
|
27 |
raise EnvironmentError("GROQ_API_KEY is not set in the environment variables")
|
28 |
|
29 |
+
MODEL_NAME = "mixtral-8x7b-32768"
|
30 |
|
31 |
client = AsyncGroq(api_key=GROQ_API_KEY)
|
32 |
|
33 |
+
def get_current_year_info():
|
34 |
+
current_year = datetime.now().year
|
35 |
+
next_year = current_year + 1
|
36 |
+
return f"""
|
37 |
+
Important: The current year is {current_year}.
|
38 |
+
Burning Man {current_year} is scheduled for August 27 - September 4, {current_year}.
|
39 |
+
Burning Man {next_year} is tentatively scheduled for August 25 - September 2, {next_year}.
|
40 |
+
Always refer to these dates when discussing current or upcoming Burning Man events.
|
41 |
+
"""
|
42 |
+
|
43 |
async def chat_with_groq(message, history):
|
44 |
formatted_messages = [
|
45 |
{
|
|
|
74 |
|
75 |
def correct_year(response):
|
76 |
current_year = datetime.now().year
|
77 |
+
next_year = current_year + 1
|
78 |
logger.info(f"Correcting year in response to {current_year}")
|
79 |
+
|
80 |
+
# Replace any year from 2020 to current_year-1 with current_year
|
81 |
for year in range(2020, current_year):
|
82 |
response = re.sub(r'\b' + str(year) + r'\b', str(current_year), response)
|
83 |
|
84 |
+
# Replace outdated event information
|
85 |
+
response = re.sub(r'Burning Man \d{4} (is|was) scheduled for.*', f'Burning Man {current_year} is scheduled for August 27 - September 4, {current_year}.', response)
|
86 |
+
|
87 |
+
# Replace "this year" references
|
88 |
response = re.sub(r'this year.*?(\d{4})', f'this year ({current_year})', response)
|
89 |
|
90 |
+
# Replace "as of our conversation in YYYY" with current year
|
91 |
+
response = re.sub(r'as of our conversation in \d{4}', f'as of our conversation in {current_year}', response)
|
92 |
+
|
93 |
+
# Add a note about potential changes
|
94 |
+
if "scheduled" in response and str(current_year) in response:
|
95 |
+
response += f" Please note that these dates are subject to change, and it's always best to check the official Burning Man website for the most up-to-date information."
|
96 |
+
|
97 |
logger.info(f"Corrected response: {response}")
|
98 |
return response
|
99 |
|
100 |
async def chat_interface(message, history):
|
101 |
try:
|
102 |
user_message = message.lower()
|
103 |
+
current_year_info = get_current_year_info()
|
104 |
logger.info(f"Received message: {user_message}")
|
105 |
|
106 |
+
# Inject current year information into the prompt
|
107 |
+
prompt = f"{current_year_info}\n\nUser: {message}\n\nAssistant:"
|
108 |
+
|
109 |
+
bot_message = await chat_with_groq(prompt, history)
|
110 |
+
|
111 |
+
# Apply year correction to all bot messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
corrected_message = correct_year(bot_message)
|
113 |
logger.info(f"Corrected message: {corrected_message}")
|
114 |
|
|
|
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
|
123 |
+
|
124 |
+
# Add this at the end of the file for testing purposes
|
125 |
+
if __name__ == "__main__":
|
126 |
+
import asyncio
|
127 |
+
|
128 |
+
async def test_chat():
|
129 |
+
response = await chat_interface("Tell me about Burning Man", [])
|
130 |
+
print(response)
|
131 |
+
|
132 |
+
asyncio.run(test_chat())
|