bmoxi / chat.py
HarshSanghavi's picture
fix system prompt
26f32ee verified
raw
history blame
2 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
from utils import deanonymizer, create_agent
import time
def chat_conversations(query,user_id):
is_first = False
if query == "START":
if isFirstSession(user_id):
query = """ return this message without changing it.:-
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'!"""
else:
is_first = True
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("time to generate response by agent",time.time()-start)
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)
return response
output = deanonymizer(response, anonymizer)
return output