Spaces:
Runtime error
Runtime error
Update chat.py
Browse files
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 |
-
|
| 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 |
-
print(response)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 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
|
|
|