File size: 2,341 Bytes
8b79aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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):

    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:
           query = """ Only use these templates to start conversation:-

1. Last time we talked, you mentioned [previous issue or goal]. How's that going?

2. Hey again! How's it going? 

3. What's up today? Need  ✨ Advice, ✨ a Mood Boost, ✨ a Chat, ✨ Resource Suggestions, ✨ App Features help?  How can I help?"

use any one of the question for response based on your understanding.

"""
    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)
    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 response