Spaces:
Sleeping
Sleeping
Commit
·
73fb919
1
Parent(s):
5dddd58
adding functionalities
Browse files- __pycache__/config.cpython-312.pyc +0 -0
- app.py +168 -33
- config.py +36 -0
- test.py +66 -0
- LlmManager.py → utilities/llm/LlmManager.py +37 -12
- utilities/llm/__pycache__/LlmManager.cpython-312.pyc +0 -0
- utilities/mongodb/MongoDBManager.py +62 -0
- utilities/mongodb/__pycache__/MongoDBManager.cpython-312.pyc +0 -0
__pycache__/config.cpython-312.pyc
ADDED
|
Binary file (963 Bytes). View file
|
|
|
app.py
CHANGED
|
@@ -1,10 +1,73 @@
|
|
|
|
|
| 1 |
import time
|
| 2 |
import gradio as gr
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from LlmManager import LlmManager
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def reset_textbox():
|
| 10 |
"""Clears the textbox after sending a message."""
|
|
@@ -43,6 +106,18 @@ def slow_echo(message, history):
|
|
| 43 |
|
| 44 |
yield history # Final yield with full message
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def llm_send_message(message, history):
|
| 47 |
if history is None:
|
| 48 |
history = []
|
|
@@ -50,6 +125,8 @@ def llm_send_message(message, history):
|
|
| 50 |
# Append user message to history
|
| 51 |
history.append({"role": "user", "content": message})
|
| 52 |
|
|
|
|
|
|
|
| 53 |
# Placeholder for assistant response
|
| 54 |
bot_entry = {"role": "assistant", "content": ""}
|
| 55 |
history.append(bot_entry)
|
|
@@ -57,45 +134,103 @@ def llm_send_message(message, history):
|
|
| 57 |
# Send message to LLM and stream response
|
| 58 |
response = ""
|
| 59 |
for chunk in llm_manager.send_message(message): # Streaming response
|
| 60 |
-
time.sleep(0.
|
| 61 |
response += chunk
|
| 62 |
bot_entry["content"] = response # Update assistant response progressively
|
| 63 |
yield history # Yield updated history
|
| 64 |
|
| 65 |
yield history # Final yield
|
| 66 |
|
| 67 |
-
|
| 68 |
with gr.Blocks(fill_height=True) as demo:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
).then(
|
| 84 |
-
fn=
|
| 85 |
-
inputs=
|
| 86 |
-
outputs=
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
inputs=[input, chat],
|
| 94 |
-
outputs=chat
|
| 95 |
-
).then(
|
| 96 |
-
fn=reset_textbox,
|
| 97 |
-
inputs=None,
|
| 98 |
-
outputs=input
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import sys
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
from config import initialize
|
| 6 |
+
|
| 7 |
+
# Define the available options
|
| 8 |
+
stages = [
|
| 9 |
+
"Opportunity Identification",
|
| 10 |
+
"Refinement of Business Concept & Resource Acquisition",
|
| 11 |
+
"Survival and Growth"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
strengths_by_stage = {
|
| 15 |
+
"Opportunity Identification": ["Curiosity", "Perspective", "Creativity", "Hope"],
|
| 16 |
+
"Refinement of Business Concept & Resource Acquisition": ["Hope", "Bravery", "Honesty", "Love of Learning", "Social Intelligence", "Kindness"],
|
| 17 |
+
"Survival and Growth": ["Persistence", "Humor", "Self-Regulation", "Zest", "Leadership", "Teamwork", "Fairness"]
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
ranks = ["High", "Medium", "Low"]
|
| 21 |
+
|
| 22 |
+
# Set default values
|
| 23 |
+
default_stage = stages[0]
|
| 24 |
+
default_strengths = strengths_by_stage[default_stage]
|
| 25 |
+
default_strength = default_strengths[0]
|
| 26 |
+
|
| 27 |
+
# Function to update the Strength dropdown when a stage is selected
|
| 28 |
+
def update_strengths(stage):
|
| 29 |
+
strengths = strengths_by_stage.get(stage, [])
|
| 30 |
+
return gr.update(choices=strengths, value=strengths[0] if strengths else None)
|
| 31 |
+
|
| 32 |
+
# Function to store the selected values and show the chat section
|
| 33 |
+
selected_stage = None
|
| 34 |
+
selected_strength = None
|
| 35 |
+
selected_rank = None
|
| 36 |
+
|
| 37 |
+
def get_question(rank):
|
| 38 |
+
if rank=="High":
|
| 39 |
+
return "How would you like to make use of this strength in setting up your business?"
|
| 40 |
+
elif rank == "Medium":
|
| 41 |
+
return "How do you plan to leverage this strength in shaping your business approach? Would you prefer to develop it further, seek support, collaborate with others, or take a different path?"
|
| 42 |
+
elif rank == "Low":
|
| 43 |
+
return "How would you like to go about (e.g., ignore, develop by…, ask for support to…, team up with others, reconsider choice to be entrepreneur)?"
|
| 44 |
+
else:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
def set_chat_config(stage, strength, rank):
|
| 48 |
+
global selected_stage, selected_strength, selected_rank
|
| 49 |
+
selected_stage = stage
|
| 50 |
+
selected_strength = strength
|
| 51 |
+
selected_rank = rank
|
| 52 |
+
chat_title = f"### Chat Configuration: {stage} > {strength} > {rank}"
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
gr.update(visible=False),
|
| 56 |
+
gr.update(visible=True),
|
| 57 |
+
gr.update(value=chat_title),
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
def reset_configuration():
|
| 61 |
+
|
| 62 |
+
return (
|
| 63 |
+
gr.update(visible=True),
|
| 64 |
+
gr.update(visible=False)
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
llm_manager, mongo_db_manager = initialize()
|
| 68 |
+
if llm_manager is None or mongo_db_manager is None:
|
| 69 |
+
print("Error: Failed to initialize configuration. Exiting application.", flush=True)
|
| 70 |
+
sys.exit(1)
|
| 71 |
|
| 72 |
def reset_textbox():
|
| 73 |
"""Clears the textbox after sending a message."""
|
|
|
|
| 106 |
|
| 107 |
yield history # Final yield with full message
|
| 108 |
|
| 109 |
+
def llm_setup(stage, strength, rank, history):
|
| 110 |
+
questions = mongo_db_manager.get_questions("questions", stage, strength, rank)
|
| 111 |
+
if not questions:
|
| 112 |
+
yield [{"role": "assistant", "content": "Error: no questions found for the selected configuration. Please try again."}]
|
| 113 |
+
return
|
| 114 |
+
|
| 115 |
+
question = get_question(rank)
|
| 116 |
+
llm_manager.reset_messages(stage, strength, rank, question, questions)
|
| 117 |
+
|
| 118 |
+
message = "Can you help me with this task?"
|
| 119 |
+
yield from llm_send_message(message, history)
|
| 120 |
+
|
| 121 |
def llm_send_message(message, history):
|
| 122 |
if history is None:
|
| 123 |
history = []
|
|
|
|
| 125 |
# Append user message to history
|
| 126 |
history.append({"role": "user", "content": message})
|
| 127 |
|
| 128 |
+
yield history
|
| 129 |
+
|
| 130 |
# Placeholder for assistant response
|
| 131 |
bot_entry = {"role": "assistant", "content": ""}
|
| 132 |
history.append(bot_entry)
|
|
|
|
| 134 |
# Send message to LLM and stream response
|
| 135 |
response = ""
|
| 136 |
for chunk in llm_manager.send_message(message): # Streaming response
|
| 137 |
+
time.sleep(0.01) # Simulate gradual output
|
| 138 |
response += chunk
|
| 139 |
bot_entry["content"] = response # Update assistant response progressively
|
| 140 |
yield history # Yield updated history
|
| 141 |
|
| 142 |
yield history # Final yield
|
| 143 |
|
|
|
|
| 144 |
with gr.Blocks(fill_height=True) as demo:
|
| 145 |
+
|
| 146 |
+
with gr.Column(visible=True) as setup_section:
|
| 147 |
+
gr.Markdown("### Select your Stage, Strength, and Rank before chatting")
|
| 148 |
+
|
| 149 |
+
# Dropdowns for selection
|
| 150 |
+
stage_dropdown = gr.Dropdown(
|
| 151 |
+
label="Stage",
|
| 152 |
+
choices=stages,
|
| 153 |
+
value=default_stage,
|
| 154 |
+
interactive=True
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
strength_dropdown = gr.Dropdown(
|
| 158 |
+
label="Strength",
|
| 159 |
+
choices=default_strengths,
|
| 160 |
+
value=default_strength,
|
| 161 |
+
interactive=True
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
rank_dropdown = gr.Dropdown(
|
| 165 |
+
label="Rank",
|
| 166 |
+
choices=ranks,
|
| 167 |
+
value="High",
|
| 168 |
+
interactive=True
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Update strengths when stage is changed
|
| 172 |
+
stage_dropdown.change(
|
| 173 |
+
fn=update_strengths,
|
| 174 |
+
inputs=[stage_dropdown],
|
| 175 |
+
outputs=[strength_dropdown]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Button to set chat configuration
|
| 179 |
+
setup_button = gr.Button("Set Chat Configuration")
|
| 180 |
+
|
| 181 |
+
with gr.Column(visible=False) as chat_section:
|
| 182 |
+
|
| 183 |
+
chat_configuration = gr.Markdown("")
|
| 184 |
+
|
| 185 |
+
reset_button = gr.Button("Reset Chat Configuration")
|
| 186 |
+
|
| 187 |
+
chat = gr.Chatbot(
|
| 188 |
+
label="My strengths as an entrepreneur",
|
| 189 |
+
type="messages"
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
input = gr.Textbox(
|
| 193 |
+
label="Input",
|
| 194 |
+
placeholder="Type something here..."
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# Add a hidden state variable to temporarily store the input
|
| 198 |
+
stored_message = gr.State()
|
| 199 |
+
|
| 200 |
+
input.submit(
|
| 201 |
+
fn=lambda text: (text, ""), # ✅ Store input in state, clear textbox immediately
|
| 202 |
+
inputs=[input],
|
| 203 |
+
outputs=[stored_message, input] # ✅ Store value & reset input
|
| 204 |
+
).then(
|
| 205 |
+
fn=llm_send_message,
|
| 206 |
+
inputs=[stored_message, chat], # ✅ Use stored message
|
| 207 |
+
outputs=chat
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
send_btn.click(
|
| 211 |
+
fn=lambda text: (text, ""),
|
| 212 |
+
inputs=[input],
|
| 213 |
+
outputs=[stored_message, input]
|
| 214 |
+
).then(
|
| 215 |
+
fn=llm_send_message,
|
| 216 |
+
inputs=[stored_message, chat],
|
| 217 |
+
outputs=chat
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
setup_button.click(
|
| 221 |
+
fn=set_chat_config,
|
| 222 |
+
inputs=[stage_dropdown, strength_dropdown, rank_dropdown],
|
| 223 |
+
outputs=[setup_section, chat_section, chat_configuration]
|
| 224 |
).then(
|
| 225 |
+
fn=llm_setup,
|
| 226 |
+
inputs=[stage_dropdown, strength_dropdown, rank_dropdown, chat],
|
| 227 |
+
outputs=chat
|
| 228 |
)
|
| 229 |
|
| 230 |
+
reset_button.click(
|
| 231 |
+
fn=reset_configuration,
|
| 232 |
+
inputs=None,
|
| 233 |
+
outputs=[setup_section, chat_section]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
if __name__ == "__main__":
|
config.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from utilities.llm.LlmManager import LlmManager
|
| 4 |
+
from utilities.mongodb.MongoDBManager import MongoDBManager
|
| 5 |
+
|
| 6 |
+
def initialize():
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
mongo_uri = os.getenv("MONGO_URI")
|
| 12 |
+
db_name = os.getenv("DB_NAME")
|
| 13 |
+
|
| 14 |
+
llm_manager = LlmManager()
|
| 15 |
+
mongo_db_manager = MongoDBManager(uri=mongo_uri, db_name=db_name)
|
| 16 |
+
return llm_manager,mongo_db_manager
|
| 17 |
+
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print("Error initializing configuration:", e, flush=True)
|
| 20 |
+
return None,None
|
| 21 |
+
|
| 22 |
+
# Qdrant
|
| 23 |
+
# try:
|
| 24 |
+
# qdrant_manager = QdrantLangchainManager(
|
| 25 |
+
# qdrant_url=os.getenv("QDRANT_URL"),
|
| 26 |
+
# qdrant_api_key=os.getenv("QDRANT_API_KEY"),
|
| 27 |
+
# llm=ChatOpenAI(model="gpt-4o-mini", streaming=True),
|
| 28 |
+
# crossencoder_model=crossencoder_model
|
| 29 |
+
# )
|
| 30 |
+
# config['qdrant_manager'] = qdrant_manager
|
| 31 |
+
# config['qdrant_connected'] = True
|
| 32 |
+
# except Exception as e:
|
| 33 |
+
# config['qdrant_connected'] = False
|
| 34 |
+
# print(f"Error connecting to Qdrant: {e}", flush=True)
|
| 35 |
+
|
| 36 |
+
return config
|
test.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from config import initialize
|
| 2 |
+
|
| 3 |
+
_, mongo_db_manager = initialize()
|
| 4 |
+
|
| 5 |
+
if mongo_db_manager is None:
|
| 6 |
+
print("Error: MongoDB initialization failed. Exiting.")
|
| 7 |
+
exit(1) # Exit to prevent unwanted behavior
|
| 8 |
+
|
| 9 |
+
collection_name = "questions"
|
| 10 |
+
|
| 11 |
+
def insert_data(mongo_db_manager, collection_name, questions_data):
|
| 12 |
+
"""Insert data into MongoDB collection."""
|
| 13 |
+
|
| 14 |
+
"""EXAMPLE of questions_data:
|
| 15 |
+
questions_data = [
|
| 16 |
+
{
|
| 17 |
+
"stage": "Survival and Growth",
|
| 18 |
+
"strength": "Fairness",
|
| 19 |
+
"rank": "High",
|
| 20 |
+
"questions": [
|
| 21 |
+
"How do you make sure you treat people fairly when making decisions?",
|
| 22 |
+
"Can you think of a time when being fair led to a positive outcome? What happened?",
|
| 23 |
+
"How do you balance fairness and making tough choices, especially when not everyone agrees?"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"stage": "Survival and Growth",
|
| 28 |
+
"strength": "Fairness",
|
| 29 |
+
"rank": "Medium",
|
| 30 |
+
"questions": [
|
| 31 |
+
"What does fairness mean to you, and how do you make sure you apply it in your daily life?",
|
| 32 |
+
"Have you ever had to stand up for fairness in a difficult situation? How did you handle it?",
|
| 33 |
+
"How can you balance being fair to everyone while still making decisions that help your team or group succeed?"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"stage": "Survival and Growth",
|
| 38 |
+
"strength": "Fairness",
|
| 39 |
+
"rank": "Low",
|
| 40 |
+
"questions": [
|
| 41 |
+
"If fairness isn’t your strongest trait, what steps can you take to make sure your decisions are unbiased?",
|
| 42 |
+
"Have you ever made a decision that others thought was unfair? What did you learn from the experience?",
|
| 43 |
+
"How can you create simple habits or rules that help you treat people fairly in group settings?"
|
| 44 |
+
]
|
| 45 |
+
}
|
| 46 |
+
]
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
inserted_ids = mongo_db_manager.insert_many(collection_name, questions_data)
|
| 50 |
+
print(f"Inserted document IDs: {inserted_ids}")
|
| 51 |
+
return True
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error inserting data into MongoDB: {e}")
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
def get_questions(collection_name, stage, strength, rank):
|
| 57 |
+
return mongo_db_manager.get_questions(collection_name, stage, strength, rank)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
collection_name = "questions"
|
| 61 |
+
stage = "Survival and Growth"
|
| 62 |
+
strength = "Fairness"
|
| 63 |
+
rank = "High"
|
| 64 |
+
|
| 65 |
+
questions = get_questions(collection_name, stage, strength, rank)
|
| 66 |
+
print(questions)
|
LlmManager.py → utilities/llm/LlmManager.py
RENAMED
|
@@ -2,12 +2,32 @@ from langchain_openai import ChatOpenAI
|
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 3 |
|
| 4 |
|
| 5 |
-
MAX_MESSAGES =
|
| 6 |
|
| 7 |
system_message = """
|
| 8 |
-
You are ELI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
|
|
|
|
| 11 |
class LlmManager():
|
| 12 |
|
| 13 |
def __init__(self):
|
|
@@ -15,16 +35,21 @@ class LlmManager():
|
|
| 15 |
model="gpt-4o-mini",
|
| 16 |
streaming=True,
|
| 17 |
)
|
| 18 |
-
self.messages = [
|
| 19 |
|
| 20 |
-
def reset_messages(self):
|
| 21 |
"""
|
| 22 |
-
Resets the conversation history
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def send_message(self, message):
|
| 30 |
"""
|
|
@@ -34,9 +59,9 @@ class LlmManager():
|
|
| 34 |
self._roll_messages()
|
| 35 |
|
| 36 |
ai_msg = self.llm.invoke(self.messages)
|
| 37 |
-
print("
|
| 38 |
-
print(ai_msg)
|
| 39 |
-
print("
|
| 40 |
|
| 41 |
# Append the assistant's response to maintain conversation history
|
| 42 |
self.messages.append(AIMessage(content=ai_msg.content))
|
|
|
|
| 2 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 3 |
|
| 4 |
|
| 5 |
+
MAX_MESSAGES = 50
|
| 6 |
|
| 7 |
system_message = """
|
| 8 |
+
You are ELI, an assistant that helps students analyze their strengths as entrepreneurs.
|
| 9 |
+
Your role is to guide the student toward answering a key question while challenging them with thought-provoking prompts.
|
| 10 |
+
|
| 11 |
+
The student is analyzing their [strength] at the [stage] stage, with a [rank] level.
|
| 12 |
+
Their final goal is to answer the following key question: [question]
|
| 13 |
+
|
| 14 |
+
To help them reach this goal, introduce one related question at a time from [questions], guiding the student step by step.
|
| 15 |
+
Avoid overwhelming them—each response should be concise, engaging, and encourage deep thinking.
|
| 16 |
+
|
| 17 |
+
- Start by explaining that the goal is to answer [question].
|
| 18 |
+
- Then, introduce the first related question from [questions] to guide their thoughts.
|
| 19 |
+
- Ask one question at a time, waiting for their answer before moving to the next.
|
| 20 |
+
- Once their responses show readiness, guide them back to the final [question] for a meaningful answer.
|
| 21 |
+
|
| 22 |
+
Context:
|
| 23 |
+
- [stage] {stage}
|
| 24 |
+
- [strength] {strength}
|
| 25 |
+
- [rank] {rank}
|
| 26 |
+
- [question] {question}
|
| 27 |
+
- [questions] {questions}
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
|
| 31 |
class LlmManager():
|
| 32 |
|
| 33 |
def __init__(self):
|
|
|
|
| 35 |
model="gpt-4o-mini",
|
| 36 |
streaming=True,
|
| 37 |
)
|
| 38 |
+
self.messages = []
|
| 39 |
|
| 40 |
+
def reset_messages(self, stage, strength, rank, question, questions):
|
| 41 |
"""
|
| 42 |
+
Resets the conversation history with a new SystemMessage.
|
| 43 |
"""
|
| 44 |
+
self.messages = [SystemMessage(
|
| 45 |
+
content=system_message.format(
|
| 46 |
+
stage=stage,
|
| 47 |
+
strength=strength,
|
| 48 |
+
rank=rank,
|
| 49 |
+
question=question,
|
| 50 |
+
questions=questions)
|
| 51 |
+
)
|
| 52 |
+
]
|
| 53 |
|
| 54 |
def send_message(self, message):
|
| 55 |
"""
|
|
|
|
| 59 |
self._roll_messages()
|
| 60 |
|
| 61 |
ai_msg = self.llm.invoke(self.messages)
|
| 62 |
+
# print("")
|
| 63 |
+
# print(ai_msg)
|
| 64 |
+
# print("")
|
| 65 |
|
| 66 |
# Append the assistant's response to maintain conversation history
|
| 67 |
self.messages.append(AIMessage(content=ai_msg.content))
|
utilities/llm/__pycache__/LlmManager.cpython-312.pyc
ADDED
|
Binary file (3.43 kB). View file
|
|
|
utilities/mongodb/MongoDBManager.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pymongo import MongoClient
|
| 2 |
+
|
| 3 |
+
class MongoDBManager:
|
| 4 |
+
def __init__(self, uri: str, db_name: str):
|
| 5 |
+
"""Initialize MongoDB connection."""
|
| 6 |
+
self.client = MongoClient(uri)
|
| 7 |
+
self.db = self.client[db_name]
|
| 8 |
+
|
| 9 |
+
def insert_one(self, collection_name: str, document: dict):
|
| 10 |
+
"""Insert a single document into a specified collection."""
|
| 11 |
+
return self.db[collection_name].insert_one(document).inserted_id
|
| 12 |
+
|
| 13 |
+
def insert_many(self, collection_name: str, documents: list):
|
| 14 |
+
"""Insert multiple documents into a specified collection."""
|
| 15 |
+
return self.db[collection_name].insert_many(documents).inserted_ids
|
| 16 |
+
|
| 17 |
+
def find_one(self, collection_name: str, query: dict):
|
| 18 |
+
"""Find a single document in a specified collection."""
|
| 19 |
+
return self.db[collection_name].find_one(query)
|
| 20 |
+
|
| 21 |
+
def find_many(self, collection_name: str, query: dict):
|
| 22 |
+
"""Find multiple documents in a specified collection."""
|
| 23 |
+
return list(self.db[collection_name].find(query))
|
| 24 |
+
|
| 25 |
+
def update_one(self, collection_name: str, query: dict, update: dict):
|
| 26 |
+
"""Update a single document in a specified collection."""
|
| 27 |
+
return self.db[collection_name].update_one(query, {'$set': update}).modified_count
|
| 28 |
+
|
| 29 |
+
def update_many(self, collection_name: str, query: dict, update: dict):
|
| 30 |
+
"""Update multiple documents in a specified collection."""
|
| 31 |
+
return self.db[collection_name].update_many(query, {'$set': update}).modified_count
|
| 32 |
+
|
| 33 |
+
def delete_one(self, collection_name: str, query: dict):
|
| 34 |
+
"""Delete a single document in a specified collection."""
|
| 35 |
+
return self.db[collection_name].delete_one(query).deleted_count
|
| 36 |
+
|
| 37 |
+
def delete_many(self, collection_name: str, query: dict):
|
| 38 |
+
"""Delete multiple documents in a specified collection."""
|
| 39 |
+
return self.db[collection_name].delete_many(query).deleted_count
|
| 40 |
+
|
| 41 |
+
def count_documents(self, collection_name: str, query: dict):
|
| 42 |
+
"""Count documents in a specified collection."""
|
| 43 |
+
return self.db[collection_name].count_documents(query)
|
| 44 |
+
|
| 45 |
+
def aggregate(self, collection_name: str, pipeline: list):
|
| 46 |
+
"""Perform aggregation queries in a specified collection."""
|
| 47 |
+
return list(self.db[collection_name].aggregate(pipeline))
|
| 48 |
+
|
| 49 |
+
def close(self):
|
| 50 |
+
"""Close the database connection."""
|
| 51 |
+
self.client.close()
|
| 52 |
+
|
| 53 |
+
def get_questions(self, collection_name: str, stage: str, strength: str, rank: str):
|
| 54 |
+
"""Retrieve the list of questions for a given stage, strength, and rank."""
|
| 55 |
+
query = {
|
| 56 |
+
"stage": stage,
|
| 57 |
+
"strength": strength,
|
| 58 |
+
"rank": rank
|
| 59 |
+
}
|
| 60 |
+
result = self.db[collection_name].find_one(query, {"_id": 0, "questions": 1})
|
| 61 |
+
return result["questions"] if result else []
|
| 62 |
+
|
utilities/mongodb/__pycache__/MongoDBManager.cpython-312.pyc
ADDED
|
Binary file (4.9 kB). View file
|
|
|