File size: 5,769 Bytes
8b79aed
 
 
 
 
 
 
 
 
 
 
 
 
 
e80121c
8b79aed
2d6ef72
8b79aed
177f919
8b79aed
 
 
 
 
 
 
177f919
 
8b79aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287750f
 
 
 
8b79aed
 
 
 
 
5039b99
8b79aed
 
 
 
 
 
26f32ee
8b79aed
 
 
 
2d6ef72
 
8b79aed
 
 
177f919
 
8b79aed
 
26f32ee
5039b99
26f32ee
5039b99
 
287750f
5039b99
 
 
 
 
207ae44
5039b99
207ae44
 
287750f
5039b99
 
 
 
 
8b79aed
 
 
 
 
 
 
2d6ef72
8b79aed
e80121c
2d6ef72
e80121c
8b79aed
 
 
 
 
 
 
5039b99
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
import time
from transformers import AutoTokenizer, AutoModel
from langchain_community.chat_models import ChatOpenAI
import pandas as pd
from config import settings
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.memory import ConversationBufferWindowMemory
from langchain.schema.runnable import RunnablePassthrough
from langchain.agents.format_scratchpad import format_to_openai_functions
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
from langchain.agents import AgentExecutor

from tools import MEMORY, set_chatbot_name, close_chat, recommand_podcast,app_features,joke_teller,SESSION_ID

from database_functions import get_chat_bot_name,get_chat_history, get_last_conversion, get_last_session, get_mood_data


def get_mood_summary(user_id):

    data = get_mood_data(user_id)
    system_prompt = """You are an descripting assistant that provides the breif description of the user data which is related to their mood tracking activity. Properly descibe the reason for their mood.Avoid times and dates in description

                     Here is the user data: {data}"""

    llm = ChatOpenAI(model=settings.OPENAI_MODEL,
                     openai_api_key=settings.OPENAI_KEY, temperature=0.0)
    
    return llm.invoke(system_prompt.format(data=data)).content


def deanonymizer(input, anonymizer):
    input = anonymizer.deanonymize(input)
    map = anonymizer.deanonymizer_mapping
    if map:
        for k in map["PERSON"]:
            names = k.split(" ")
            for i in names:
                input = input.replace(i, map["PERSON"][k])
    return input



def get_last_session_summary(last_session_id, second_last_session_id):

    conversation = get_last_conversion(last_session_id,second_last_session_id) 
    if conversation:
        system_prompt = """ context: there is one typical conversation going on between two high school gen z girls.

        you are one of the high school gen z girl. your voice is edgy and raw. 

        must use I for AI named BMOXI and for human use my friend. in summary.

        this is your conversation with your best friend. summerize whole conversation and if you found any question can be asked from chat then return summary and qustion else return summary and append None at last.

        conversation: {conversation}

        summary: 

        """

        llm = ChatOpenAI(model=settings.OPENAI_MODEL,
                        openai_api_key=settings.OPENAI_KEY, temperature=0.0)

        response = llm.invoke(system_prompt.format(conversation=conversation)).content
        return response
    else:
        return ""

def create_agent(user_id,is_first = False):
    # print("get user Id**********************",user_id)
    
    previous_session_id = get_last_session(user_id)
    # print(previous_session_id)
   
    tools = [set_chatbot_name,close_chat,recommand_podcast,app_features,joke_teller]

    functions = [convert_to_openai_function(f) for f in tools]
    model = ChatOpenAI(model_name=settings.OPENAI_MODEL,
                       openai_api_key=settings.OPENAI_KEY, frequency_penalty= 1, temperature=0.7).bind(functions=functions)
 
    chat_bot_name = get_chat_bot_name(user_id)

    extra_prompt = ""
    previous_problem_summary = None
    if is_first:
        start = time.time()
        mood_summary = get_mood_summary(user_id)
        print(previous_session_id)
        if previous_session_id['second_last_session_id']:
            previous_problem_summary = get_last_session_summary(previous_session_id['last_session_id'], previous_session_id['second_last_session_id'])

        print("time require for mood summary: ",time.time()-start)
        if previous_problem_summary.find('None') == -1:
            extra_prompt = f"""ask user her previous problem is solved or not.use previous problem summary for framming the question. must include her name which is {user_id} .nothing else."""
        else:
            extra_prompt = f""" Only use these templates to start conversation:-

                                1. Hey again {user_id}! How's it going? 

                                2. hey again {user_id}! 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 not use anything else simply return one of these two only.

                            """
                            
                            
    prompt = ChatPromptTemplate.from_messages([("system", settings.SYSTEM_PROMPT.format(name = chat_bot_name, mood="", previous_summary=previous_problem_summary)+extra_prompt),
                                               MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"),
                                               MessagesPlaceholder(variable_name="agent_scratchpad")])



    memory = ConversationBufferWindowMemory(memory_key="chat_history", chat_memory=get_chat_history(
        previous_session_id['last_session_id']), return_messages=True, k=5)
    
    # print("memory created")
    global MEMORY,SESSION_ID
    MEMORY = memory
    SESSION_ID = previous_session_id['last_session_id']

    chain = RunnablePassthrough.assign(agent_scratchpad=lambda x: format_to_openai_functions(x["intermediate_steps"])) | prompt | model | OpenAIFunctionsAgentOutputParser()

    agent_executor = AgentExecutor(
        agent=chain, tools=tools, memory=memory, verbose=True)

    return agent_executor