Skip to content

Commit

Permalink
added advanced_rag
Browse files Browse the repository at this point in the history
  • Loading branch information
luv-singh-ai committed Aug 29, 2024
1 parent a38d6ab commit d548bab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 235 deletions.
24 changes: 0 additions & 24 deletions core/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@
)

from llama_index.core import PromptTemplate

# Templates
QA_TEMPLATE = PromptTemplate(
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given this information, please answer the question: {query_str}\n"
"If you don't know the answer, just say that you don't know. Don't try to make up an answer.\n"
"Provide a detailed response and explain your reasoning step by step."
)

REFINE_TEMPLATE = PromptTemplate(
"The original question is as follows: {query_str}\n"
"We have provided an existing answer: {existing_answer}\n"
"We have the opportunity to refine the existing answer "
"(only if needed) with some more context below.\n"
"------------\n"
"{context_msg}\n"
"------------\n"
"Given the new context, refine the original answer to better "
"answer the question. If the context isn't useful, return the original answer."
)

# llama index imports
# from llama_index.legacy.text_splitter import SentenceSplitter
from llama_index.core import (
Expand Down
2 changes: 1 addition & 1 deletion core/ai_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}

# Initialize settings
Settings.chunk_size = 128
Settings.chunk_size = 512
Settings.llm = OpenAI(
model=os.getenv("MODEL_NAME"),
temperature=0.1,
Expand Down
1 change: 0 additions & 1 deletion data/Haq_data_v4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,6 @@ How can I ensure all relevant schemes show up for an eligible citizen?

English Answer:
If the questionnaire is completed correctly, the relevant schemes will be shown as per the eligibility criteria. To address this issue:

Ensure that the scheme you are looking for is mapped under your organization.
Double-check that all questionnaire answers are accurate and complete.
Verify that you have the latest app version and data update.
Expand Down
232 changes: 23 additions & 209 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import redis
import logging
from core.ai import ragindex
# from core.ai import ragindex
from core.ai_1 import ragindex
from telegram import Update
import os
import dotenv
from functools import lru_cache

from telegram import (
Update,
Expand Down Expand Up @@ -60,6 +61,15 @@ def __new__(cls):
cls.run_once = True
return cls._instance

@lru_cache(maxsize=128)
def get_language_message(lang):
messages = {
"en": "You have chosen English. \nPlease tell your problem",
"hi": "आपने हिंदी चुनी है. \nकृपया मुझे बताएं कि आपको क्या समस्या आ रही है।",
"mr": "तुम्ही मराठीची निवड केली आहे. \कृपया मला तुमची समस्या सांगा"
}
return messages.get(lang, "Language not supported")

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
BotInitializer() # To initialize only once

Expand Down Expand Up @@ -88,36 +98,18 @@ async def language_handler(update: Update, context: CallbackContext):
)

async def preferred_language_callback(update: Update, context: CallbackContext):

callback_query = update.callback_query
languages = {"1": "en", "2": "hi", "3": "mr"}
try:
preferred_language = callback_query.data
lang = languages.get(preferred_language)
context.user_data['lang'] = lang
except (AttributeError, ValueError):
lang = 'en'
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Error getting language! Setting default to English."
)

text_message = ""
if lang == "en":
text_message = "You have chosen English. \nPlease tell your problem"
elif lang == "hi":
text_message = "आपने हिंदी चुनी है. \nकृपया मुझे बताएं कि आपको क्या समस्या आ रही है।"
elif lang == "mr":
text_message = "तुम्ही मराठीची निवड केली आहे. \कृपया मला तुमची समस्या सांगा"

await context.bot.send_message(
chat_id=update.effective_chat.id,
text=text_message
)
lang = languages.get(callback_query.data, 'en')
context.user_data['lang'] = lang

text_message = get_language_message(lang)

await callback_query.answer()
await callback_query.edit_message_text(text=text_message)

set_redis('lang', lang)


async def response_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await query_handler(update, context)

Expand All @@ -126,41 +118,26 @@ async def response_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -


async def query_handler(update: Update, context: CallbackContext):

lang = context.user_data.get('lang')
if not lang:
await language_handler(update, context)
return

if update.message.text:
if update.message and update.message.text:
text = update.message.text
print(f"text is {text}")
# if check_change_language_query(text):
# await language_handler(update, context)
# return
await flow(update, context, text)
# else:
# if update.message.voice:
# voice = await context.bot.get_file(update.message.voice.file_id)
# await flow_voice(update, context, voice)


async def flow(update: Update, context: ContextTypes.DEFAULT_TYPE, text):
response = ""
chat_id = update.effective_chat.id
lang = context.user_data.get('lang')
lang = context.user_data.get('lang', 'en')

if lang == 'en':
response_en = ragindex(chat_id, text)
await context.bot.send_message(chat_id=chat_id, text=response_en)
else:
# response_en = ragindex(chat_id, text)
response, response_en = bhashini_text_chat(chat_id, text, lang)
if response:
await context.bot.send_message(chat_id=chat_id, text=response)
else:
await context.bot.send_message(chat_id=chat_id, text="Sorry, I didn't get that. Reverting to english")
await context.bot.send_message(chat_id=chat_id, text=response_en)
await context.bot.send_message(chat_id=chat_id, text=response or f"Sorry, I didn't get that. Reverting to English:\n\n{response_en}")

if __name__ == '__main__':
application = ApplicationBuilder().token(token).read_timeout(30).write_timeout(30).build()
Expand All @@ -172,167 +149,4 @@ async def flow(update: Update, context: ContextTypes.DEFAULT_TYPE, text):
application.add_handler(language_handler_)
application.add_handler(chosen_language)
application.add_handler(response_handler)
application.run_polling()


'''
"""
Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
"""
import logging
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Sends a message with three inline buttons attached."""
keyboard = [
[
InlineKeyboardButton("Option 1", callback_data="1"),
InlineKeyboardButton("Option 2", callback_data="2"),
],
[InlineKeyboardButton("Option 3", callback_data="3")],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text("Please choose:", reply_markup=reply_markup)
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Parses the CallbackQuery and updates the message text."""
query = update.callback_query
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
await query.answer()
await query.edit_message_text(text=f"Selected option: {query.data}")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Displays info on how to use the bot."""
await update.message.reply_text("Use /start to test this bot.")
def main() -> None:
"""Run the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token("TOKEN").build()
application.add_handler(CommandHandler("start", start))
application.add_handler(CallbackQueryHandler(button))
application.add_handler(CommandHandler("help", help_command))
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()
'''

# async def chat_handler(update: Update, context: ContextTypes.DEFAULT_TYPE, text: str):
# response = ""
# chat_id = update.effective_chat.id
# lang = context.user_data.get('lang')
# wait_message = get_random_wait_messages(
# not_always=True,
# lang=lang
# )
# if wait_message:
# await context.bot.send_message(chat_id=chat_id, text=wait_message)
# if lang == 'en':
# response_en, history = chat(chat_id, text)
# else:
# response, response_en, history = bhashini_text_chat(chat_id,text, lang)
# if response:
# await context.bot.send_message(chat_id=chat_id, text=response)
# await context.bot.send_message(chat_id=chat_id, text=response_en)

# async def talk_handler(update: Update, context: ContextTypes.DEFAULT_TYPE, voice):
# lang = context.user_data.get('lang')
# # getting audio file
# audio_file = voice
# # audio_file = await context.bot.get_file(update.message.voice.file_id)

# if lang == 'en':
# with tempfile.NamedTemporaryFile(suffix='.wav', delete=True) as temp_audio_file:
# await audio_file.download_to_drive(custom_path=temp_audio_file.name)
# chat_id = update.effective_chat.id

# wait_message = get_random_wait_messages(
# not_always=True,
# lang=lang
# )
# if wait_message:
# await context.bot.send_message(chat_id=chat_id, text=wait_message)

# with open(temp_audio_file.name, "rb") as file:
# audio_data = file.read()
# audio_base64 = base64.b64encode(audio_data).decode('utf-8')

# response_audio, assistant_message, history = audio_chat(
# chat_id, audio_file=open(temp_audio_file.name, "rb")
# )
# response_audio.stream_to_file(temp_audio_file.name)
# # fix this error "raise JSONDecodeError("Expecting value", s, err.value) from None" here
# # duration = get_duration_pydub(temp_audio_file.name)
# await context.bot.send_audio(
# chat_id=chat_id,
# audio=open(temp_audio_file.name, "rb"),
# #duration=duration,
# filename="response.wav",
# performer="Yojana Didi",
# )
# await context.bot.send_message(
# chat_id=chat_id, text=assistant_message
# )
# file.close()
# else:
# with tempfile.NamedTemporaryFile(suffix='.mp3', delete=True) as temp_audio_file: # suffix='.wav'
# await audio_file.download_to_drive(custom_path=temp_audio_file.name)
# chat_id = update.effective_chat.id

# wait_message = get_random_wait_messages(
# not_always=True,
# lang=lang
# )
# if wait_message:
# await context.bot.send_message(chat_id=chat_id, text=wait_message)

# with open(temp_audio_file.name, "rb") as file:
# audio_data = file.read()
# audio_base64 = base64.b64encode(audio_data).decode('utf-8')
# response_audio, response, history = bhashini_audio_chat(
# chat_id,
# audio_file=audio_base64,
# lang=lang
# )
# file_ = open(temp_audio_file.name, "wb")
# file_.write(response_audio.content)
# file_.close()
# with open(temp_audio_file.name, "rb") as file:
# duration = get_duration_pydub(temp_audio_file.name)
# await context.bot.send_audio(
# chat_id=chat_id,
# audio=open(temp_audio_file.name, "rb"),
# duration=duration,
# filename="response.mp3",
# performer="Yojana Didi",
# )
# await context.bot.send_message(
# chat_id=chat_id, text=response
# )
# file_.close()
application.run_polling()
Empty file removed temp.py
Empty file.

0 comments on commit d548bab

Please sign in to comment.