-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
308 lines (257 loc) · 11 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from clients import GPTClient
from manager import MemoryManager, RecipeManager
from parsers import *
from utils import *
load_dotenv(".env")
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
OLLAMA_URL = os.getenv("OLLAMA_URL")
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL")
# Example rules text or welcome message
RULES_TEXT = (
f"Hello! Thanks for trying the SadMadBot - powered by {OLLAMA_MODEL} .\n\n"
"Here are a few usage guidelines:\n\n"
"1) Type `!gpt <question>` to ask.\n"
"2) I'll remember the last 5 messages.\n"
"3) Type `!gpt !context` to see recent Q&A.\n"
"4) Type `!gpt !rules` to see these rules again.\n\n"
"5) Type `!help_gpt` for more commands.\n\n"
"6) Type `!recipe <query>` to search @madihowa's food Pinterest board.\n\n"
"Enjoy!"
)
SYSTEM_PROMPT = """You are a concise, knowledgeable assistant.
Give brief yet helpful answers, usually 1–5 sentences.
Include enough detail to address the question thoroughly,
but do not ramble or include irrelevant commentary or disclaimers.
If there's prior context in the conversation, use it as needed.
"""
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Create singletons
memory = MemoryManager(max_history=10)
gpt = GPTClient(url=OLLAMA_URL, model=OLLAMA_MODEL)
recipe_manager = RecipeManager()
recipe_manager.load_from_json("data/recipes_all_analyzed.json")
# Load the analyzed dataset
with open("data/outfits_analyzed.json", "r", encoding="utf-8") as f:
analyzed_data = json.load(f)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("Bot is ready to chat with short-term memory + context commands.")
def chunk_text(text, chunk_size=2000):
"""Split `text` into a list of smaller strings each <= chunk_size."""
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
@bot.command(name="gpt")
async def gpt_command(ctx, *, prompt: str = ""):
"""
Usage: !gpt <your question here>
The bot will remember up to 5 total messages in this channel,
building short conversation context.
"""
await check_for_new_users(ctx, RULES_TEXT)
channel_id = str(ctx.channel.id)
# If the user typed "!gpt !context", we show a summary of Q&A
if prompt.strip().lower() == "!context":
summary = memory.summarize_context(channel_id)
chunks = chunk_text(summary, 2000)
for chunk in chunks:
await ctx.send(chunk)
return
# If user typed "!gpt !rules"
if prompt.strip().lower() == "!rules":
rules_text = (
"**GPT Bot Rules:**\n"
"1. Keep questions concise.\n"
"2. Avoid sending personal info.\n"
"3. The bot is short-term memory only (5 Q&A long)\n"
)
await ctx.send(rules_text)
return
# 1) Add user's message
memory.add_message(channel_id, "User", prompt)
# 2) Build full prompt
final_prompt = memory.build_prompt(channel_id)
# 3) Query GPT
reply = gpt.query(final_prompt)
if not reply.strip():
reply = "*(No response from the model.)*"
# 4) Add assistant reply to memory
memory.add_message(channel_id, "Assistant", reply)
# 5) Send reply (with chunking)
chunks = chunk_text(reply)
for chunk in chunks:
await ctx.send(chunk)
@bot.command(name="help_gpt")
async def help_gpt_command(ctx):
help_text = (
"**GPT Bot Help**\n"
"Usage:\n"
"`!gpt <your question>` - Ask the GPT.\n"
"`!gpt !context` - Show recent Q&A summary.\n"
"`!gpt !rules` - Show usage rules.\n"
)
await ctx.send(help_text)
@bot.command(name="recipe")
async def recipe_command(ctx, *, query: str = ""):
"""
Usage: !recipe <query>
e.g. "!recipe can i make something with shrimp garlic and onion?"
We'll parse the query with GPT to extract ingredient words
(like ["shrimp","garlic","onion"]) then find matches.
"""
await check_for_new_users(ctx, RULES_TEXT)
if not query.strip():
await ctx.send("Please provide a query about ingredients. e.g. `!recipe shrimp garlic onion`")
return
# 1) Extract user ingredients from the query using GPT
user_ingredients = parse_ingredients_with_llm(query, OLLAMA_URL, OLLAMA_MODEL)
if not user_ingredients:
await ctx.send("I couldn't find any ingredients in that query. Please try again.")
return
# 2) Find matching recipes
matched_recipes = recipe_manager.find_recipes_by_ingredients(user_ingredients)
if not matched_recipes:
await ctx.send(f"No recipes found matching these ingredients: {user_ingredients}")
return
# 3) Show results as multiple embedded messages
for idx, recipe in enumerate(matched_recipes, start=1):
# If the JSON has a real title, it’ll show up. Otherwise fallback is domain
title_to_show = fallback_title_for(recipe)
domain_part = extract_domain(recipe.source_url)
embed = discord.Embed(
title=f"Match #{idx}: {title_to_show}",
url=recipe.source_url,
description="Click the title for details."
)
# If we want to show the ingredient list
if recipe.ingredients:
embed.add_field(
name="Ingredients",
value=", ".join(recipe.ingredients),
inline=False
)
# Show the recipe image if present
if recipe.image_url:
embed.set_image(url=recipe.image_url)
# Footer now includes "SadMad Recipe Bot • {domain_part}"
embed.set_footer(text=f"SadMad Recipe Bot • {domain_part}")
await ctx.send(embed=embed)
@bot.command(name="food")
async def food_command(ctx, *, query: str = ""):
"""
Handles queries like:
- "!food show me N recipes for X type of food"
- "!food any quick and easy X recipes that I can make?"
- "!food X recipes that use A,B,C ingredients"
Uses LLM to parse and RecipeManager to retrieve recipes.
"""
if not query.strip():
await ctx.send("Please provide a query. Example: `!food show me 3 recipes for Italian food`.")
return
# Parse query using LLM
parsed_query = parse_query_with_llm(query, OLLAMA_URL, OLLAMA_MODEL)
if parsed_query["action"] == "error":
await ctx.send("Sorry, I couldn't understand your query. Please try rephrasing.")
return
# Extract parsed details
action = parsed_query.get("action", "")
filters = parsed_query.get("filters", {})
limit = parsed_query.get("limit", 5)
# Handle actions
if action in ["show", "find"]:
food_type = filters.get("type", "").lower()
ingredients = filters.get("ingredients", [])
difficulty = filters.get("difficulty", "")
# Fetch recipes based on filters
if ingredients:
recipes = recipe_manager.find_recipes_by_ingredients(ingredients)
elif difficulty:
recipes = recipe_manager.get_quick_easy_recipes(food_type)
else:
recipes = recipe_manager.get_recipes_by_type(food_type, limit)
# No recipes found
if not recipes:
await ctx.send(f"No recipes found matching your query: {filters}.")
return
# Send recipes as embeds
for recipe in recipes[:limit]:
embed = discord.Embed(
title=recipe.title or "Untitled Recipe",
url=recipe.source_url,
description=f"Difficulty: {recipe.extra.get('difficulty', 'unknown')} | "
f"Time: {recipe.extra.get('time', 'unknown')}",
color=discord.Color.green()
)
embed.add_field(name="Ingredients", value=", ".join(recipe.ingredients) if recipe.ingredients else "N/A", inline=False)
if recipe.image_url:
embed.set_image(url=recipe.image_url)
embed.set_footer(text=f"Source: {recipe.source_url}")
await ctx.send(embed=embed)
elif action == "suggest":
# Suggest general recipes
recipes = recipe_manager.get_recipes_by_type("", limit)
if not recipes:
await ctx.send("Sorry, I couldn't find any recipe suggestions at the moment.")
return
for recipe in recipes[:limit]:
embed = discord.Embed(
title=recipe.title or "Untitled Recipe",
url=recipe.source_url,
description=f"Difficulty: {recipe.extra.get('difficulty', 'unknown')} | "
f"Time: {recipe.extra.get('time', 'unknown')}",
color=discord.Color.blue()
)
embed.add_field(name="Ingredients", value=", ".join(recipe.ingredients) if recipe.ingredients else "N/A", inline=False)
if recipe.image_url:
embed.set_image(url=recipe.image_url)
embed.set_footer(text=f"Source: {recipe.source_url}")
await ctx.send(embed=embed)
else:
await ctx.send(
"I didn't understand that query. Try:\n"
"- `!food show me 3 recipes for Italian food`\n"
"- `!food any quick and easy seafood recipes that I can make?`\n"
"- `!food Italian recipes that use shrimp, garlic, and lemon`"
)
@bot.command(name="outfits")
async def outfit_command(ctx, *, query: str = ""):
"""
Handles queries like:
- "!outfit show me 2 casual outfits that use red shirts"
- "!outfit is there an outfit with black shirt and white jeans?"
- "!outfit show me 3 winter outfits"
"""
if not query.strip():
await ctx.send("Please provide a query. Example: `!outfit show me 3 winter outfits`.")
return
# Parse query using Ollama
parsed_query = parse_outfit_query_with_ollama(query, OLLAMA_URL, OLLAMA_MODEL)
print(f"Parsed query: {parsed_query}")
filters = parsed_query.get("filters", {})
limit = parsed_query.get("limit", 1)
# Match outfits
matching_outfits = match_outfits(analyzed_data, filters, limit)
print(f"Matching outfits: {matching_outfits}")
if not matching_outfits:
await ctx.send(f"No outfits found matching your query: {query}")
else:
response = f"Here are {len(matching_outfits)} outfits that match your query:\n"
for outfit in matching_outfits:
# If outfit is a tuple, extract the image field correctly
if isinstance(outfit, tuple):
image_path = outfit[1]["image"] # The second element is likely the details dictionary
else:
image_path = outfit["image"] # Directly use if it's already a dictionary
try:
await ctx.send(file=discord.File(image_path))
except FileNotFoundError:
await ctx.send(f"Image file not found: {image_path}")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
if __name__ == "__main__":
bot.run(DISCORD_BOT_TOKEN)