Skip to content

Commit

Permalink
ADDED: Advance RAG System
Browse files Browse the repository at this point in the history
  • Loading branch information
AquibPy committed May 15, 2024
1 parent 76336d6 commit 3d4e337
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ venv/
.env
.pytest_cache/
.vscode
hit.py
hit.py
try.ipynb
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ percentage, missing keywords, and profile summary.
### 19. Text to Image using Diffusion Models

- **Route:** `/text2image`
description: This route allows you to generate images using various diffusion models available on Hugging Face.
- **Description:** This route allows you to generate images using various diffusion models available on Hugging Face.

You can choose from the following models -

Expand All @@ -167,6 +167,15 @@ description: This route allows you to generate images using various diffusion mo

**Request Body:** `{ "model": "DreamShaper_v7", "prompt": "An astronaut riding a horse on the moon" }` The response will be the generated image in PNG format.

### 20. Advanced RAG with LLaMA Index

- **Route:** `/advance_rag_llama_index`
- **Description:** This API provides an advanced retrieval-augmented generation (RAG) functionality using the LLaMA Index. It allows users to upload a document and ask questions, enabling the API to search for specific facts or summarize the document based on the query.
- **Feature:**
- Upload PDF documents and ask natural language questions.
- Choose between vector search or summarization for responses.
- Support for multiple Open Source LLMs models.

## Usage

Each endpoint accepts specific parameters as described in the respective endpoint documentation. Users can make POST requests to these endpoints with the required parameters to perform the desired tasks.
Expand Down
24 changes: 22 additions & 2 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mongo import MongoDB
from helper_functions import get_qa_chain,get_gemini_response,get_url_doc_qa,extract_transcript_details,\
get_gemini_response_health,get_gemini_pdf,read_sql_query,remove_substrings,questions_generator,groq_pdf,\
summarize_audio,chatbot_send_message,extraxt_pdf_text
summarize_audio,chatbot_send_message,extraxt_pdf_text,advance_rag_llama_index
from langchain_groq import ChatGroq
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
Expand Down Expand Up @@ -529,7 +529,27 @@ async def ats(resume_pdf: UploadFile = File(...), job_description: str = Form(..
return ResponseText(response=response.text)
except Exception as e:
return ResponseText(response=f"Error: {str(e)}")


@app.post("/advance_rag_llama_index",description="The endpoint build a Router that can choose whether to do vector search or summarization\
In model input default is llama3-70b-8192 but you can choose mixtral-8x7b-32768, gemma-7b-it and llama3-8b-8192.")
async def llama_index_rag(pdf: UploadFile = File(...),question: str = Form(...),
model: Optional[str] = Form('llama3-70b-8192')):
try:
rag_output = advance_rag_llama_index(pdf,model,question)
db = MongoDB()
payload = {
"endpoint" : "/advance_rag_llama_index",
"model" : model,
"prompt" : question,
"output" : rag_output
}
mongo_data = {"Document": payload}
result = db.insert_data(mongo_data)
print(result)
return ResponseText(response=rag_output)
except Exception as e:
return ResponseText(response=f"Error: {str(e)}")

@app.post("/text2image",description=
"""
This API provides access to the following diffusion models for generating images from text prompts.
Expand Down
47 changes: 47 additions & 0 deletions helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
from typing import AsyncIterable
import asyncio
from langchain.schema import HumanMessage
from llama_index.llms.groq import Groq
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from llama_index.core import SimpleDirectoryReader,VectorStoreIndex
from llama_index import core
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.query_engine import RouterQueryEngine
import shutil

load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
Expand Down Expand Up @@ -272,6 +279,46 @@ def extraxt_pdf_text(uploaded_file):
text+=str(page.extract_text())
return text

def advance_rag_llama_index(pdf,model,question):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix='.' + pdf.filename.split('.')[-1]) as tmp:
shutil.copyfileobj(pdf.file, tmp)
tmp_path = tmp.name
except Exception as e:
raise Exception(f"Error handling uploaded file: {e}")

finally:
pdf.file.close()
llm = Groq(model=model,api_key=os.getenv("GROQ_API_KEY"))
embed_model = HuggingFaceInferenceAPIEmbeddings(
api_key=os.getenv("HUGGINGFACE_API_KEY"), model_name=settings.INSTRUCTOR_EMBEDDING,query_instruction="Represent the query for retrieval: ")

core.Settings.llm = llm
core.Settings.embed_model = embed_model

docs = SimpleDirectoryReader(input_files=[tmp_path]).load_data()
index = VectorStoreIndex.from_documents(docs)
vector_tool = QueryEngineTool(
index.as_query_engine(),
metadata=ToolMetadata(
name="vector_search",
description="Useful for searching for specific facts."))

summary_tool = QueryEngineTool(
index.as_query_engine(response_mode="tree_summarize"),
metadata=ToolMetadata(
name="summary",
description="Useful for summarizing an entire document."))

query_engine = RouterQueryEngine.from_defaults(
[vector_tool, summary_tool], select_multi=False, verbose=True, llm=llm)

response = query_engine.query(question)
os.remove(tmp_path)

return str(response)


if __name__ == "__main__":
create_vector_db()
chain = get_qa_chain()
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ jinja2
tiktoken
redis
python-jose
sendgrid
sendgrid
llama-index-llms-groq
llama-index-embeddings-langchain

0 comments on commit 3d4e337

Please sign in to comment.