Spaces:
Runtime error
Runtime error
File size: 3,705 Bytes
dc43547 41b3406 df160a9 41b3406 cbb545f f7b2977 cbb545f 58a43b0 cbb545f 58a43b0 de60b62 58a43b0 de60b62 58a43b0 de60b62 cbb545f de60b62 dc43547 cbb545f dc43547 90ef3be dc43547 41b3406 |
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 |
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationChain
from langchain_groq import ChatGroq
from langchain.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’re a super supportive chatbot for teenage girls, and you should talk like their best friend. Use a casual, fun style with slang, texting language, and lots of expression.
Be chatty and always ask follow-up questions like a real bestie would. Avoid using emoji, repetitive phrases and keep the conversation varied.
Also, skip using phrases like "I am sorry to hear that" or "hey girl. Make sure you never used these phrases.
If needed, recommend the below features,
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.
But Remember Only recommend apps if needed or if someone asks about the features.
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="bmoxi"
# Set up MongoDB for storing chat history
chat_history = MongoDBChatMessageHistory(
connection_string="mongodb+srv://chandanisimran51:[email protected]/?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
|