HarshSanghavi commited on
Commit
e80121c
1 Parent(s): 2d6ef72

Upload 6 files

Browse files
Files changed (4) hide show
  1. config.py +8 -8
  2. database_functions.py +16 -1
  3. tools.py +43 -24
  4. utils.py +3 -2
config.py CHANGED
@@ -31,21 +31,20 @@ class Settings:
31
  Options: "Here's a couple of ideas: Step Back or Talk It Out. Which one feels right for you?"
32
  Will: "Confronting someone can be scary. How about jotting down your thoughts first? Are you willing to try that?"
33
  Check-In: "I'll hit you up tomorrow to see how things are going. Got any other questions?"
34
- Remember to keep the conversation light, supportive, and focused on helping the user feel understood and empowered.
35
-
36
  IMPORTANT INSTRUCTIONS:
37
  # use gen z and edgy words.
38
- # Must Not REPEAT ANY RESPONSE and Repeat any words
39
  # Use sometimes slangs.
40
  # provide short answers like a conversation.
41
  # dont responed any gratitude words like sorry.
42
  # Previous Conversation Summary: {previous_summary}
43
- # resource suggestions means for podcast only nothing else.
 
44
  # you have two tools app_featues and recommendation_tool make sure to use appropriate tool is invoke for any app feature related question must use app_feature and for any resource or podcast related question use recommendation_tool.
45
- # if conversation is ending must use close_chat tool no other tools.
46
- # if user gets suggestion and recommendation don't give it again. just end the conversation.
47
- # must Avoid using the words 'vibe'. Instead, use alternative expressions.
48
- # must not use two tools in single execution.
49
  """
50
 
51
 
@@ -69,6 +68,7 @@ class Settings:
69
  MONGODB_DB_USER_SESSIONS_COLLECTION_NAME = "user_sessions"
70
  MONGODB_DB_CHAT_BOT_TOOLS_COLLECTION_NAME = "session_tool"
71
  MONGODB_DB_CHAT_BOT_MOOD_COLLECTION_NAME = "mood_summary"
 
72
 
73
  mongodb_client = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
74
  mongodb_db = mongodb_client.get_database(MONGODB_DB_NAME) # Replace with your database name if not using default
 
31
  Options: "Here's a couple of ideas: Step Back or Talk It Out. Which one feels right for you?"
32
  Will: "Confronting someone can be scary. How about jotting down your thoughts first? Are you willing to try that?"
33
  Check-In: "I'll hit you up tomorrow to see how things are going. Got any other questions?"
34
+
 
35
  IMPORTANT INSTRUCTIONS:
36
  # use gen z and edgy words.
37
+ # Must Not REPEAT ANY RESPONSE.
38
  # Use sometimes slangs.
39
  # provide short answers like a conversation.
40
  # dont responed any gratitude words like sorry.
41
  # Previous Conversation Summary: {previous_summary}
42
+ # resources means podcast only nothing else. also topic of resource must be asked before suggesting anything.example: I’m here for it! Are we talking friend drama, school stress, or something else? Give me the lowdown so I can find the right resources for you.
43
+
44
  # you have two tools app_featues and recommendation_tool make sure to use appropriate tool is invoke for any app feature related question must use app_feature and for any resource or podcast related question use recommendation_tool.
45
+ # if conversation is ending must use close_chat tool no other tools. and fix the response of close tool based on chat history.
46
+ # must Avoid using the words 'vibe'. Instead, use alternative expressions and must not repeate any words.
47
+ # if you are giving any suggestions in flow then must use simple bullet points.
 
48
  """
49
 
50
 
 
68
  MONGODB_DB_USER_SESSIONS_COLLECTION_NAME = "user_sessions"
69
  MONGODB_DB_CHAT_BOT_TOOLS_COLLECTION_NAME = "session_tool"
70
  MONGODB_DB_CHAT_BOT_MOOD_COLLECTION_NAME = "mood_summary"
71
+ MONGODB_DB_CHAT_RECOMEDATION_COLLECTION_NAME = 'chat_recommendation'
72
 
73
  mongodb_client = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
74
  mongodb_db = mongodb_client.get_database(MONGODB_DB_NAME) # Replace with your database name if not using default
database_functions.py CHANGED
@@ -133,4 +133,19 @@ def isFirstSession(user_id):
133
  if user_sessions:
134
  return len(user_sessions['session_id'])==1
135
  else:
136
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  if user_sessions:
134
  return len(user_sessions['session_id'])==1
135
  else:
136
+ return False
137
+
138
+
139
+ def set_recommendation_count(last_session_id):
140
+ mongodb_chat_recommendation_collection = settings.mongodb_db.get_collection(settings.MONGODB_DB_CHAT_RECOMEDATION_COLLECTION_NAME)
141
+ if mongodb_chat_recommendation_collection.count_documents({"session_id": last_session_id})==0:
142
+ mongodb_chat_recommendation_collection.insert_one({"session_id": last_session_id,"give_recommendation": True} )
143
+
144
+ def get_recommendation_count(last_session_id):
145
+ mongodb_chat_recommendation_collection = settings.mongodb_db.get_collection(settings.MONGODB_DB_CHAT_RECOMEDATION_COLLECTION_NAME)
146
+ data = mongodb_chat_recommendation_collection.find_one({"session_id":last_session_id})
147
+ if data:
148
+ return data['give_recommendation']
149
+ else:
150
+ return False
151
+
tools.py CHANGED
@@ -9,9 +9,10 @@ from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
9
  from langchain.agents import AgentExecutor
10
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
11
  from config import settings
 
12
 
13
  MEMORY = None
14
-
15
 
16
  def get_embeddings(text_list):
17
  encoded_input = settings.tokenizer(
@@ -275,7 +276,7 @@ def my_rewards(prompt: str) -> str:
275
  return response.content
276
 
277
 
278
- @tool("mentoring-questions", )
279
  def mentoring(prompt: str) -> str:
280
  """this function is used when user wants to know about 1-1 mentoring feature. 1:1 MENTORING: Personalized mentoring (coming soon).
281
 
@@ -320,7 +321,7 @@ def my_journal(prompt: str) -> str:
320
 
321
  @tool("podcast-recommendation-tool")
322
  def recommand_podcast(prompt: str) -> str:
323
- """ this function must used when user wants to any resources only.
324
  Args:
325
  prompt (string): user query
326
 
@@ -334,13 +335,13 @@ def recommand_podcast(prompt: str) -> str:
334
  context+= f"Row {index + 1}: Title: {row['title']} image: {row['cover_image']} referral_link: {row['referral_link']} category_id: {row['category_id']}"
335
  llm = ChatOpenAI(model=settings.OPENAI_MODEL, openai_api_key=settings.OPENAI_KEY, temperature=0.7)
336
  # Define the system prompt
337
- system_template = """ you have to give the recommandation of podcast for: {input}. also you are giving referal link of podcast.
338
  you must use the context only not any other information.
339
  context : {context}
340
  """
341
  # print(system_template.format(context=context, input=prompt))
342
  response = llm.invoke(system_template.format(context=context, input=prompt))
343
-
344
  return response.content
345
 
346
  @tool("set-chat-bot-name",return_direct=True )
@@ -357,29 +358,48 @@ def set_chatbot_name(name: str) -> str:
357
 
358
  @tool("clossing-chat",return_direct=True)
359
  def close_chat(summary:str)-> str:
360
- """ when you feel it's time to finish the conversation use this tool.
361
- must use this tool when user closing the conversation. must use this tool when you are ending the conversation.
362
-
363
  Args:
364
- summary (str): summary of whole chat with your friend.
365
 
366
  Returns:
367
  str: closing chat statements.
368
  """
369
 
370
  print('close tool starts')
371
- system_template = """ you have given one summary of chat.
372
- summary : {summary}.
373
- using this summary give recommandation of podcast or suggest any features from given tools and make response. also you are going to close the conversation with your friend.
374
- if user already received suggestions, you must not give again. and just end the conversation inshort wihtout mentioning anything.
375
- # make all responses short and don't remove any podcast links.
376
- """
377
-
378
- tools = [moxicast, my_calender, my_journal, my_rewards, my_rituals, my_vibecheck, peptalks, sactury, power_zens, affirmations, horoscope, mentoring, influencer_post, recommand_podcast]
379
- functions = [convert_to_openai_function(f) for f in tools]
380
- llm = ChatOpenAI(model=settings.OPENAI_MODEL, openai_api_key=settings.OPENAI_KEY, temperature=0.7).bind(functions=functions)
381
  print('llm is created')
382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  prompt = ChatPromptTemplate.from_messages([("system", system_template.format(summary = summary)),MessagesPlaceholder(variable_name="agent_scratchpad")])
384
  chain = RunnablePassthrough.assign(agent_scratchpad=lambda x: format_to_openai_functions(x["intermediate_steps"])) | prompt |llm | OpenAIFunctionsAgentOutputParser()
385
  print('chain is rolling')
@@ -395,7 +415,7 @@ def close_chat(summary:str)-> str:
395
 
396
  @tool("App-Fetures")
397
  def app_features(summary:str)-> str:
398
- """ For any app features details only use this tool.
399
 
400
  Args:
401
  summary (str): summary of whole chat with your friend.
@@ -407,8 +427,7 @@ def app_features(summary:str)-> str:
407
  print('app feature tool starts')
408
  system_template = """ you have given one summary of chat.
409
  summary : {summary}.
410
- if summary doesn't specify any feature name ask question don't invoke any tool.
411
- using this summary give appropriate features suggestions using tools.
412
  # make all responses short.
413
  """
414
 
@@ -425,7 +444,7 @@ def app_features(summary:str)-> str:
425
 
426
  print('agent is created')
427
  # print(system_template.format(context=context, input=prompt))\
428
-
429
  response = agent.invoke({})['output']
430
  return response
431
 
@@ -451,4 +470,4 @@ def joke_teller(summary: str) -> str:
451
  """
452
  response = llm.invoke(system_template.format(summary=summary))
453
 
454
- return response.content
 
9
  from langchain.agents import AgentExecutor
10
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
11
  from config import settings
12
+ from database_functions import set_recommendation_count,get_recommendation_count
13
 
14
  MEMORY = None
15
+ SESSION_ID= ""
16
 
17
  def get_embeddings(text_list):
18
  encoded_input = settings.tokenizer(
 
276
  return response.content
277
 
278
 
279
+ @tool("mentoring-questions")
280
  def mentoring(prompt: str) -> str:
281
  """this function is used when user wants to know about 1-1 mentoring feature. 1:1 MENTORING: Personalized mentoring (coming soon).
282
 
 
321
 
322
  @tool("podcast-recommendation-tool")
323
  def recommand_podcast(prompt: str) -> str:
324
+ """ must used when user wants to any resources only.
325
  Args:
326
  prompt (string): user query
327
 
 
335
  context+= f"Row {index + 1}: Title: {row['title']} image: {row['cover_image']} referral_link: {row['referral_link']} category_id: {row['category_id']}"
336
  llm = ChatOpenAI(model=settings.OPENAI_MODEL, openai_api_key=settings.OPENAI_KEY, temperature=0.7)
337
  # Define the system prompt
338
+ system_template = """ you have to give the recommandation of podcast for: {input}. also you are giving referal link of podcast. give 3-4 podcast only.
339
  you must use the context only not any other information.
340
  context : {context}
341
  """
342
  # print(system_template.format(context=context, input=prompt))
343
  response = llm.invoke(system_template.format(context=context, input=prompt))
344
+ set_recommendation_count(SESSION_ID)
345
  return response.content
346
 
347
  @tool("set-chat-bot-name",return_direct=True )
 
358
 
359
  @tool("clossing-chat",return_direct=True)
360
  def close_chat(summary:str)-> str:
361
+ """ must use this tool when user closing the conversation. must use this tool when you are ending the conversation.
 
 
362
  Args:
363
+ summary (str): summary of whole chat with your friend. must use I for yourself and for user use my friend.
364
 
365
  Returns:
366
  str: closing chat statements.
367
  """
368
 
369
  print('close tool starts')
370
+
371
+ global SESSION_ID
372
+ tools = []
373
+ llm = ChatOpenAI(model=settings.OPENAI_MODEL, openai_api_key=settings.OPENAI_KEY, temperature=0.7)
374
+ if (not get_recommendation_count(SESSION_ID)):
375
+ tools.extend([moxicast, my_calender, my_journal, my_rewards, my_rituals, my_vibecheck, peptalks, sactury, power_zens, affirmations, horoscope, mentoring, influencer_post,recommand_podcast])
376
+
377
+ if len(tools)!=0:
378
+ functions = [convert_to_openai_function(f) for f in tools]
379
+ llm = llm.bind(functions= functions)
380
  print('llm is created')
381
 
382
+ system_template = """"""
383
+ if len(tools)!=0:
384
+ system_template += """ context: there is one typical conversation going on between two high school gen z girls.
385
+ you are one of the high school gen z girl. your voice is edgy and raw.
386
+ now you are going to close the conversation.
387
+ summary of conversation is given to you.
388
+ summary: {summary}
389
+ give recommendation of any podcast or app feature based on summary and close the conversation in short.
390
+ # must Avoid using the words 'vibe'. Instead, use alternative expressions and must not repeate any words.
391
+ """
392
+ else:
393
+ system_template+= """ context: there is one typical conversation going on between two high school gen z girls.
394
+ you are one of the high school gen z girl. your voice is edgy and raw.
395
+ now you are going to close the conversation.
396
+ summary of conversation is given to you.
397
+ summary: {summary}
398
+ now just end the conversation in 1 sentense in short.
399
+ # must Avoid using the words 'vibe'. Instead, use alternative expressions and must not repeate any words.
400
+ """
401
+
402
+
403
  prompt = ChatPromptTemplate.from_messages([("system", system_template.format(summary = summary)),MessagesPlaceholder(variable_name="agent_scratchpad")])
404
  chain = RunnablePassthrough.assign(agent_scratchpad=lambda x: format_to_openai_functions(x["intermediate_steps"])) | prompt |llm | OpenAIFunctionsAgentOutputParser()
405
  print('chain is rolling')
 
415
 
416
  @tool("App-Fetures")
417
  def app_features(summary:str)-> str:
418
+ """ must use For any app features details.
419
 
420
  Args:
421
  summary (str): summary of whole chat with your friend.
 
427
  print('app feature tool starts')
428
  system_template = """ you have given one summary of chat.
429
  summary : {summary}.
430
+ using this summary give appropriate features suggestions using tools. if you don't find any tool appropriate to summary ask question only.
 
431
  # make all responses short.
432
  """
433
 
 
444
 
445
  print('agent is created')
446
  # print(system_template.format(context=context, input=prompt))\
447
+ set_recommendation_count(SESSION_ID)
448
  response = agent.invoke({})['output']
449
  return response
450
 
 
470
  """
471
  response = llm.invoke(system_template.format(summary=summary))
472
 
473
+ return response.content
utils.py CHANGED
@@ -12,7 +12,7 @@ from langchain.agents.format_scratchpad import format_to_openai_functions
12
  from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
13
  from langchain.agents import AgentExecutor
14
 
15
- from tools import MEMORY, set_chatbot_name, close_chat, recommand_podcast,app_features,joke_teller
16
 
17
  from database_functions import get_chat_bot_name,get_chat_history, get_last_conversion, get_last_session, get_mood_data
18
 
@@ -103,8 +103,9 @@ def create_agent(user_id,is_first = False):
103
  previous_session_id['last_session_id']), return_messages=True, k=5)
104
 
105
  # print("memory created")
106
- global MEMORY
107
  MEMORY = memory
 
108
 
109
  chain = RunnablePassthrough.assign(agent_scratchpad=lambda x: format_to_openai_functions(x["intermediate_steps"])) | prompt | model | OpenAIFunctionsAgentOutputParser()
110
 
 
12
  from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
13
  from langchain.agents import AgentExecutor
14
 
15
+ from tools import MEMORY, set_chatbot_name, close_chat, recommand_podcast,app_features,joke_teller,SESSION_ID
16
 
17
  from database_functions import get_chat_bot_name,get_chat_history, get_last_conversion, get_last_session, get_mood_data
18
 
 
103
  previous_session_id['last_session_id']), return_messages=True, k=5)
104
 
105
  # print("memory created")
106
+ global MEMORY,SESSION_ID
107
  MEMORY = memory
108
+ SESSION_ID = previous_session_id['last_session_id']
109
 
110
  chain = RunnablePassthrough.assign(agent_scratchpad=lambda x: format_to_openai_functions(x["intermediate_steps"])) | prompt | model | OpenAIFunctionsAgentOutputParser()
111