Spaces:
Build error
Build error
Download actions/ChatBot.py from bacancydataprophets/bb_chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/spaces/bacancydataprophets/bb_chatbot/resolve/main/actions/ChatBot.py
- Command line
-
hf download hf://spaces/bacancydataprophets/bb_chatbot/actions/ChatBot.py
-
curl -L -o ChatBot.py https://huggingface.co/spaces/bacancydataprophets/bb_chatbot/resolve/main/actions/ChatBot.py
1.81 kB
| import os | |
| from dotenv import load_dotenv | |
| from langchain.memory import ConversationBufferWindowMemory | |
| from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate | |
| from langchain.chains import ConversationChain | |
| from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory | |
| from langchain_openai import ChatOpenAI | |
| from langchain_groq import ChatGroq | |
| load_dotenv() | |
| class ChatBot: | |
| def __init__(self, session_id): | |
| self.session_id = session_id | |
| self.mongo_conn_str = "mongodb+srv://dhara732002:6M2rikdwZxvwMzN0@cluster0.pbzipls.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" | |
| def create_llm_chain(self): | |
| prompt = ChatPromptTemplate.from_messages( | |
| [ | |
| ("system", "You are a helpful assistant.You should give respnse in 1-2 lines without new line."), | |
| MessagesPlaceholder(variable_name="history"), | |
| HumanMessagePromptTemplate.from_template("{input}"), | |
| ] | |
| ) | |
| message_history = MongoDBChatMessageHistory(connection_string=self.mongo_conn_str, session_id=self.session_id) | |
| memory = ConversationBufferWindowMemory(memory_key="history", chat_memory=message_history, return_messages=True, k=3) | |
| conversation_chain = ConversationChain( | |
| llm=ChatGroq(temperature=0, groq_api_key=os.getenv("GROQ_API_KEY"), model_name="llama3-70b-8192"), | |
| prompt=prompt, | |
| verbose=True, | |
| memory=memory, | |
| ) | |
| self.conversation_chain = conversation_chain | |
| return "Chain created successfully" | |
| def get_response(self, question): | |
| ans= self.conversation_chain.predict(input=question) | |
| return ans | |