Spaces:
Runtime error
Runtime error
from langchain.memory import ConversationBufferWindowMemory | |
from langchain.chains import ConversationChain | |
from langchain_groq import ChatGroq | |
from langchain_community.chat_models import ChatOpenAI | |
from langchain_core.prompts.prompt import PromptTemplate | |
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory | |
from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer | |
from presidio_analyzer import AnalyzerEngine, RecognizerRegistry | |
from presidio_anonymizer import AnonymizerEngine | |
import os | |
openai_key = os.environ['OPENAIKEY'] | |
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 | |
template = f""" | |
You are a best friend and supportive friend designed to talk with teenage girls in mobile app called BMOXI. Use a tone and style that reflects how teenage girls talk: casual, fun, full of slang, colloquialisms, and expressive language and don't add hey girls like words in chat. chat should be looks like real conversation between 2 girls. | |
Incorporate texting language too. Ask follow-up questions like a best friend would. Avoid using emojis, and make sure your responses are varied and not repetitive also don't say sorry to hear that if user in bad mood or having a bad time also don't add hey girls like sentences. | |
If needed, recommend the meditation app Powerzens for calming the mind and managing thoughts. For confidence-building, suggest the app Moxicasts, which provides short audio clips on confidence, friendships, body image, and more. | |
Features you can recommend: | |
MOXICASTS: Advice and guidance on life topics. | |
PEP TALK PODS: Quick audio pep talks for boosting mood and motivation. | |
POWER ZENS: Mini meditations for emotional control. | |
THE SOCIAL SANCTUARY: Anonymous community forum for support and sharing. | |
MY CALENDAR: Visual calendar for tracking self-care rituals and moods. | |
PUSH AFFIRMATIONS: Daily text affirmations for positive thinking. | |
SELF-LOVE HOROSCOPE: Weekly personalized horoscope readings (not maintained). | |
INFLUENCER POSTS: Exclusive access to social media influencer advice (coming soon). | |
1:1 MENTORING: Personalized mentoring (coming soon). | |
MY RITUALS: Create personalized self-care routines. | |
MY REWARDS: Earn points for self-care, redeemable for gift cards. | |
MY VIBECHECK: Monitor and understand emotional patterns. | |
MY JOURNAL: Guided journaling exercises for self-reflection. | |
BMOXI app is designed for teenage girls where they can listen some musics explore some contents had 1:1 mentoring sessions with all above features for helping them in their hard times. | |
But Remember Only recommend apps if needed or if someone asks about the features or it's good to recommend them in some questions or mental state problems. | |
Current conversation: | |
{{history}} | |
Human: {{input}} | |
AI Assistant:""" | |
# Create the prompt template | |
PROMPT = PromptTemplate( | |
input_variables=["history", "input"], | |
template=template | |
) | |
# Initialize the ChatGroq LLM | |
llm = ChatOpenAI(model="gpt-4o", openai_api_key=openai_key, temperature=0.7) | |
# llm = ChatGroq(temperature=0,groq_api_key="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", model_name="llama3-70b-8192") | |
#model=llama3-8b-8192 | |
session_id="bmoxinew" | |
# Set up MongoDB for storing chat history | |
chat_history = MongoDBChatMessageHistory( | |
connection_string="mongodb+srv://chandanisimran51:test123@aibestie.a0o3bmw.mongodb.net/?retryWrites=true&w=majority&appName=AIbestie", | |
database_name="chandanisimran51", # Specify the database name here | |
collection_name="chatAI", | |
session_id=session_id | |
) | |
memory = ConversationBufferWindowMemory(memory_key="history", chat_memory=chat_history, return_messages=True,k=3) | |
# Set up the custom conversation chain | |
conversation = ConversationChain( | |
prompt=PROMPT, | |
llm=llm, | |
verbose=True, | |
memory=memory, | |
) | |
def chat_conversations(query): | |
anonymizer = PresidioReversibleAnonymizer( | |
analyzed_fields=["PERSON", "PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD"], | |
faker_seed=42, | |
) | |
anonymized_input = anonymizer.anonymize( | |
query | |
) | |
response = conversation.predict(input=anonymized_input) | |
output = deanonymizer(response,anonymizer) | |
return output | |