Spaces:
Build error
Build error
HarshSanghavi
commited on
Commit
•
177f919
1
Parent(s):
e8f2247
Upload 3 files
Browse files
chat.py
CHANGED
@@ -1,46 +1,65 @@
|
|
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
|
8 |
-
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
-
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
8 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
+
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 |
+
|
16 |
+
|
17 |
+
is_second = False
|
18 |
+
use_extra = False
|
19 |
+
def chat_conversations(query,user_id):
|
20 |
+
is_first = False
|
21 |
+
global is_second ,use_extra
|
22 |
+
if is_second:
|
23 |
+
if 'no' in query:
|
24 |
+
query= """ "Okk no problem!
|
25 |
+
What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
|
26 |
+
Ask this question only without thinking anything.
|
27 |
+
"""
|
28 |
+
is_second = False
|
29 |
+
else:
|
30 |
+
use_extra = True
|
31 |
+
if query == "START":
|
32 |
+
if isFirstSession(user_id):
|
33 |
+
is_second = True
|
34 |
+
query = """ return this message without changing it.:-
|
35 |
+
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'!"""
|
36 |
+
else:
|
37 |
+
is_first = True
|
38 |
+
query = ""
|
39 |
+
|
40 |
+
# anonymizer = PresidioReversibleAnonymizer(
|
41 |
+
# analyzed_fields=["PHONE_NUMBER",
|
42 |
+
# "EMAIL_ADDRESS", "CREDIT_CARD"],
|
43 |
+
# faker_seed=42,
|
44 |
+
# )
|
45 |
+
# anonymized_input = anonymizer.anonymize(
|
46 |
+
# query
|
47 |
+
# )
|
48 |
+
start = time.time()
|
49 |
+
agent = create_agent(user_id,is_first)
|
50 |
+
print("time to create agent: ",time.time()-start)
|
51 |
+
response = agent({"input": query})['output']
|
52 |
+
print(response)
|
53 |
+
print("time to generate response by agent",time.time()-start)
|
54 |
+
|
55 |
+
# response = chat_balancer(query,response)
|
56 |
+
if "Okay, from now my name will be " in response:
|
57 |
+
set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
|
58 |
+
if use_extra:
|
59 |
+
use_extra = False
|
60 |
+
is_second = False
|
61 |
+
return response+ "\n What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
|
62 |
+
return response
|
63 |
+
|
64 |
+
# output = deanonymizer(response, anonymizer)
|
65 |
+
return response
|
utils.py
CHANGED
@@ -16,6 +16,7 @@ from tools import moxicast, my_calender, my_journal, my_rewards, my_rituals, my_
|
|
16 |
|
17 |
from database_functions import get_chat_bot_name,get_chat_history, get_last_conversion, get_last_session, get_mood_data,use_tools
|
18 |
|
|
|
19 |
def get_mood_summary(user_id):
|
20 |
|
21 |
data = get_mood_data(user_id)
|
@@ -23,7 +24,8 @@ def get_mood_summary(user_id):
|
|
23 |
Here is the user data: {data}"""
|
24 |
|
25 |
llm = ChatOpenAI(model=settings.OPENAI_MODEL,
|
26 |
-
openai_api_key=settings.OPENAI_KEY, temperature=0.
|
|
|
27 |
return llm.invoke(system_prompt.format(data=data)).content
|
28 |
|
29 |
|
@@ -70,11 +72,10 @@ def create_agent(user_id,is_first = False):
|
|
70 |
|
71 |
functions = [convert_to_openai_function(f) for f in tools]
|
72 |
model = ChatOpenAI(model_name=settings.OPENAI_MODEL,
|
73 |
-
openai_api_key=settings.OPENAI_KEY, frequency_penalty= 1, temperature=
|
74 |
-
|
75 |
chat_bot_name = get_chat_bot_name(user_id)
|
76 |
|
77 |
-
# print("CHABT NAME", chat_bot_name)
|
78 |
extra_prompt = ""
|
79 |
previous_problem_summary = None
|
80 |
if is_first:
|
@@ -83,8 +84,6 @@ def create_agent(user_id,is_first = False):
|
|
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."""
|
|
|
16 |
|
17 |
from database_functions import get_chat_bot_name,get_chat_history, get_last_conversion, get_last_session, get_mood_data,use_tools
|
18 |
|
19 |
+
|
20 |
def get_mood_summary(user_id):
|
21 |
|
22 |
data = get_mood_data(user_id)
|
|
|
24 |
Here is the user data: {data}"""
|
25 |
|
26 |
llm = ChatOpenAI(model=settings.OPENAI_MODEL,
|
27 |
+
openai_api_key=settings.OPENAI_KEY, temperature=0.0)
|
28 |
+
|
29 |
return llm.invoke(system_prompt.format(data=data)).content
|
30 |
|
31 |
|
|
|
72 |
|
73 |
functions = [convert_to_openai_function(f) for f in tools]
|
74 |
model = ChatOpenAI(model_name=settings.OPENAI_MODEL,
|
75 |
+
openai_api_key=settings.OPENAI_KEY, frequency_penalty= 1, temperature=0.7).bind(functions=functions)
|
76 |
+
|
77 |
chat_bot_name = get_chat_bot_name(user_id)
|
78 |
|
|
|
79 |
extra_prompt = ""
|
80 |
previous_problem_summary = None
|
81 |
if is_first:
|
|
|
84 |
if previous_session_id['second_last_session_id']:
|
85 |
previous_problem_summary = get_last_session_summary(previous_session_id['last_session_id'], previous_session_id['second_last_session_id'])
|
86 |
|
|
|
|
|
87 |
print("time require for mood summary: ",time.time()-start)
|
88 |
if previous_problem_summary.find('None') == -1:
|
89 |
extra_prompt = """ask user her previous problem is solved or not.use previous problem summary for framming the question. nothing else."""
|