File size: 11,582 Bytes
263997c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9923cde
263997c
 
 
 
 
 
 
 
 
 
 
 
 
 
9923cde
 
9380b17
263997c
 
 
 
 
 
 
 
 
 
 
 
 
5ec76ac
263997c
 
 
 
 
5ec76ac
263997c
 
 
5ec76ac
263997c
9923cde
5ec76ac
9923cde
 
5ec76ac
 
263997c
5ec76ac
263997c
9923cde
263997c
9923cde
5ec76ac
263997c
 
 
9923cde
5ec76ac
9923cde
 
 
263997c
9923cde
bd88854
5ec76ac
263997c
5ec76ac
 
263997c
 
9923cde
263997c
 
 
 
 
 
 
5ec76ac
263997c
 
 
 
5ec76ac
263997c
5ec76ac
263997c
 
 
 
5ec76ac
 
263997c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# import os  
# import streamlit as st  
# import google.generativeai as genai  
# from langchain_google_genai import GoogleGenerativeAIEmbeddings  
# from langchain_google_genai import ChatGoogleGenerativeAI  
# from langchain_community.document_loaders import PyPDFLoader   
# from langchain.text_splitter import RecursiveCharacterTextSplitter  
# from langchain_community.vectorstores import Chroma  
# from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder  
# from langchain_core.messages import HumanMessage, SystemMessage  
# from langchain.chains import create_history_aware_retriever, create_retrieval_chain 
# from langchain.chains.combine_documents import create_stuff_documents_chain 
# from dotenv import load_dotenv  
# from langchain.embeddings import HuggingFaceEmbeddings

# from sentence_transformers import SentenceTransformer

# import pysqlite3
# import sys
# sys.modules['sqlite3'] = pysqlite3

# import os

# # Retrieve Google API key
# GOOGLE_API_KEY = str(os.getenv('GOOGLE_API_KEY'))
# HF_TOKEN = str(os.getenv("HF_TOKEN"))

# if not GOOGLE_API_KEY:
#     raise ValueError("Gemini API key not found. Please set it in the .env file.")

# os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
# os.environ["HF_TOKEN"] = HF_TOKEN
# # Streamlit app configuration
# st.set_page_config(page_title="English Chatbot", layout="centered")
# st.title("English Tutor Bot")

# # Initialize Google Generative AI LLM 
# llm = ChatGoogleGenerativeAI(
#     model="gemini-1.5-pro-latest",
#     temperature=0.2,
#     max_tokens=500,
#     timeout=None,
#     max_retries=2,
# )

# # Initialize embeddings using HuggingFace
# embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

# def load_preprocessed_vectorstore():
#     try:
#         loader = PyPDFLoader("sound.pdf")
#         documents = loader.load()

#         text_splitter = RecursiveCharacterTextSplitter(
#             separators=["\n\n", "\n", ". ", " ", ""],
#             chunk_size=500, 
#             chunk_overlap=150
#         )

#         document_chunks = text_splitter.split_documents(documents)

#         vector_store = Chroma.from_documents(
#             embedding=embeddings,
#             documents=document_chunks,
#             persist_directory="./data32"
#         )
#         return vector_store
#     except Exception as e:
#         st.error(f"Error creating vector store: {e}")
#         return None

# def get_context_retriever_chain(vector_store):
#     retriever = vector_store.as_retriever()

#     prompt = ChatPromptTemplate.from_messages([
#         MessagesPlaceholder(variable_name="chat_history"),
#         ("human", "{input}"),
#         ("system", """You are an expert english tutor, your task is to help users to learn english. Given the chat history and the latest user question, which might reference context in the chat history, Answer the question
#         by taking reference from the document.
# If the question is directly addressed within the provided document, provide a relevant answer. 
# If the question is not explicitly addressed in the document, return the following message: 
# 'This question is beyond the scope of the available information. Please contact your mentor for further assistance.'
# Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.""")
#     ])

#     retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
#     return retriever_chain

# def get_conversational_chain(retriever_chain):
#     prompt = ChatPromptTemplate.from_messages([
#         ("system", """Hello! I'm your English Tutor, I am here to help you with learning english and can also take quiz to test your skills.
# Note: I will only provide information that is available within our database to ensure accuracy. Let's get started!
# """
#          "\n\n"
#          "{context}"),
#         MessagesPlaceholder(variable_name="chat_history"),
#         ("human", "{input}")
#     ])

#     stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
#     return create_retrieval_chain(retriever_chain, stuff_documents_chain)

# def get_response(user_query):
#     retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
#     conversation_rag_chain = get_conversational_chain(retriever_chain)

#     formatted_chat_history = []
#     for message in st.session_state.chat_history:
#         if isinstance(message, HumanMessage):
#             formatted_chat_history.append({"author": "user", "content": message.content})
#         elif isinstance(message, SystemMessage):
#             formatted_chat_history.append({"author": "assistant", "content": message.content})

#     response = conversation_rag_chain.invoke({
#         "chat_history": formatted_chat_history,
#         "input": user_query
#     })

#     return response['answer']

# # Load the preprocessed vector store from the local directory
# st.session_state.vector_store = load_preprocessed_vectorstore()

# # Initialize chat history if not present
# if "chat_history" not in st.session_state:
#     st.session_state.chat_history = [
#         {"author": "assistant", "content": "Hello, I am a English Tutor Bot. How can I help you?"}
#     ]

# # Main app logic
# if st.session_state.get("vector_store") is None:
#     st.error("Failed to load preprocessed data. Please ensure the data exists in './data' directory.")
# else:
#     # Display chat history
#     with st.container():
#         for message in st.session_state.chat_history:
#             if message["author"] == "assistant":
#                 with st.chat_message("system"):
#                     st.write(message["content"])
#             elif message["author"] == "user":
#                 with st.chat_message("human"):
#                     st.write(message["content"])

#     # Add user input box below the chat
#     with st.container():
#         with st.form(key="chat_form", clear_on_submit=True):
#             user_query = st.text_input("Type your message here...", key="user_input")
#             submit_button = st.form_submit_button("Send")

#         if submit_button and user_query:
#             # Get bot response
#             response = get_response(user_query)
#             st.session_state.chat_history.append({"author": "user", "content": user_query})
#             st.session_state.chat_history.append({"author": "assistant", "content": response})

#             # Rerun the app to refresh the chat display
#             st.rerun()


import os
import logging
import pathlib
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
from langchain.chains import LLMChain, create_retrieval_chain
from langchain_google_genai import GoogleGenerativeAI
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_community.document_loaders import Docx2txtLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)

GOOGLE_API_KEY = "AIzaSyAytkzRS0Xp0pCyo6WqKJ4m1o330bF-gPk"
OPENAI_API_KEY = "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"
TOKEN = '8126949340:AAGmr4ByOLlYXtEQuleOsinS2w_wUogldj0'

# Set up OpenAI API key
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY

# Initialize embeddings using HuggingFace
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

def load_preprocessed_vectorstore():
    """Load documents and create a vector store."""
    try:
        loader = Docx2txtLoader("./Pre.docx")
        documents = loader.load()
        
        text_splitter = RecursiveCharacterTextSplitter(
            separators=["\n\n", "\n", ". ", " ", ""],
            chunk_size=3000,
            chunk_overlap=1000)
        
        document_chunks = text_splitter.split_documents(documents)

        vector_store = Chroma.from_documents(
            embedding=embeddings,
            documents=document_chunks,
            persist_directory="./data"
        )
        return vector_store
    except Exception as e:
        logger.error(f"Error creating vector store: {e}")
        return None

# Initialize the vector store
vector_store = load_preprocessed_vectorstore()

# Define the Langchain chain with a retrieval mechanism
def get_response(user_message, context):
    retriever_chain = vector_store.as_retriever()
    prompt_template = PromptTemplate(input_variables=["chat_history", "input"], template="{chat_history}\nUser: {input}\nAssistant:")
    
    conversation_chain = create_retrieval_chain(retriever_chain, prompt_template)
    
    formatted_chat_history = []
    if 'chat_history' in context.user_data:
        formatted_chat_history.extend(context.user_data['chat_history'])
    
    response = conversation_chain.invoke({
        "chat_history": formatted_chat_history,
        "input": user_message
    })
    
    # Update chat history in user data
    context.user_data['chat_history'] = formatted_chat_history + [{"author": "user", "content": user_message}, {"author": "assistant", "content": response['answer']}]
    
    return response['answer']

# Start the bot
def start(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    user = update.effective_user
    update.message.reply_text(f'Hi {user.first_name}! I\'m a bot powered by OpenAI. Ask me anything.')

# Help command
def help_command(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('Ask me any question, and I\'ll try to answer using my knowledge!')

# Handle messages
def handle_message(update: Update, context: CallbackContext) -> None:
    """Handle user messages and generate responses using Langchain."""
    user_message = update.message.text

    try:
        # Generate a response using Langchain and OpenAI
        response = get_response(user_message, context)
        update.message.reply_text(response)
    except Exception as e:
        update.message.reply_text("Sorry, I couldn't process your request at the moment.")
        logger.error(f"Error: {e}")

# Error handler
def error_handler(update: Update, context: CallbackContext) -> None:
    """Log Errors caused by Updates."""
    logger.warning(f'Update "{update}" caused error "{context.error}"')

def main() -> None:
    """Start the bot."""
    updater = Updater(TOKEN)

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # On different commands - answer in Telegram
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))

    # On non-command i.e. message - handle the message
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))

    # Log all errors
    dispatcher.add_error_handler(error_handler)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT, SIGTERM, or SIGABRT
    updater.idle()

if __name__ == '__main__':
    main()