bmoxi / chat.py
HarshSanghavi's picture
Upload 4 files
0147a0e verified
raw
history blame contribute delete
No virus
2.91 kB
from langchain.memory import ConversationBufferWindowMemory
from langchain_community.chat_models import ChatOpenAI
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
# from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
from langchain.agents import AgentExecutor
from langchain.agents.format_scratchpad import format_to_openai_functions
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.schema.runnable import RunnablePassthrough
from langchain_core.utils.function_calling import convert_to_openai_function
from database_functions import set_chat_bot_name,isFirstSession,save_message
from utils import deanonymizer, create_agent
import time
is_second = False
use_extra = False
def chat_conversations(query,user_id):
is_first = False
global is_second ,use_extra
if is_second:
if 'no' in query:
query= """ "Okk no problem!
What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
"""
save_message(query,user_id)
is_second = False
return query
else:
use_extra = True
if query == "START":
if isFirstSession(user_id):
is_second = True
query = f"""
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'!
"""
save_message(query,user_id)
return query
else:
is_first = True
query = ""
# anonymizer = PresidioReversibleAnonymizer(
# analyzed_fields=["PHONE_NUMBER",
# "EMAIL_ADDRESS", "CREDIT_CARD"],
# faker_seed=42,
# )
# anonymized_input = anonymizer.anonymize(
# query
# )
start = time.time()
agent = create_agent(user_id,is_first)
print("time to create agent: ",time.time()-start)
response = agent({"input": query})['output']
print(response)
print("time to generate response by agent",time.time()-start)
# response = chat_balancer(query,response)
if "Okay, from now my name will be " in response:
set_chat_bot_name(response.split("Okay, from now my name will be ")[-1], user_id)
if use_extra:
use_extra = False
is_second = False
return response+ "\n What's up today? Need ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help? How can I help?"
return response
# output = deanonymizer(response, anonymizer)
return response