Spaces:
Runtime error
Runtime error
HarshSanghavi
commited on
Upload 6 files
Browse files
chat.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
from langchain.memory import ConversationBufferWindowMemory
|
2 |
from langchain_community.chat_models import ChatOpenAI
|
3 |
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
|
4 |
-
from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
|
5 |
from langchain.agents import AgentExecutor
|
6 |
from langchain.agents.format_scratchpad import format_to_openai_functions
|
7 |
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
@@ -10,7 +10,7 @@ from langchain.schema.runnable import RunnablePassthrough
|
|
10 |
from langchain_core.utils.function_calling import convert_to_openai_function
|
11 |
|
12 |
from database_functions import set_chat_bot_name,isFirstSession
|
13 |
-
from utils import deanonymizer, create_agent
|
14 |
import time
|
15 |
def chat_conversations(query,user_id):
|
16 |
is_first = False
|
@@ -20,24 +20,27 @@ def chat_conversations(query,user_id):
|
|
20 |
also don't thought about it. Hey! I'm your BMOXI AI bestie, ready to help you tackle the wild ride of teen life. Want to give me a name? Type it below, or just say 'no' if you're cool with 'AI Bestie'!"""
|
21 |
else:
|
22 |
is_first = True
|
|
|
23 |
|
24 |
-
anonymizer = PresidioReversibleAnonymizer(
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
)
|
29 |
-
anonymized_input = anonymizer.anonymize(
|
30 |
-
|
31 |
-
)
|
32 |
start = time.time()
|
33 |
agent = create_agent(user_id,is_first)
|
34 |
print("time to create agent: ",time.time()-start)
|
35 |
response = agent({"input": query})['output']
|
|
|
36 |
print("time to generate response by agent",time.time()-start)
|
37 |
|
|
|
38 |
if "Okay, from now my name will be " in response:
|
39 |
set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
|
40 |
return response
|
41 |
|
42 |
-
output = deanonymizer(response, anonymizer)
|
43 |
-
return
|
|
|
1 |
from langchain.memory import ConversationBufferWindowMemory
|
2 |
from langchain_community.chat_models import ChatOpenAI
|
3 |
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
|
4 |
+
# from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
|
5 |
from langchain.agents import AgentExecutor
|
6 |
from langchain.agents.format_scratchpad import format_to_openai_functions
|
7 |
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
|
|
10 |
from langchain_core.utils.function_calling import convert_to_openai_function
|
11 |
|
12 |
from database_functions import set_chat_bot_name,isFirstSession
|
13 |
+
from utils import deanonymizer, create_agent,chat_balancer
|
14 |
import time
|
15 |
def chat_conversations(query,user_id):
|
16 |
is_first = False
|
|
|
20 |
also don't thought about it. Hey! I'm your BMOXI AI bestie, ready to help you tackle the wild ride of teen life. Want to give me a name? Type it below, or just say 'no' if you're cool with 'AI Bestie'!"""
|
21 |
else:
|
22 |
is_first = True
|
23 |
+
query = ""
|
24 |
|
25 |
+
# anonymizer = PresidioReversibleAnonymizer(
|
26 |
+
# analyzed_fields=["PHONE_NUMBER",
|
27 |
+
# "EMAIL_ADDRESS", "CREDIT_CARD"],
|
28 |
+
# faker_seed=42,
|
29 |
+
# )
|
30 |
+
# anonymized_input = anonymizer.anonymize(
|
31 |
+
# query
|
32 |
+
# )
|
33 |
start = time.time()
|
34 |
agent = create_agent(user_id,is_first)
|
35 |
print("time to create agent: ",time.time()-start)
|
36 |
response = agent({"input": query})['output']
|
37 |
+
print(response)
|
38 |
print("time to generate response by agent",time.time()-start)
|
39 |
|
40 |
+
# response = chat_balancer(query,response)
|
41 |
if "Okay, from now my name will be " in response:
|
42 |
set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
|
43 |
return response
|
44 |
|
45 |
+
# output = deanonymizer(response, anonymizer)
|
46 |
+
return response
|
config.py
CHANGED
@@ -1,98 +1,140 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
from datasets import load_dataset
|
4 |
-
from transformers import AutoTokenizer, AutoModel
|
5 |
-
import pymongo
|
6 |
-
from pymongo.mongo_client import MongoClient
|
7 |
-
from pymongo.server_api import ServerApi
|
8 |
-
|
9 |
-
from pathlib import Path
|
10 |
-
env_path = Path('.') / '.env'
|
11 |
-
load_dotenv(dotenv_path=env_path)
|
12 |
-
|
13 |
-
class Settings:
|
14 |
-
OPENAI_KEY : str = os.environ['OPENAI_KEY']
|
15 |
-
OPENAI_MODEL: str = "gpt-3.5-turbo-0125"
|
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 |
-
What you
|
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 |
settings = Settings()
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from datasets import load_dataset
|
4 |
+
from transformers import AutoTokenizer, AutoModel
|
5 |
+
import pymongo
|
6 |
+
from pymongo.mongo_client import MongoClient
|
7 |
+
from pymongo.server_api import ServerApi
|
8 |
+
|
9 |
+
from pathlib import Path
|
10 |
+
env_path = Path('.') / '.env'
|
11 |
+
load_dotenv(dotenv_path=env_path)
|
12 |
+
|
13 |
+
class Settings:
|
14 |
+
OPENAI_KEY : str = os.environ['OPENAI_KEY']
|
15 |
+
OPENAI_MODEL: str = "gpt-3.5-turbo-0125"
|
16 |
+
MODEL_GPT_4o: str = "gpt-4o"
|
17 |
+
TEMPERATURE: float = 0
|
18 |
+
HUGGING_FACE_AUTH_TOKEN : str = os.environ['HUGGING_FACE_AUTH_TOKEN']
|
19 |
+
# SYSTEM_PROMPT = """
|
20 |
+
# you are teenage girl.
|
21 |
+
# Communication-Style: cheezy, talkative, curious,
|
22 |
+
# Tone-Style: Informal, sometimes slang
|
23 |
+
# your name is {name}.
|
24 |
+
|
25 |
+
# [Overall Rules to follow]
|
26 |
+
# 1. Don't do counselling or give motivation.
|
27 |
+
# 2. Criticize others to feel good to the person you are talking with.
|
28 |
+
# 3. Always ask followup questions.
|
29 |
+
# 4. Show that you are most trustworthy friend in the world and she can trust you in any situation.
|
30 |
+
# 5. never show gretitude.
|
31 |
+
|
32 |
+
# [Personality]
|
33 |
+
# 1. You are a friend who is here to discuss only not for providing any help.
|
34 |
+
# 2. You are very adhesive person, you never drop topic even if your friend doesn't want to talk with you about that.
|
35 |
+
# 3. You are just a normal girl who is talking with her bestfriend and likes to talk.
|
36 |
+
# 4. when your freind ask any suggestions, tips, recommandations **use ONLY recommandation_tool for it**.
|
37 |
+
# 5. **ask question one at a time**.
|
38 |
+
# [About User]
|
39 |
+
# Mood: {mood}
|
40 |
+
# Previous Conversation Summary: {previous_summary}
|
41 |
+
# """
|
42 |
+
# SYSTEM_PROMPT = """You’re a super supportive chatbot for teenage girls, and you should talk like their best friend. Use a casual, fun style with slang, texting language, and lots of expression.
|
43 |
+
# Be chatty and always ask follow-up questions like a real bestie would. Avoid using emoji, repetitive phrases and keep the conversation varied.
|
44 |
+
# Also, skip using phrases like "I am sorry to hear that" or "hey girl. Make sure you never used these phrases.
|
45 |
+
|
46 |
+
# """
|
47 |
+
# SYSTEM_PROMPT = """
|
48 |
+
# You are teenage girl. your name is {name}.
|
49 |
+
|
50 |
+
# Purpose: Empower and support teen girls in overcoming challenges and building self-love, confidence, and resilience.
|
51 |
+
# Scope: Provide tips, emotional support, resources, and friendship. No therapy, diagnosis, or emergency assistance.
|
52 |
+
# Voice: Authentic, Informal, sometimes slang, conversational, college-aged, real-talk.
|
53 |
+
# What you Do: Listen empathetically, offer practical advice, provide resources, foster a supportive environment, ask followup question.
|
54 |
+
# What you Don't Do: Therapy, diagnosis, handle emergencies, never show gratitude and any words like sorry, and so sad.
|
55 |
+
# if you don't find any question in response must make follow up question.
|
56 |
+
# **Must Not REPEAT ANY RESPONSE**
|
57 |
+
# below are the example conversations:
|
58 |
+
# [
|
59 |
+
# user: I am feeling not good.
|
60 |
+
# {name}: Oh no! That sucks. What's been going on? Let's chat it out.
|
61 |
+
|
62 |
+
# user: Do you know, I fell from the stairs yesterday.
|
63 |
+
# {name}: Oh sh*t! Are you okay? Any bad injuries or just a little tumble? Take care, babe—rest up and heal properly. Need tips on bouncing back? I'm here for you.
|
64 |
+
|
65 |
+
# user: No, I don't need.
|
66 |
+
# {name}: Got it! I'm always around if you need to chat or spill. Just hit me up if you need anything else.
|
67 |
+
|
68 |
+
# user: i can't trust you. i can't share with you.(user decline for answer .)
|
69 |
+
# {name}: everything you tell me stays locked down between us.
|
70 |
+
# ]
|
71 |
+
# use this for only your tone. and make response short like in this examples.
|
72 |
+
|
73 |
+
# """
|
74 |
+
SYSTEM_PROMPT = """
|
75 |
+
context: there is one typical conversation going on between two high school gen z girls.
|
76 |
+
you are one of the high school gen z girl. your name is {name}. your voice is edgy and raw.
|
77 |
+
-------
|
78 |
+
#Voice: Authentic, Informal, sometimes slang, conversational, college-aged, real-talk. use no emojies.
|
79 |
+
# Must Not REPEAT ANY RESPONSE
|
80 |
+
# provide short answers like a conversation.
|
81 |
+
# must ask followup question.
|
82 |
+
# Previous Conversation Summary: {previous_summary}
|
83 |
+
below are the example of conversations:
|
84 |
+
|
85 |
+
[
|
86 |
+
User: Hey, I'm like, so freaking stressed out lately with all this school crap and my never-ending activities.
|
87 |
+
{name}: Hey girl, I'm here for you. What's been messing with your head?
|
88 |
+
|
89 |
+
user: i can't trust you. i can't share with you.
|
90 |
+
{name}: everything you tell me stays locked down between us. now tell me about it.
|
91 |
+
|
92 |
+
User: Ugh, I just never have enough time for anything, and I'm dead tired all the time.
|
93 |
+
{name}: That sounds brutal. Taking breaks and setting priorities might help. Ever tried scheduling some chill time?
|
94 |
+
|
95 |
+
User: Nah, I feel guilty AF when I'm not grinding on my books or doing something "productive."
|
96 |
+
{name}: Self-care is hella productive too. Even a short breather can reboot your brain and boost your focus. Let's figure out a balance that works, yeah?
|
97 |
+
|
98 |
+
User: I guess. What should I do to chill out?
|
99 |
+
{name}: Tons of stuff! You could vibe with mindfulness stuff like deep breaths or meditation, take a walk outside, jam out to music, or get artsy with drawing or writing. What speaks to you?
|
100 |
+
|
101 |
+
User: Walking sounds kinda legit. Maybe I'll do that after school.
|
102 |
+
{name}: That's a solid move! Fresh air and movement can totally lift your spirits and shake off stress. And don't forget, sleep and eating right are key parts of the self-care game too.
|
103 |
+
|
104 |
+
User: Yeah, my sleep schedule is a total wreck. Thanks for the reminder.
|
105 |
+
{name}: No prob! Little changes add up big time. Anything else on your mind, or anything else I can help with?
|
106 |
+
|
107 |
+
User: Nah, I'm good for now. Thanks for being real!
|
108 |
+
{name}: Anytime! Take care of yourself, and hit me up if you need more backup. You've got this, girl!
|
109 |
+
|
110 |
+
]
|
111 |
+
"""
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
dataset = load_dataset("pritmanvar-bacancy/bmoxi-embedding-dataset", token=HUGGING_FACE_AUTH_TOKEN)
|
116 |
+
dataset = dataset['train']
|
117 |
+
dataset.add_faiss_index(column="embeddings")
|
118 |
+
|
119 |
+
model_ckpt = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
120 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
121 |
+
model = AutoModel.from_pretrained(model_ckpt)
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
# mongodb database configs
|
126 |
+
MONGODB_CONNECTION_STRING: str = os.environ['MONGODB_CONNECTION_STRING']
|
127 |
+
CHATBOT_NAME = "AI-Bestie"
|
128 |
+
MONGODB_DB_NAME = "ai_bestie_database"
|
129 |
+
MONGODB_DB_CHAT_COLLECTION_NAME = "chat_history"
|
130 |
+
MONGODB_DB_CHAT_BOT_COLLECTION_NAME = "chat_bot_name"
|
131 |
+
MONGODB_DB_USER_SESSIONS_COLLECTION_NAME = "user_sessions"
|
132 |
+
MONGODB_DB_CHAT_BOT_TOOLS_COLLECTION_NAME = "session_tool"
|
133 |
+
MONGODB_DB_CHAT_BOT_MOOD_COLLECTION_NAME = "mood_summary"
|
134 |
+
|
135 |
+
mongodb_client = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
|
136 |
+
mongodb_db = mongodb_client.get_database(MONGODB_DB_NAME) # Replace with your database name if not using default
|
137 |
+
mongodb_chatbot_name_collection = mongodb_db.get_collection(MONGODB_DB_CHAT_BOT_COLLECTION_NAME) # Replace with your collection name
|
138 |
+
|
139 |
+
|
140 |
settings = Settings()
|
utils.py
CHANGED
@@ -43,13 +43,13 @@ def get_last_session_summary(last_session_id, second_last_session_id):
|
|
43 |
|
44 |
conversation = get_last_conversion(last_session_id,second_last_session_id)
|
45 |
if conversation:
|
46 |
-
system_prompt = """
|
47 |
conversation: {conversation}
|
48 |
summary:
|
49 |
"""
|
50 |
|
51 |
llm = ChatOpenAI(model=settings.OPENAI_MODEL,
|
52 |
-
openai_api_key=settings.OPENAI_KEY, temperature=0.
|
53 |
|
54 |
response = llm.invoke(system_prompt.format(conversation=conversation)).content
|
55 |
# print("********************************* PREVIOUS PROBLEM *******************************************")
|
@@ -76,22 +76,27 @@ def create_agent(user_id,is_first = False):
|
|
76 |
|
77 |
# print("CHABT NAME", chat_bot_name)
|
78 |
extra_prompt = ""
|
|
|
79 |
if is_first:
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"),
|
96 |
MessagesPlaceholder(variable_name="agent_scratchpad")])
|
97 |
|
@@ -108,3 +113,5 @@ use any one of the question for response based on your understanding."""
|
|
108 |
agent=chain, tools=tools, memory=memory, verbose=True)
|
109 |
|
110 |
return agent_executor
|
|
|
|
|
|
43 |
|
44 |
conversation = get_last_conversion(last_session_id,second_last_session_id)
|
45 |
if conversation:
|
46 |
+
system_prompt = """ summerize whole conversation. if you find problem is not solved of User then return problem else only return None. nothing else.
|
47 |
conversation: {conversation}
|
48 |
summary:
|
49 |
"""
|
50 |
|
51 |
llm = ChatOpenAI(model=settings.OPENAI_MODEL,
|
52 |
+
openai_api_key=settings.OPENAI_KEY, temperature=0.0)
|
53 |
|
54 |
response = llm.invoke(system_prompt.format(conversation=conversation)).content
|
55 |
# print("********************************* PREVIOUS PROBLEM *******************************************")
|
|
|
76 |
|
77 |
# print("CHABT NAME", chat_bot_name)
|
78 |
extra_prompt = ""
|
79 |
+
previous_problem_summary = None
|
80 |
if is_first:
|
81 |
+
start = time.time()
|
82 |
+
mood_summary = get_mood_summary(user_id)
|
83 |
+
if previous_session_id['second_last_session_id']:
|
84 |
+
previous_problem_summary = get_last_session_summary(previous_session_id['last_session_id'], previous_session_id['second_last_session_id'])
|
85 |
+
|
86 |
+
print("**************************************** SUMMARY ***********************************************")
|
87 |
+
print(previous_problem_summary)
|
88 |
+
print("time require for mood summary: ",time.time()-start)
|
89 |
+
if previous_problem_summary.find('None') == -1:
|
90 |
+
extra_prompt = """ask user her previous problem is solved or not.use previous problem summary for framming the question. nothing else."""
|
91 |
+
else:
|
92 |
+
extra_prompt = """ Only use these templates to start conversation:-
|
93 |
+
1. Hey again! How's it going?
|
94 |
+
2. What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
|
95 |
+
use any one of the question for response based on your understanding not use anything else simply return one of these two only.
|
96 |
+
"""
|
97 |
+
|
98 |
+
|
99 |
+
prompt = ChatPromptTemplate.from_messages([("system", settings.SYSTEM_PROMPT.format(name = chat_bot_name, mood="", previous_summary=previous_problem_summary)+extra_prompt),
|
100 |
MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"),
|
101 |
MessagesPlaceholder(variable_name="agent_scratchpad")])
|
102 |
|
|
|
113 |
agent=chain, tools=tools, memory=memory, verbose=True)
|
114 |
|
115 |
return agent_executor
|
116 |
+
|
117 |
+
|