Spaces:
Runtime error
Runtime error
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 config import settings | |
from utils import deanonymizer, create_agent, set_chat_bot_name | |
def create_new_session(user_id): | |
mongodb_chatbot_message_collection = settings.mongodb_db.get_collection(settings.MONGODB_DB_USER_SESSIONS_COLLECTION_NAME) | |
print("********************************************************** NEW SESSION *******************************************************************") | |
try: | |
user_sessions = mongodb_chatbot_message_collection.find_one({"user_id": user_id}) | |
print(user_sessions, "LAST") | |
if user_sessions: | |
session_ids = user_sessions['session_id'] | |
session_id = "bmoxi_" + str(int(session_ids[-1].split("_")[-1])+1) | |
print("session_id:", session_id) | |
mongodb_chatbot_message_collection.update_one({ "user_id": user_id },{"$push": {"session_id":session_id}}) | |
return session_id | |
else: | |
mongodb_chatbot_message_collection.insert_one({ "user_id": user_id,"session_id":["bmoxi_1"]}) | |
return "bmoxi_1" | |
except: | |
mongodb_chatbot_message_collection.insert_one({ "user_id": user_id,"session_id":["bmoxi_1"]}) | |
return "bmoxi_1" | |
def chat_conversations(query,user_id): | |
anonymizer = PresidioReversibleAnonymizer( | |
analyzed_fields=["PHONE_NUMBER", | |
"EMAIL_ADDRESS", "CREDIT_CARD"], | |
faker_seed=42, | |
) | |
anonymized_input = anonymizer.anonymize( | |
query | |
) | |
agent = create_agent(user_id) | |
response = agent({"input": query})['output'] | |
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_1") | |
return response | |
output = deanonymizer(response, anonymizer) | |
return response | |