Skip to content

Commit f6ef26f

Browse files
pavanjavapavanmantha
andauthored
Advanced rag tech (#97)
* -deep evals base * crewai agents for prompting * -skeleton code for adv RAG techniques like recursive retriever and sub question query * -dummy commit to make the branch upto date * -fully funtional recursive retrieval agents * -fully funtional sub question engine --------- Co-authored-by: pavanmantha <pavan.mantha@thevaslabs.io>
1 parent 3837594 commit f6ef26f

22 files changed

Lines changed: 550 additions & 8 deletions

File tree

bootstraprag/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def create(project_name, framework, template, observability):
3838
'rag-with-controllable-agents',
3939
'rag-with-llama-parse',
4040
'rag-with-adjacent-context',
41+
'rag-with-sub-question-query-engine',
42+
'rag-with-recursive-retriever',
4143
'rag-with-citation',
4244
'agents-with-introspection',
4345
'llama-deploy-with-simplemq',
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Recursive Retrieve Agents
2+
In LlamaIndex, the Recursive Retriever is a specialized component designed to enhance information retrieval by navigating through interconnected nodes within a document or across multiple documents. Unlike traditional retrieval methods that fetch information based solely on direct relevance, the Recursive Retriever delves deeper into the relationships between data points, allowing for a more comprehensive extraction of pertinent information.
3+
4+
### How to run?
5+
`pip install -r requirements.txt`
6+
`python main.py`
7+
8+
### How to expose as API?
9+
`python api_server.py`
10+
- Method: POST
11+
- API: http://localhost:8000/api/v1/chat-completion
12+
- Body:
13+
```json
14+
{
15+
"query": "explain mlops architecture"
16+
}
17+
```

bootstraprag/templates/llamaindex/rag_with_recursive_retriever/recursive_retriever_agents_core.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, agent_names: List[str], data_dir: str = 'data'):
2727
self.document_data = {}
2828
self.agents = {}
2929
self.query_engine = None
30+
self.client: qdrant_client.QdrantClient = None
3031

3132
# Load environment variables
3233
load_dotenv(find_dotenv())
@@ -60,6 +61,7 @@ def _setup_vector_store(self):
6061
url=os.environ['DB_URL'],
6162
api_key=os.environ['DB_API_KEY']
6263
)
64+
self.client = client
6365
self.vector_store = QdrantVectorStore(
6466
client=client,
6567
collection_name=os.environ['COLLECTION_NAME']
@@ -91,11 +93,16 @@ def _create_query_engine_tools(self, agent_name: str, vector_index: VectorStoreI
9193
def _build_agents(self):
9294
"""Build agents with their respective tools."""
9395
for agent_name in self.agent_names:
94-
# Build indices
95-
vector_index = VectorStoreIndex.from_documents(
96-
self.document_data[agent_name],
97-
storage_context=self.storage_context
98-
)
96+
97+
if not self.client.collection_exists(collection_name=os.environ.get("COLLECTION_NAME")):
98+
# Build indices
99+
vector_index = VectorStoreIndex.from_documents(
100+
self.document_data[agent_name],
101+
storage_context=self.storage_context
102+
)
103+
else:
104+
vector_index = VectorStoreIndex.from_vector_store(vector_store=self.vector_store)
105+
99106
summary_index = SummaryIndex.from_documents(
100107
self.document_data[agent_name],
101108
storage_context=self.storage_context
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### Recursive Retrieve Agents
2+
In LlamaIndex, the Recursive Retriever is a specialized component designed to enhance information retrieval by navigating through interconnected nodes within a document or across multiple documents. Unlike traditional retrieval methods that fetch information based solely on direct relevance, the Recursive Retriever delves deeper into the relationships between data points, allowing for a more comprehensive extraction of pertinent information.
3+
4+
### How to run?
5+
`pip install -r requirements.txt`
6+
`python main.py`
7+
8+
### How to expose as API?
9+
`python api_server.py`
10+
- Method: POST
11+
- API: http://localhost:8000/api/v1/chat-completion
12+
- Body:
13+
```json
14+
{
15+
"query": "explain mlops architecture"
16+
}
17+
```
18+
19+
#### How to spin observability
20+
- run `docker compose -f docker-compose-langfuse.yml up`
21+
- launch langfuse in browser `http://localhost:3000`
22+
- click on `signup`
23+
- create `organization` & `project`
24+
- once done create your `public` and `private` api keys

bootstraprag/templates/llamaindex/rag_with_self_correction_with_observability/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- click on `signup`
66
- create `organization` & `project`
77
- once done create your `public` and `private` api keys
8-
-
8+
99
#### Instructions to run the code
1010
- Navigate to the root of the project and run the below command
1111
- `pip install -r requirements.txt`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
DB_URL='http://localhost:6333'
2+
DB_API_KEY='th3s3cr3tk3y'
3+
COLLECTION_NAME='SUB_QUESTION_COLLECTION'
4+
5+
OPENAI_API_KEY='sk-proj-'
6+
OPENAI_EMBED_MODEL='gpt-4o'
7+
8+
# use this incase you are prefering to experiment with local models.
9+
OLLAMA_BASE_URL='http://localhost:11434'
10+
OLLAMA_LLM_MODEL='llama3.1'
11+
OLLAMA_EMBED_MODEL='nomic-embed-text:latest'
12+
13+
CHUNK_SIZE=128
14+
CHUNK_OVERLAP=20
15+
16+
# logger can be controlled usiing env
17+
CRITICAL = 50
18+
FATAL = 50
19+
ERROR = 40
20+
WARNING = 30
21+
WARN = 30
22+
INFO = 20
23+
DEBUG = 10
24+
NOTSET = 0
25+
26+
LIT_SERVER_PORT=8000
27+
LIT_SERVER_WORKERS_PER_DEVICE=4
28+
29+
IS_EVALUATION_NEEDED=true
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from dotenv import load_dotenv, find_dotenv
2+
from sub_question_query_engine import SubQuestionQueryEngineAgent
3+
import litserve as lit
4+
import os
5+
6+
7+
class SubQuestionQueryAPI(lit.LitAPI):
8+
def __init__(self):
9+
load_dotenv(find_dotenv())
10+
self.engine = None
11+
12+
def setup(self, device):
13+
self.engine = SubQuestionQueryEngineAgent()
14+
15+
def decode_request(self, request, **kwargs):
16+
return request['query']
17+
18+
def predict(self, x, **kwargs):
19+
return self.engine.query(x)
20+
21+
def encode_response(self, output, **kwargs):
22+
return {'Agent': output}
23+
24+
25+
if __name__ == "__main__":
26+
lit_api = SubQuestionQueryAPI()
27+
server = lit.LitServer(lit_api=lit_api, api_path='/api/v1/chat-completion',
28+
workers_per_device=int(os.environ.get('LIT_SERVER_WORKERS_PER_DEVICE')))
29+
server.run(port=os.environ.get('LIT_SERVER_PORT'))
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from sub_question_query_engine import SubQuestionQueryEngineAgent
2+
3+
4+
def print_welcome_message():
5+
print("\n=== Orthodontics Query System ===")
6+
print("Type your question and press Enter")
7+
print("Type 'quit' to exit the program")
8+
print("================================\n")
9+
10+
11+
if __name__ == "__main__":
12+
# Initialize the engine
13+
engine = SubQuestionQueryEngineAgent()
14+
15+
# Load and index documents
16+
print("Initializing the system... Please wait...")
17+
engine.load_and_index_documents()
18+
19+
# Display welcome message
20+
print_welcome_message()
21+
22+
while True:
23+
try:
24+
# Get user input
25+
user_question = input("\nEnter your question: ").strip()
26+
27+
# Check for quit command
28+
if user_question.lower() == 'quit':
29+
print("\nThank you for using the system. Goodbye!")
30+
break
31+
32+
# Skip empty questions
33+
if not user_question:
34+
print("Please enter a valid question.")
35+
continue
36+
37+
# Execute query and print response
38+
print("\nProcessing your question...\n")
39+
response = engine.query(user_question)
40+
print("\nResponse:", response)
41+
print("\n" + "-" * 50) # Separator line
42+
43+
except Exception as e:
44+
print(f"\nAn error occurred: {str(e)}")
45+
print("Please try again with a different question.")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Sub Question Query Engine
2+
The Sub-Question Query Engine in LlamaIndex is designed to handle complex queries that require information from multiple data sources. It operates by decomposing a complex query into several sub-questions, each directed to the most relevant data source. After obtaining responses to these sub-questions, it synthesizes them into a comprehensive final answer.
3+
4+
### How to run?
5+
`pip install -r requirements.txt`
6+
`python main.py`
7+
8+
### How to expose as API?
9+
`python api_server.py`
10+
- Method: POST
11+
- API: http://localhost:8000/api/v1/chat-completion
12+
- Body:
13+
```json
14+
{
15+
"query": "Explain vertical plane in orthodontics"
16+
}
17+
```

0 commit comments

Comments
 (0)