Prajith04 commited on
Commit
db70da0
·
verified ·
1 Parent(s): 0f3e71e

Upload 10 files

Browse files
Files changed (10) hide show
  1. Dockerfile +33 -0
  2. Readme.md +73 -0
  3. agents.py +11 -0
  4. gradio_demo.py +34 -0
  5. main.py +11 -0
  6. prompts.py +14 -0
  7. query_vectordb.py +29 -0
  8. requirements.txt +124 -0
  9. store2db.py +37 -0
  10. tools.py +13 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Install system dependencies
4
+ RUN apt-get update && apt-get install -y \
5
+ git curl && \
6
+ rm -rf /var/lib/apt/lists/*
7
+
8
+ # Set working directory
9
+ WORKDIR /app
10
+
11
+ # Copy project files
12
+ COPY . /app
13
+
14
+ # Create cache directory
15
+ RUN mkdir -p /app/cache && chmod -R 777 /app/cache
16
+
17
+ # Set environment variables
18
+ ENV TRANSFORMERS_CACHE=/app/cache \
19
+ HF_HOME=/app/cache \
20
+ SENTENCE_TRANSFORMERS_HOME=/app/cache \
21
+ PORT=7860 \
22
+ PYTHONUNBUFFERED=1
23
+
24
+ # Install Python dependencies
25
+ RUN pip install --no-cache-dir --upgrade pip && \
26
+ pip install --no-cache-dir -r requirements.txt && \
27
+ pip install gradio
28
+
29
+ # Expose Gradio port
30
+ EXPOSE 7860
31
+
32
+ # Run the app
33
+ CMD ["python", "gradio_demo.py"]
Readme.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: MultiDoc-RAG-Agent
3
+ emoji: 💻
4
+ colorFrom: blue
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 5.28.0
8
+ app_file: gradio_demo.py
9
+ pinned: false
10
+ ---
11
+ # MultiDoc-RAG-Agent
12
+
13
+ ## Overview
14
+ The MultiDoc-RAG-Agent is a Retrieval-Augmented Generation (RAG) system designed to interact with users, retrieve relevant documents, and provide intelligent responses. It leverages advanced language models, vector databases, and tools to process queries effectively. This system is particularly useful for scenarios requiring document retrieval and contextual understanding, such as customer support, research assistance, and knowledge management.
15
+
16
+ ## Components
17
+
18
+ ### 1. Agents
19
+ - **File**: `agents.py`
20
+ - **Description**: Defines the `rag_agent` function, which creates a tool-calling agent using a language model, tools, and a prompt. The agent is responsible for orchestrating interactions between the user, tools, and the language model to generate accurate and contextually relevant responses.
21
+
22
+ ### 2. Main Application
23
+ - **File**: `main.py`
24
+ - **Description**: Implements a command-line interface for interacting with the RAG agent. It processes user queries, maintains a chat history, and ensures seamless communication between the user and the agent. This serves as the entry point for users to interact with the system.
25
+
26
+ ### 3. Prompts
27
+ - **File**: `prompts.py`
28
+ - **Description**: Contains functions to generate prompts for the agent and retriever using templates and a hub-pulled prompt. These prompts guide the language model in understanding the context and generating appropriate responses.
29
+
30
+ ### 4. Query Vector Database
31
+ - **File**: `query_vectordb.py`
32
+ - **Description**: Handles vector database interactions, initializes chat models, and provides a function to retrieve documents based on similarity. This component ensures efficient and accurate retrieval of relevant documents from the vector database.
33
+
34
+ ### 5. Document Storage
35
+ - **File**: `store2db.py`
36
+ - **Description**: Loads PDF documents, splits them into smaller chunks, and stores them in a Qdrant vector database. This enables the system to handle large documents and retrieve specific sections relevant to user queries.
37
+
38
+ ### 6. Tools
39
+ - **File**: `tools.py`
40
+ - **Description**: Defines tools for the agent, including a retriever tool for Samsung mobile-related queries and a calculator tool. These tools extend the agent's capabilities, allowing it to perform specialized tasks.
41
+
42
+ ## How to Use
43
+
44
+ ### 1. Setup
45
+ - Ensure all dependencies are installed.
46
+ - Configure environment variables in a `.env` file. For example:
47
+ - `GROQ_API_KEY`: API key for the language model.
48
+ - `QDRANT_URL`: URL for the Qdrant vector database.
49
+ - `QDRANT_API_KEY`: API key for the Qdrant vector database.
50
+
51
+ ### 2. Run the Application
52
+ - Execute `main.py` to start the command-line interface.
53
+ - Enter queries to interact with the agent and retrieve intelligent responses.
54
+
55
+ ### 3. Document Storage
56
+ - Use `store2db.py` to load and store documents in the vector database. This step is essential for preparing the system to handle user queries effectively.
57
+
58
+ ## Dependencies
59
+ - **Python**: The primary programming language used for the project.
60
+ - **LangChain**: A framework for building applications with language models.
61
+ - **Qdrant**: A vector database for storing and retrieving document embeddings.
62
+ - **HuggingFace**: A library for natural language processing and machine learning models.
63
+ - **dotenv**: A library for managing environment variables.
64
+
65
+ ## Example Use Case
66
+ 1. A user queries the system about a specific topic related to Samsung mobile devices.
67
+ 2. The agent retrieves relevant documents from the vector database using `query_vectordb.py`.
68
+ 3. The language model processes the retrieved documents and generates a coherent response.
69
+ 4. The user receives an intelligent and contextually accurate answer.
70
+
71
+ ## License
72
+ This project is licensed under the MIT License.
73
+
agents.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.agents import create_tool_calling_agent
2
+ from query_vectordb import chat_model
3
+ from tools import retrieve_tool, calculator_tool
4
+ from prompts import agent_prompt
5
+ def rag_agent():
6
+ llm=chat_model()
7
+ tools = [retrieve_tool(), calculator_tool()]
8
+ prompt=agent_prompt()
9
+ agent = create_tool_calling_agent(llm, tools, prompt)
10
+ return agent
11
+
gradio_demo.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_community.chat_message_histories import ChatMessageHistory
3
+ from langchain.agents import AgentExecutor
4
+ from agents import rag_agent
5
+ from tools import retrieve_tool, calculator_tool
6
+
7
+ chat_history_obj = ChatMessageHistory()
8
+
9
+ agent_executor = AgentExecutor(
10
+ agent=rag_agent(),
11
+ tools=[retrieve_tool(), calculator_tool()],
12
+ verbose=True,
13
+ return_intermediate_steps=True,
14
+ )
15
+
16
+ def chat_interface(user_input,history_list):
17
+ response = agent_executor.invoke({"input": user_input, "chat_history": chat_history_obj.messages})
18
+ chat_history_obj.add_user_message(user_input)
19
+ chat_history_obj.add_ai_message(response['output'])
20
+ print(response)
21
+ if len(response['intermediate_steps']) > 0:
22
+ final_response ="Final Output:\n\n"+response['output']+'\n\nTool Used:'+response['intermediate_steps'][0][0].tool+'\n\nTool output:\n'+response['intermediate_steps'][0][1]
23
+ return final_response
24
+ response = "Final Output:\n\n"+response['output']
25
+ return response
26
+
27
+ iface = gr.ChatInterface(
28
+ fn=chat_interface,
29
+ examples=["how to turn on dark mode in Samsung S25","what is 23*56-67+99*78"],
30
+ cache_examples=False,
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ iface.launch()
main.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.chat_message_histories import ChatMessageHistory
2
+ from langchain.agents import AgentExecutor
3
+ from agents import rag_agent
4
+ from tools import retrieve_tool, calculator_tool
5
+ chat_history = ChatMessageHistory()
6
+ agent_executor = AgentExecutor(agent=rag_agent(),tools=[retrieve_tool(),calculator_tool()], verbose=True)
7
+ while True:
8
+ response=agent_executor.invoke({"input": input("Enter the query:"),"chat_history":chat_history.messages})
9
+ chat_history.add_ai_message(response['input'])
10
+ chat_history.add_ai_message(response['output'])
11
+ print(response)
prompts.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate,MessagesPlaceholder
2
+ from langchain import hub
3
+ def retriever_prompt():
4
+ return ChatPromptTemplate.from_messages([
5
+ SystemMessagePromptTemplate.from_template(
6
+ "Use the context to answer the question:\nContext: {context}"
7
+ "these are the titles of manuals you have:\nManuals: {docs}"
8
+ ),
9
+ HumanMessagePromptTemplate.from_template("{query}"),
10
+ ])
11
+ def agent_prompt():
12
+ prompt = hub.pull("hwchase17/openai-functions-agent")
13
+ return prompt
14
+
query_vectordb.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import init_chat_model
2
+ from dotenv import load_dotenv
3
+ import os
4
+ from langchain_huggingface import HuggingFaceEmbeddings
5
+ from langchain_qdrant import QdrantVectorStore
6
+
7
+ load_dotenv()
8
+ def chat_model():
9
+ groq_api_key = os.getenv('GROQ_API_KEY')
10
+ llm = init_chat_model("mistral-saba-24b", model_provider="groq",api_key=groq_api_key)
11
+ return llm
12
+ def small_chat_model():
13
+ groq_api_key = os.getenv('GROQ_API_KEY')
14
+ llm = init_chat_model("llama-3.3-70b-versatile", model_provider="groq",api_key=groq_api_key)
15
+ return llm
16
+ def init_vector_store():
17
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
18
+ doc_store = QdrantVectorStore.from_existing_collection(
19
+ embedding=embeddings,
20
+ collection_name="multidoc-rag-agent",
21
+ url=os.getenv('QDRANT_URL'),
22
+ api_key=os.getenv('QDRANT_API_KEY'))
23
+ return doc_store
24
+ def retrieve_docs(query, doc_store):
25
+ retriever = doc_store.as_retriever(search_type="similarity", search_kwargs={"k": 3,})
26
+ response=retriever.invoke(query)
27
+ return response
28
+
29
+
requirements.txt ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==24.1.0
2
+ aiohappyeyeballs==2.6.1
3
+ aiohttp==3.11.18
4
+ aiosignal==1.3.2
5
+ annotated-types==0.7.0
6
+ anyio==4.9.0
7
+ attrs==25.3.0
8
+ certifi==2025.4.26
9
+ charset-normalizer==3.4.2
10
+ click==8.1.8
11
+ dataclasses-json==0.6.7
12
+ distro==1.9.0
13
+ dotenv==0.9.9
14
+ fastapi==0.115.12
15
+ ffmpy==0.5.0
16
+ filelock==3.18.0
17
+ frozenlist==1.6.0
18
+ fsspec==2025.3.2
19
+ gradio==5.29.0
20
+ gradio-client==1.10.0
21
+ greenlet==3.2.1
22
+ groovy==0.1.2
23
+ groq==0.24.0
24
+ grpcio==1.71.0
25
+ h11==0.16.0
26
+ h2==4.2.0
27
+ hpack==4.1.0
28
+ httpcore==1.0.9
29
+ httpx==0.28.1
30
+ httpx-sse==0.4.0
31
+ huggingface-hub==0.30.2
32
+ hyperframe==6.1.0
33
+ idna==3.10
34
+ jinja2==3.1.6
35
+ joblib==1.5.0
36
+ jsonpatch==1.33
37
+ jsonpointer==3.0.0
38
+ langchain==0.3.25
39
+ langchain-community==0.3.23
40
+ langchain-core==0.3.58
41
+ langchain-groq==0.3.2
42
+ langchain-huggingface==0.1.2
43
+ langchain-qdrant==0.2.0
44
+ langchain-text-splitters==0.3.8
45
+ langsmith==0.3.42
46
+ markdown-it-py==3.0.0
47
+ markupsafe==3.0.2
48
+ marshmallow==3.26.1
49
+ mdurl==0.1.2
50
+ mpmath==1.3.0
51
+ multidict==6.4.3
52
+ mypy-extensions==1.1.0
53
+ networkx==3.4.2
54
+ numexpr==2.10.2
55
+ numpy==2.2.5
56
+ nvidia-cublas-cu12==12.6.4.1
57
+ nvidia-cuda-cupti-cu12==12.6.80
58
+ nvidia-cuda-nvrtc-cu12==12.6.77
59
+ nvidia-cuda-runtime-cu12==12.6.77
60
+ nvidia-cudnn-cu12==9.5.1.17
61
+ nvidia-cufft-cu12==11.3.0.4
62
+ nvidia-cufile-cu12==1.11.1.6
63
+ nvidia-curand-cu12==10.3.7.77
64
+ nvidia-cusolver-cu12==11.7.1.2
65
+ nvidia-cusparse-cu12==12.5.4.2
66
+ nvidia-cusparselt-cu12==0.6.3
67
+ nvidia-nccl-cu12==2.26.2
68
+ nvidia-nvjitlink-cu12==12.6.85
69
+ nvidia-nvtx-cu12==12.6.77
70
+ orjson==3.10.18
71
+ packaging==24.2
72
+ pandas==2.2.3
73
+ pillow==11.2.1
74
+ portalocker==2.10.1
75
+ propcache==0.3.1
76
+ protobuf==6.30.2
77
+ pydantic==2.11.4
78
+ pydantic-core==2.33.2
79
+ pydantic-settings==2.9.1
80
+ pydub==0.25.1
81
+ pygments==2.19.1
82
+ pypdf==5.4.0
83
+ python-dateutil==2.9.0.post0
84
+ python-dotenv==1.1.0
85
+ python-multipart==0.0.20
86
+ pytz==2025.2
87
+ pyyaml==6.0.2
88
+ qdrant-client==1.14.2
89
+ regex==2024.11.6
90
+ requests==2.32.3
91
+ requests-toolbelt==1.0.0
92
+ rich==14.0.0
93
+ ruff==0.11.8
94
+ safehttpx==0.1.6
95
+ safetensors==0.5.3
96
+ scikit-learn==1.6.1
97
+ scipy==1.15.2
98
+ semantic-version==2.10.0
99
+ sentence-transformers==4.1.0
100
+ setuptools==80.3.1
101
+ shellingham==1.5.4
102
+ six==1.17.0
103
+ sniffio==1.3.1
104
+ sqlalchemy==2.0.40
105
+ starlette==0.46.2
106
+ sympy==1.14.0
107
+ tenacity==9.1.2
108
+ threadpoolctl==3.6.0
109
+ tokenizers==0.21.1
110
+ tomlkit==0.13.2
111
+ torch==2.7.0
112
+ tqdm==4.67.1
113
+ transformers==4.51.3
114
+ triton==3.3.0
115
+ typer==0.15.3
116
+ typing-extensions==4.13.2
117
+ typing-inspect==0.9.0
118
+ typing-inspection==0.4.0
119
+ tzdata==2025.2
120
+ urllib3==2.4.0
121
+ uvicorn==0.34.2
122
+ websockets==15.0.1
123
+ yarl==1.20.0
124
+ zstandard==0.23.0
store2db.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_qdrant import QdrantVectorStore
2
+ from langchain_community.document_loaders import PyPDFLoader
3
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
4
+ from langchain_huggingface import HuggingFaceEmbeddings
5
+ from qdrant_client import QdrantClient
6
+ from qdrant_client.http.models import Distance, VectorParams
7
+ import os
8
+ from dotenv import load_dotenv
9
+ load_dotenv()
10
+ url=os.getenv('QDRANT_URL')
11
+ api_key=os.getenv('QDRANT_API_KEY')
12
+ client=QdrantClient(
13
+ url=url,
14
+ api_key=api_key,
15
+ )
16
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
17
+ loader1 = PyPDFLoader("sam-a16.pdf")
18
+ loader2 = PyPDFLoader("sam-s25.pdf")
19
+ loader3 = PyPDFLoader("sam-fold.pdf")
20
+ docs1 = loader1.load()
21
+ docs2 = loader2.load()
22
+ docs3 = loader3.load()
23
+ docs = docs1 + docs2 + docs3
24
+ text_splitter = RecursiveCharacterTextSplitter(
25
+ chunk_size=1000, # chunk size (characters)
26
+ chunk_overlap=200, # chunk overlap (characters)
27
+ add_start_index=True, # track index in original document
28
+ )
29
+ all_splits = text_splitter.split_documents(docs)
30
+ client.create_collection(
31
+ collection_name="multidoc-rag-agent",
32
+ vectors_config=VectorParams(size=768, distance=Distance.COSINE),
33
+ )
34
+ print(f"Split blog post into {len(all_splits)} sub-documents.")
35
+ vector_store = QdrantVectorStore(client=client, embedding=embeddings, collection_name="multidoc-rag-agent")
36
+ vector_store.add_documents(all_splits)
37
+ print("Documents stored in Qdrant.")
tools.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.tools.retriever import create_retriever_tool
2
+ from query_vectordb import chat_model,init_vector_store,small_chat_model
3
+ from langchain_community.agent_toolkits.load_tools import load_tools
4
+ def retrieve_tool():
5
+ doc_store=init_vector_store()
6
+ retriever = doc_store.as_retriever(search_type="similarity", search_kwargs={"k": 3,})
7
+ retriever_tool = create_retriever_tool(
8
+ retriever,
9
+ "VectorDB_search",
10
+ "Use this tool when you need to answer questions about Samsung mobile phones, including their features, settings, or troubleshooting. For example: how to enable dark mode, battery saving tips, or camera settings.",)
11
+ return retriever_tool
12
+ def calculator_tool():
13
+ return load_tools(["llm-math"],llm=small_chat_model())[0]