-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
167 lines (142 loc) · 5.93 KB
/
manager.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
# bot.py
import json
import os
from pathlib import Path
from typing import List
from dotenv import load_dotenv
from models.recipe import Recipe
load_dotenv("../.env")
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
OLLAMA_URL = os.getenv("OLLAMA_URL")
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL")
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.
"""
class MemoryManager:
def __init__(self, max_history=5):
# store dict: { channel_id: [(role, message), ...], ... }
self.conversation_history = {}
self.max_history = max_history
def add_message(self, channel_id, role, content):
"""
Add a message to the channel's conversation log.
Trim older messages if we exceed self.max_history.
"""
if channel_id not in self.conversation_history:
self.conversation_history[channel_id] = []
self.conversation_history[channel_id].append((role, content))
# Trim if we exceed max
if len(self.conversation_history[channel_id]) > self.max_history:
self.conversation_history[channel_id].pop(0)
def build_prompt(self, channel_id):
"""
Build final prompt from system instructions + conversation history + "Assistant:".
"""
lines = [f"System: {SYSTEM_PROMPT}"]
# add messages from memory
if channel_id in self.conversation_history:
for (role, text) in self.conversation_history[channel_id]:
lines.append(f"{role}: {text}")
# final line: assistant responds
lines.append("Assistant:")
return "\n".join(lines)
def summarize_context(self, channel_id):
"""
Returns a short listing of what user asked and what the assistant replied.
Example format for each stored pair:
Q: <user text>
A: <assistant text>
"""
if channel_id not in self.conversation_history or not self.conversation_history[channel_id]:
return "No previous context in this channel."
# Gather pairs of (User, Assistant) messages
user_idx = 1
summary_lines = []
user_buffer = None
for (role, text) in self.conversation_history[channel_id]:
if role == "User":
user_buffer = text
elif role == "Assistant" and user_buffer:
summary_lines.append(f"Q{user_idx}: {user_buffer}\nA{user_idx}: {text}")
user_buffer = None
user_idx += 1
# if there's a user msg at the end without an assistant reply, handle it
if user_buffer is not None:
summary_lines.append(f"Q{user_idx}: {user_buffer}\nA{user_idx}: *(No assistant reply yet)*")
if not summary_lines:
return "No user Q&A found yet."
return "\n\n".join(summary_lines)
class RecipeManager:
"""
Stores a list of Recipe objects and provides methods for loading
and searching them by ingredients.
"""
def __init__(self):
self.recipes = []
def load_from_json(self, json_path: str) -> None:
"""
Loads recipes from a JSON file (the structure shown in your example).
"""
path = Path(json_path)
if not path.exists():
raise FileNotFoundError(f"Recipe JSON file not found at {json_path}")
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, list):
raise ValueError("Expected a list of recipe records in JSON.")
for record in data:
pinterest_url = record.get("pinterest_url", "")
source_url = record.get("source_url", "")
recipe_data = record.get("recipe_data", {})
title = recipe_data.get("title", "")
image_url = recipe_data.get("image_url", "")
ingredients = recipe_data.get("ingredients", [])
extra = recipe_data.get("extra", {})
recipe_obj = Recipe(
pinterest_url=pinterest_url,
source_url=source_url,
title=title,
image_url=image_url,
ingredients=ingredients,
extra=extra
)
self.recipes.append(recipe_obj)
def find_recipes_by_ingredients(self, user_ingredients: List[str]):
"""
Returns a list of recipes that match the given user_ingredients.
"""
if not user_ingredients:
return []
matched = []
for recipe in self.recipes:
# recipe.fetch_title_from_pinterest()
if recipe.has_ingredients(user_ingredients):
matched.append(recipe)
return matched
def get_recipes_by_type(self, food_type: str, limit: [int] = None):
"""
Finds recipes matching the specified food type.
"""
matching_recipes = [
recipe for recipe in self.recipes
if food_type.lower() in map(str.lower, recipe.extra.get("cuisines", []))
]
return matching_recipes[:limit] if limit else matching_recipes
def get_quick_easy_recipes(self, food_type: [str] = None):
"""
Finds quick and easy recipes, optionally filtered by food type.
"""
matching_recipes = [
recipe for recipe in self.recipes
if recipe.extra.get("difficulty", "").lower() == "easy"
and "30" in recipe.extra.get("time", "").lower()
]
if food_type:
matching_recipes = [
recipe for recipe in matching_recipes
if food_type.lower() in map(str.lower, recipe.extra.get("cuisines", []))
]
return matching_recipes