HarshSanghavi commited on
Commit
bd5bc52
·
verified ·
1 Parent(s): 7ebc5b4

Update chat.py

Browse files
Files changed (1) hide show
  1. chat.py +70 -71
chat.py CHANGED
@@ -1,71 +1,70 @@
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,save_message
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
- """
27
-
28
- save_message(query,user_id)
29
- is_second = False
30
- return query
31
- else:
32
- use_extra = True
33
- if query == "START":
34
- if isFirstSession(user_id):
35
- is_second = True
36
- query = f"""
37
- Hey {user_id}! 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'!
38
- """
39
- save_message(query,user_id)
40
- return query
41
- else:
42
- is_first = True
43
- query = ""
44
-
45
- # anonymizer = PresidioReversibleAnonymizer(
46
- # analyzed_fields=["PHONE_NUMBER",
47
- # "EMAIL_ADDRESS", "CREDIT_CARD"],
48
- # faker_seed=42,
49
- # )
50
- # anonymized_input = anonymizer.anonymize(
51
- # query
52
- # )
53
- start = time.time()
54
- agent = create_agent(user_id,is_first)
55
-
56
- print("time to create agent: ",time.time()-start)
57
- response = agent({"input": query})['output']
58
- print(response)
59
- print("time to generate response by agent",time.time()-start)
60
-
61
- # response = chat_balancer(query,response)
62
- if "Okay, from now my name will be " in response:
63
- set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
64
- if use_extra:
65
- use_extra = False
66
- is_second = False
67
- return response+ "\n What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
68
- return response
69
-
70
- # output = deanonymizer(response, anonymizer)
71
- return response
 
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,save_message
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! What's up today?"
25
+ """
26
+
27
+ save_message(query,user_id)
28
+ is_second = False
29
+ return query
30
+ else:
31
+ use_extra = True
32
+ if query == "START":
33
+ if isFirstSession(user_id):
34
+ is_second = True
35
+ query = f"""
36
+ Hey {user_id}! 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'!
37
+ """
38
+ save_message(query,user_id)
39
+ return query
40
+ else:
41
+ is_first = True
42
+ query = ""
43
+
44
+ # anonymizer = PresidioReversibleAnonymizer(
45
+ # analyzed_fields=["PHONE_NUMBER",
46
+ # "EMAIL_ADDRESS", "CREDIT_CARD"],
47
+ # faker_seed=42,
48
+ # )
49
+ # anonymized_input = anonymizer.anonymize(
50
+ # query
51
+ # )
52
+ start = time.time()
53
+ agent = create_agent(user_id,is_first)
54
+
55
+ print("time to create agent: ",time.time()-start)
56
+ response = agent({"input": query})['output']
57
+ print(response)
58
+ print("time to generate response by agent",time.time()-start)
59
+
60
+ # response = chat_balancer(query,response)
61
+ if "Okay, from now my name will be " in response:
62
+ set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
63
+ if use_extra:
64
+ use_extra = False
65
+ is_second = False
66
+ return response+ "\n So, What's up today?"
67
+ return response
68
+
69
+ # output = deanonymizer(response, anonymizer)
70
+ return response