Skip to content

Commit

Permalink
removed cache
Browse files Browse the repository at this point in the history
  • Loading branch information
luv-singh-ai committed Sep 21, 2024
1 parent 25be2ea commit f3ff214
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 240 deletions.
Binary file modified .DS_Store
Binary file not shown.
243 changes: 6 additions & 237 deletions core/ai_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from utils.bhashini_utils import bhashini_translate
from dotenv import load_dotenv
# from llama_index.legacy.query_engine import FLAREInstructQueryEngine

Expand All @@ -37,237 +36,10 @@
# from llama_index.core.prompts import LangchainPromptTemplate
from llama_index.core import PromptTemplate

# TO ARTIFICALLY GENERATE Q&A
from llama_index.core.evaluation import generate_question_context_pairs
# Prompt to generate questions
qa_generate_prompt_tmpl = """\
Context information is below.
---------------------
{context_str}
---------------------
Given the context information and not prior knowledge.
generate only questions based on the below query.
You are a Professor. Your task is to setup \
{num_questions_per_chunk} questions for an upcoming \
quiz/examination. The questions should be diverse in nature \
across the document. The questions should not contain options, not start with Q1/ Q2. \
Restrict the questions to the context information provided.\
"""

class CustomQueryEngine(RetrieverQueryEngine):
def __init__(self, custom_str, refine_str):
self.custom_str = custom_str
self.refine_str = refine_str

def custom_query(self, query_str: str):
# Retrieve nodes relevant to the query
nodes = self.retriever.retrieve(query_str)

qa_dataset = generate_question_context_pairs(
nodes, llm=llm, num_questions_per_chunk=2, qa_generate_prompt_tmpl=qa_generate_prompt_tmpl
)
# The returned result is a EmbeddingQAFinetuneDataset object (containing queries, relevant_docs, and corpus).

# Generate the context string
context_str = "\n\n".join([n.node.get_context() for n in nodes])

# print or log the context string
print("Context string:", context_str)

# Call the superclass's query method to generate a response
return super().query(query_str)

# 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."
)

# Load environment variables
load_dotenv(dotenv_path="ops/.env")
# llm = OpenAI()
# Constants
PERSIST_DIR = "./storage"
DATA_FILE = 'data/Haq_data_v4.txt'
PORTKEY_HEADERS = {
"x-portkey-api-key": os.getenv("PORTKEY_API_KEY"),
"x-portkey-provider": "openai",
"Content-Type": "application/json"
}
# Initialize settings
Settings.chunk_size = 512
Settings.llm = OpenAI(
model=os.getenv("MODEL_NAME"),
temperature=0.1,
api_base=os.getenv("PORTKEY_GATEWAY_URL"),
default_headers=PORTKEY_HEADERS
)
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

# openai_api_key = os.getenv("OPENAI_API_KEY")
# port_api_key = os.getenv("PORTKEY_API_KEY")
# model = os.getenv("MODEL_NAME")

@lru_cache(maxsize=1)
def get_or_create_index():
if os.path.exists(PERSIST_DIR):
return load_index_from_storage(StorageContext.from_defaults(persist_dir=PERSIST_DIR))

documents = SimpleDirectoryReader(input_files=[DATA_FILE]).load_data()
document = Document(text="\n\n".join(doc.text for doc in documents))
index = VectorStoreIndex.from_documents([document])
index.storage_context.persist(persist_dir=PERSIST_DIR)
return index

SIMILARITY_CUTOFF = 0.7
TOP_K = 3

# RETREIVER PART
from llama_index.core.evaluation import RetrieverEvaluator

def create_custom_query_engine(index: VectorStoreIndex) -> RetrieverQueryEngine:
"""Create a custom query engine with advanced retrieval and postprocessing."""
retriever = index.as_retriever(similarity_top_k=TOP_K)
retriever_evaluator = RetrieverEvaluator.from_metric_names(
["mrr", "hit_rate"],
retriever=retriever)

# retriever_evaluator.evaluate(
# query="query", expected_ids=["node_id1", "node_id2"]
# )
# eval_results = await retriever_evaluator.aevaluate_dataset(qa_dataset)

return RetrieverQueryEngine.from_args(
retriever,
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=SIMILARITY_CUTOFF)],
text_qa_template=QA_TEMPLATE,
refine_template=REFINE_TEMPLATE,
)

query_engine = RetrieverQueryEngine.from_args(
retriever,
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=SIMILARITY_CUTOFF)],
text_qa_template=lc_prompt_tmpl,
)
return query_engine

def semantic_cache(func):
"""Simple semantic caching decorator."""
cache = {}

def wrapper(*args, **kwargs):
query = args[0] if args else kwargs.get('input_message', '')
for cached_query, cached_response in cache.items():
if semantic_similarity(query, cached_query) > 0.9: # Adjust threshold as needed
logger.info("Using cached response for similar query.")
return cached_response
result = func(*args, **kwargs)
cache[query] = result
return result

return wrapper

def semantic_similarity(query1: str, query2: str) -> float:
"""Compute semantic similarity between two queries."""
# Implement semantic similarity calculation here
# This is a placeholder and should be replaced with actual implementation
return 0.5 # Placeholder value


# @lru_cache(maxsize=100)
@semantic_cache
def llama_index_rag(input_message):
# query_engine = get_or_create_index().as_query_engine(similarity_top_k=2)
query_engine = create_custom_query_engine(get_or_create_index())
# debug_handler = LlamaDebugHandler(print_trace_on_end=True)
# callback_manager = CallbackManager([debug_handler])
try:
response = query_engine.query(input_message)
logger.info(f"Query: {input_message}")
logger.info(f"Response: {response}")
return str(response)
except Exception as e:
logger.error(f"Error during query processing: {e}")
return "An error occurred while processing your query. Please try again."

# response = query_engine.query(input_message)
# return str(response)

# for streaming
# stream = llm.stream_complete(input_message)
# for r in stream:
# print(r.delta, end="", flush = True)

def ragindex(chat_id: str, input_message: str) -> str:
"""Wrapper function to call llama_index_rag."""
res = llama_index_rag(input_message)
logger.info(f"Chat ID: {chat_id}, Query: {input_message}")
logger.info(f"Response type: {type(res)}, Response: {res}")
return res


def bhashini_text_chat(chat_id, text, lang):
input_message = bhashini_translate(text, lang, "en")
response_en = ragindex(chat_id, input_message)
response = bhashini_translate(response_en, "en", lang)
return response, response_enfrom openai import OpenAI
import os
import json
from functools import lru_cache
# portkey
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders, Portkey
# llama index imports
from llama_index.core import (
SimpleDirectoryReader, StorageContext, load_index_from_storage, VectorStoreIndex,
Document, Settings, PromptTemplate
)
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from utils.bhashini_utils import bhashini_translate
from dotenv import load_dotenv
# from llama_index.legacy.query_engine import FLAREInstructQueryEngine

# pip install -U sentence-transformers
# from sentence_transformers import SentenceTransformer

from utils.bhashini_utils import bhashini_translate # bhashini_asr,# bhashini_tts)

import logging
from typing import Optional
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core.postprocessor import SimilarityPostprocessor
# from llama_index.callbacks import CallbackManager, LlamaDebugHandler

# from llama_index.response.pprint_utils import pprint_response
# pprint_response(response, show_source=True)

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

from langchain import hub
# from llama_index.core.prompts import LangchainPromptTemplate
from llama_index.core import PromptTemplate
api_key = os.environ("OPENAI_API_KEY")
llm = OpenAI(api_key=api_key)

# TO ARTIFICALLY GENERATE Q&A
from llama_index.core.evaluation import generate_question_context_pairs
Expand All @@ -290,9 +62,9 @@ def bhashini_text_chat(chat_id, text, lang):
"""

class CustomQueryEngine(RetrieverQueryEngine):
def __init__(self, custom_str, refine_str):
self.custom_str = custom_str
self.refine_str = refine_str
# def __init__(self, custom_str, refine_str):
# self.custom_str = custom_str
# self.refine_str = refine_str

def custom_query(self, query_str: str):
# Retrieve nodes relevant to the query
Expand Down Expand Up @@ -335,9 +107,6 @@ def custom_query(self, query_str: str):
"answer the question. If the context isn't useful, return the original answer."
)

# Load environment variables
load_dotenv(dotenv_path="ops/.env")
# llm = OpenAI()
# Constants
PERSIST_DIR = "./storage"
DATA_FILE = 'data/Haq_data_v4.txt'
Expand Down Expand Up @@ -462,4 +231,4 @@ def bhashini_text_chat(chat_id, text, lang):
input_message = bhashini_translate(text, lang, "en")
response_en = ragindex(chat_id, input_message)
response = bhashini_translate(response_en, "en", lang)
return response, response_en
return response, response_en
Binary file added cs alias
Binary file not shown.
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
# from core.ai import ragindex
from core.ai_1 import ragindex
# from core.ai_2 import ragindex
from telegram import Update
import os
import dotenv
Expand Down
1 change: 1 addition & 0 deletions screening-q_optimisations
Submodule screening-q_optimisations added at 143574
2 changes: 1 addition & 1 deletion storage/default__vector_store.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion storage/docstore.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion storage/index_store.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"index_store/data": {"10542696-3b13-4936-aeed-e0478f4f4606": {"__type__": "vector_store", "__data__": "{\"index_id\": \"10542696-3b13-4936-aeed-e0478f4f4606\", \"summary\": null, \"nodes_dict\": {\"cb7d35e2-7643-4351-a5c9-f73e106da74b\": \"cb7d35e2-7643-4351-a5c9-f73e106da74b\", \"a81795a9-669b-470c-8f84-ba33adf7596d\": \"a81795a9-669b-470c-8f84-ba33adf7596d\", \"3f418996-ec74-436a-bacf-f32d9af850aa\": \"3f418996-ec74-436a-bacf-f32d9af850aa\", \"c742b29b-bab3-4abc-805d-f470b49e46e3\": \"c742b29b-bab3-4abc-805d-f470b49e46e3\", \"3971ba22-57eb-45e7-9d42-5e47a94a33fa\": \"3971ba22-57eb-45e7-9d42-5e47a94a33fa\", \"138b76dd-e345-4d31-a006-e48048a69469\": \"138b76dd-e345-4d31-a006-e48048a69469\", \"fd292866-84f1-402a-9736-1d48ba26f405\": \"fd292866-84f1-402a-9736-1d48ba26f405\", \"00e8827b-1fde-4314-9937-17b05d3dcb5a\": \"00e8827b-1fde-4314-9937-17b05d3dcb5a\"}, \"doc_id_dict\": {}, \"embeddings_dict\": {}}"}}}
{"index_store/data": {"eb3f4354-46ea-403b-b0df-7cf7482799d9": {"__type__": "vector_store", "__data__": "{\"index_id\": \"eb3f4354-46ea-403b-b0df-7cf7482799d9\", \"summary\": null, \"nodes_dict\": {\"d6b5ab1d-0961-4e99-a108-30d70f8abef2\": \"d6b5ab1d-0961-4e99-a108-30d70f8abef2\", \"c09194cd-6361-42ac-b9e7-ba087c74d7e2\": \"c09194cd-6361-42ac-b9e7-ba087c74d7e2\", \"5532cc4a-e055-410c-a373-49430e34d5d1\": \"5532cc4a-e055-410c-a373-49430e34d5d1\", \"1a3a3b7c-ee15-4d44-bae3-50a77f944c8d\": \"1a3a3b7c-ee15-4d44-bae3-50a77f944c8d\", \"f192b2db-6de7-4806-9c6c-8e1c3abc1d93\": \"f192b2db-6de7-4806-9c6c-8e1c3abc1d93\", \"56da22db-41b3-4aaf-808d-821deae2e5e4\": \"56da22db-41b3-4aaf-808d-821deae2e5e4\", \"05751854-84bb-4efb-aac7-1b9c0963712a\": \"05751854-84bb-4efb-aac7-1b9c0963712a\", \"c6a53284-e08f-4c91-9247-67afe3d6a669\": \"c6a53284-e08f-4c91-9247-67afe3d6a669\", \"3549011f-8621-461a-8d53-00df3e0ba5cb\": \"3549011f-8621-461a-8d53-00df3e0ba5cb\", \"9c6fc2b6-6251-4321-bc19-11724a6c4ce2\": \"9c6fc2b6-6251-4321-bc19-11724a6c4ce2\", \"788851b8-e719-4796-86ba-109ea1e03798\": \"788851b8-e719-4796-86ba-109ea1e03798\", \"df752e78-023d-4043-b0f9-a822ac6ebb6c\": \"df752e78-023d-4043-b0f9-a822ac6ebb6c\", \"7fb24649-a9ee-4879-95a5-576f1218764b\": \"7fb24649-a9ee-4879-95a5-576f1218764b\", \"2fcc3328-92c5-4a67-8bf3-077a0f0e0360\": \"2fcc3328-92c5-4a67-8bf3-077a0f0e0360\", \"e7923477-3d12-4cc7-9ec6-8074aa07a454\": \"e7923477-3d12-4cc7-9ec6-8074aa07a454\", \"4dc49f3e-c065-4ed5-9dff-b0a9a9f2f521\": \"4dc49f3e-c065-4ed5-9dff-b0a9a9f2f521\", \"d4e8af6d-31e3-4394-a497-198261b0be54\": \"d4e8af6d-31e3-4394-a497-198261b0be54\", \"9d3ab415-92f1-4d4c-ada2-3df199495f44\": \"9d3ab415-92f1-4d4c-ada2-3df199495f44\", \"e91808ff-f72e-4cd2-a10e-7b7027c3117e\": \"e91808ff-f72e-4cd2-a10e-7b7027c3117e\", \"88b792b9-c388-4e3b-a521-9213b14898ff\": \"88b792b9-c388-4e3b-a521-9213b14898ff\", \"79b10ad5-6204-4319-b7ff-1168bb0268ba\": \"79b10ad5-6204-4319-b7ff-1168bb0268ba\", \"2780b88e-9078-43df-a63d-9a8c6be609ba\": \"2780b88e-9078-43df-a63d-9a8c6be609ba\", \"889775ae-5120-4b1c-8b55-929e79c14bb8\": \"889775ae-5120-4b1c-8b55-929e79c14bb8\", \"6c38ece5-f833-48a8-a596-699a8eaebd4d\": \"6c38ece5-f833-48a8-a596-699a8eaebd4d\", \"160f5173-ce59-4354-bb1b-2ef61fda44bd\": \"160f5173-ce59-4354-bb1b-2ef61fda44bd\", \"b672476d-0dd7-453f-9a5b-8b198624b7e4\": \"b672476d-0dd7-453f-9a5b-8b198624b7e4\", \"46074827-aca5-4bc0-8450-1cc7ae4b8581\": \"46074827-aca5-4bc0-8450-1cc7ae4b8581\", \"da081514-32e3-43c9-a641-fc4961aeb8f1\": \"da081514-32e3-43c9-a641-fc4961aeb8f1\", \"29bf10fc-248f-4095-9e35-4f679dfebc96\": \"29bf10fc-248f-4095-9e35-4f679dfebc96\", \"d94e2a2a-53c5-4efa-946c-a6cdfb2b28aa\": \"d94e2a2a-53c5-4efa-946c-a6cdfb2b28aa\", \"510591bf-3ea8-4a9c-ae4b-80994bc8bc9f\": \"510591bf-3ea8-4a9c-ae4b-80994bc8bc9f\", \"026deff9-e172-4d98-82cc-712515a0d142\": \"026deff9-e172-4d98-82cc-712515a0d142\", \"41865b0a-f466-4af8-bb63-d65448869e93\": \"41865b0a-f466-4af8-bb63-d65448869e93\", \"8eee4a42-8c54-47d8-b806-f666e7dff495\": \"8eee4a42-8c54-47d8-b806-f666e7dff495\", \"a1d9a7e2-807c-4234-87aa-fb04d212b1d6\": \"a1d9a7e2-807c-4234-87aa-fb04d212b1d6\", \"c002f794-a118-4596-8fc5-1a583fc1cd8e\": \"c002f794-a118-4596-8fc5-1a583fc1cd8e\", \"35e2081d-5288-4b59-8e8d-3e956a47ae9c\": \"35e2081d-5288-4b59-8e8d-3e956a47ae9c\", \"ddf2f48d-33dc-4125-bbfa-d246c1cf5f9c\": \"ddf2f48d-33dc-4125-bbfa-d246c1cf5f9c\", \"d601700a-368f-4017-b334-d46d10325bbb\": \"d601700a-368f-4017-b334-d46d10325bbb\", \"fbae5f18-728a-4c9e-a69a-4dcc99522076\": \"fbae5f18-728a-4c9e-a69a-4dcc99522076\", \"5dcf1ce1-4668-401c-8c9a-6fac43a7d6a5\": \"5dcf1ce1-4668-401c-8c9a-6fac43a7d6a5\", \"bb824fc5-0534-4c58-a06a-ed71019e476d\": \"bb824fc5-0534-4c58-a06a-ed71019e476d\", \"b85898f8-4f69-4654-ae10-15ef39064e9c\": \"b85898f8-4f69-4654-ae10-15ef39064e9c\", \"71393b4c-c46e-4772-ae25-4900737e9f88\": \"71393b4c-c46e-4772-ae25-4900737e9f88\", \"87f2210c-e344-4a79-8168-3e0b8a94f5c3\": \"87f2210c-e344-4a79-8168-3e0b8a94f5c3\", \"13f2a3a8-2c0d-4b42-a9c6-a5719324f406\": \"13f2a3a8-2c0d-4b42-a9c6-a5719324f406\", \"b2d85a76-08fc-45aa-94c2-177c8996dcc1\": \"b2d85a76-08fc-45aa-94c2-177c8996dcc1\", \"ac709021-f911-4b35-be05-f3b3411f7d9c\": \"ac709021-f911-4b35-be05-f3b3411f7d9c\", \"096927c1-1464-4bbe-8d55-39811b68b384\": \"096927c1-1464-4bbe-8d55-39811b68b384\", \"c3d6c75e-a3ef-443a-b724-199be3dc9897\": \"c3d6c75e-a3ef-443a-b724-199be3dc9897\", \"f082491a-5cfc-49c0-9c75-1b8f8cbe283f\": \"f082491a-5cfc-49c0-9c75-1b8f8cbe283f\", \"d7bffc3e-0af8-49ec-a00c-75692643ffa1\": \"d7bffc3e-0af8-49ec-a00c-75692643ffa1\"}, \"doc_id_dict\": {}, \"embeddings_dict\": {}}"}}}

0 comments on commit f3ff214

Please sign in to comment.