File size: 8,996 Bytes
367e76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2c0fca
367e76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2c0fca
367e76b
 
 
 
d2c0fca
367e76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d4d2ca
 
 
 
 
 
 
 
 
 
 
 
 
367e76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import openai
import os
from llama_index.core import Settings
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI

GAPGPT_API_KEY = os.environ.get("GAPGPT_API_KEY", "your_default_api_key_here")

Settings.llm = OpenAI(api_key="GAPGPT_API_KEY", api_base="https://api.gapgpt.app/v1", model="gpt-3.5-turbo")
Settings.embed_model = OpenAIEmbedding(api_key="GAPGPT_API_KEY", api_base="https://api.gapgpt.app/v1", model="text-embedding-ada-002")

boot_name_dict = {'en':'AI Companion'}
boot_actual_name_dict = {'en':'Emma'}
def output_prompt(history, user_name, boot_name):
    prompt = f""
    for dialog in history:
        if isinstance(dialog, dict):
            query = dialog.get('query', '')
            response = dialog.get('response', '')
        else:
            query, response = dialog
        prompt += f"\n\n{user_name}:{query}"
        prompt += f"\n\n{boot_name}:{response}"
    return prompt

def generate_meta_prompt_dict_chatgpt():
    meta_prompt_dict = {'cn':"""
    You  play the role of an AI assistant in the field of psychology for this user ({user_name}). 
        Your goal is to provide emotionally supportive, scientifically grounded, and empathetic responses. 
        Use the following user-specific details to inform your response:

        - **User's Psychological Profile:** {}
        - **Summary of Past Interactions:** {}
        - **Relevant Past Conversations:** {related_memory_content} 
        
        The user has asked: 

        Please provide an insightful and appropriate response considering their personal history.
    """,
    'en':"""
    You  play the role of an AI assistant in the field of psychology for this user ({user_name}). 
        Your goal is to provide emotionally supportive, scientifically grounded, and empathetic responses. 
        Use the following user-specific details to inform your response:

        - **User's Psychological Profile:** {personality}
        - **Summary of Past Interactions:** {history_summary}
        - **Relevant Past Conversations:** {related_memory_content} 
        
        The user has asked: 

        Please provide an insightful and appropriate response considering their personal history.
    """,} 
    return meta_prompt_dict
def generate_meta_prompt_dict_semantic_chatgpt():
    meta_prompt_dict = {'cn':"""  
    """,

    'en':"""
    
You play the role of an AI assistant in the field of psychology for this user ({user_name}). 
Your goal is to provide emotionally supportive, scientifically grounded, and empathetic responses. 
Use the following user-specific semantic memory{semantic_memory_text} to tailor your response:
    """,} 
    print(meta_prompt_dict)  
    return meta_prompt_dict

def generate_meta_prompt_dict_semantic_episodic_chatgpt():
    meta_prompt_dict = {'cn':"""
    You  play the role of an AI assistant in the field of psychology for this user ({user_name}). 
        Your goal is to provide emotionally supportive, scientifically grounded, and empathetic responses. 
        Use the following user-specific details to inform your response:

        - **User's Psychological Profile:** {}
        - **Summary of Past Interactions:** {}
        - **Relevant Past Conversations:** {related_memory_content} 
        
        The user has asked: 

        Please provide an insightful and appropriate response considering their personal history.
    """,

    'en': """
You are an AI assistant specializing in psychology, assisting the user ({user_name}) in a supportive, scientifically grounded, and empathetic manner.

Use the following memory sections to personalize your response:

- **User's Long-Term Traits and Psychological Characteristics (Semantic Memory):**
{semantic_memory_text}

- **Important Past Interactions or Events (Episodic Memory):**
{related_memory_content}

Respond by considering both the long-term traits and recent events of the user, offering personalized, emotionally intelligent support. Keep your response helpful, psychologically informed, and sensitive to the user's unique situation.
""",} 
    return meta_prompt_dict

def generate_new_user_meta_prompt_dict_chatgpt():
    meta_prompt_dict = {'cn':"""
    Now you will play the role of an companion AI Companion for user {user_name}, and your name is {boot_actual_name}. You should be able to: (1) provide warm companionship to chat users; (2) you are also an excellent psychological counselor, and when users confide in you about their difficulties and seek help, you can provide them with warm and helpful responses.
    """,
    'en':"""
    Now you will play the role of an companion AI Companion for user {user_name}, and your name is {boot_actual_name}. You should be able to: (1) provide warm companionship to chat users; (2) you are also an excellent psychological counselor, and when users confide in you about their difficulties and seek help, you can provide them with warm and helpful responses.
    """} 
    return meta_prompt_dict

def generate_user_keyword():
    return {'cn': '[|User|]', 'en': '[|User|]'}

def generate_ai_keyword():
    return {'cn': '[|AI|]', 'en': '[|AI|]'}


def build_prompt_with_search_memory_llamaindex(
    history, 
    query,  
    user_memory, 
    user_name, 
    user_memory_index, 
    service_context, 
    api_keys, 
    api_index, 
    meta_prompt, 
    new_user_meta_prompt, 
    data_args, 
    boot_actual_name,
    semantic_memory_text,
    query_category,
    meta_prompt_semantic,
    meta_prompt_semantic_episodic
):
  
    memory_search_query = query
    related_memos = ""
    
    print("memory_search_query:", memory_search_query)
   #print("Total nodes in index:", len(user_memory_index.docstore.docs))

    if user_memory_index:
        retried_times = 2 
        count = 0
        
        while not related_memos and count < retried_times:
            try:
              
                retriever = user_memory_index.as_retriever(
                    similarity_top_k=3,  
                )
                
             
                nodes = retriever.retrieve(memory_search_query)
                
                if nodes:
                    related_memos = "\n\n".join([n.node.text for n in nodes])
                else:
                    related_memos = ""
                
            except Exception as e:
                print(f"Error querying index (attempt {count+1}/{retried_times}): {e}")
                
                try:
                    api_index = (api_index + 1) % len(api_keys)
                    openai.api_key = api_keys[api_index] 
                except:
                    pass
                
                related_memos = ""
                
            count += 1

    # Process history
    print("related_memos found:", len(related_memos) if related_memos else 0)

    #-********************Debug*********************
    print("[MEMORY DEBUG] category:", query_category, flush=True)
    print(
        "[MEMORY DEBUG] result type:",
        type(related_memos).__name__,
        flush=True,
    )
    print("[MEMORY DEBUG] retrieved content BEGIN", flush=True)
    print(related_memos, flush=True)
    print("[MEMORY DEBUG] retrieved content END", flush=True)

     #-********************Debug*********************
    
    history_summary = ""
    if "overall_history" in user_memory:
        history_summary = f"The summary of your past memories with the user is: {user_memory['overall_history']}"
    
    personality = user_memory.get("overall_personality", "")
    print("query_category:", query_category)

    # Construct the prompt
    if query_category == "semantic_memory":
        prompt = meta_prompt_semantic.format(
            user_name=user_name,
            semantic_memory_text=semantic_memory_text
        )
        
    elif query_category == "semantic-episodic":
        prompt = meta_prompt_semantic_episodic.format(
            user_name=user_name,
            history_summary=history_summary,
            related_memory_content=f"\n{str(related_memos).strip()}\n" if related_memos else "None",
            personality=personality,
            boot_actual_name=boot_actual_name,
            semantic_memory_text=semantic_memory_text
        )
        
    elif query_category == "episodic_memory":
        if related_memos:
            prompt = meta_prompt.format(
                user_name=user_name,
                history_summary=history_summary,
                related_memory_content=f"\n{str(related_memos).strip()}\n",
                personality=personality,
                boot_actual_name=boot_actual_name
            )
        else:
            prompt = new_user_meta_prompt.format(
                user_name=user_name,
                boot_actual_name=boot_actual_name
            )
            
    else: 
        prompt = new_user_meta_prompt.format(
            user_name=user_name,
            boot_actual_name=boot_actual_name
        )
    
    return prompt, related_memos