Generative AI Engineer (Part 1) — Interview 5¶
Q: Explain the end-to-end data lifecycle in a production RAG system: data sources, ingestion, chunking, embedding, storage, and retrieval.
- The Knowledge GPT system is designed as an enterprise knowledge search platform leveraging Generative AI and hybrid search (semantic + keyword).
- Here’s a step-by-step overview of the data lifecycle, from source to retrieval:
1. Data Sources & Ingestion - The primary data source is [Company]’s KaaS (Knowledge as a Service), which acts as a central repository for enterprise documents. - Data formats ingested include PDF, HTML, and ZIP files, among others. - The ingestion pipeline queries KaaS APIs to discover new or updated documents, supporting both bulk ingestion (~700K documents initially) and daily incremental updates. - For each document, a secure render link is generated via KaaS APIs, and the document is downloaded and stored in AWS S3.
2. Preprocessing & Chunking
- Downloaded documents are preprocessed to extract clean text content.
- Chunking is performed using the LangChain library’s MarkdownHeaderTextSplitter and custom logic.
- Documents are split based on headings (H1, H2, etc.), paragraphs, and logical sections (FAQs, steps, tables).
- Each chunk is tagged with metadata (source, section, page, document ID) for traceability.
3. Embedding Generation
- Each chunk of text is sent to OpenAI’s text-embedding-ada-002 model via API.
- The model returns a high-dimensional vector representing the semantic content of the chunk.
4. Storage in ElasticSearch/OpenSearch - Embeddings, along with the original chunk text and metadata, are stored in ElasticSearch (recently migrated to OpenSearch for scalability). - The index structure supports both dense vector fields (for semantic search) and text fields (for keyword search). - Metadata fields are indexed for filtering, faceting, and traceability.
5. Retrieval & Search - When a user submits a query, the system performs a hybrid search: - Vector similarity search retrieves semantically relevant chunks. - Keyword search ensures precise matching on important terms. - The most relevant chunks are passed to the LLM (Azure OpenAI) to generate a coherent, context-rich answer. - User feedback is collected via a Feedback API to continuously improve relevance and performance.
Summary Table:
| Step | Tool/Component | Purpose/Details |
|---|---|---|
| Data Discovery | KaaS API | Find new/updated docs, get metadata |
| Download & Storage | AWS S3 | Store raw documents |
| Chunking | LangChain, Custom | Split docs into logical, retrievable chunks |
| Embedding | OpenAI API | Generate semantic vectors for each chunk |
| Indexing | ElasticSearch/OpenSearch | Store vectors, text, and metadata for hybrid search |
| Retrieval | Hybrid Search API | Retrieve relevant chunks for LLM answer generation |
- This architecture ensures scalability, efficient retrieval, traceability, and high-quality, context-aware responses for enterprise knowledge search.
Q: Walk through how to create a FastAPI endpoint that accepts any text and returns a response.
- To create a FastAPI endpoint that accepts any text and returns a response, you need to:
- Set up a FastAPI application.
- Define a POST endpoint that receives a JSON payload with a text field.
- Process the input text (for demo, echo it back or apply any logic).
- Return the response as JSON.
🔑 Key Steps: - Install FastAPI and Uvicorn. - Define a Pydantic model for the request body. - Create the FastAPI app and endpoint. - Run the server with Uvicorn.
💻 Code:
from fastapi import FastAPI # Import FastAPI framework
from pydantic import BaseModel # Import BaseModel for request validation
# Define the request body schema
class TextRequest(BaseModel):
text: str # The input text field
# Initialize the FastAPI app
app = FastAPI() # Create FastAPI instance
# Define the POST endpoint
@app.post("/process-text")
async def process_text(request: TextRequest): # Define endpoint function
input_text = request.text # Extract text from request
# Here you can add any processing logic, for now just echo back
response = {"received_text": input_text} # Prepare response
return response # Return response as JSON
# To run: uvicorn filename:app --reload
💡 Explanation:
- Request Model: TextRequest ensures the endpoint receives a JSON with a text field.
- Endpoint: /process-text accepts POST requests, parses the input, and returns a JSON response.
- Extensibility: You can add any processing logic (e.g., LLM inference, text classification) inside the endpoint.
- Running: Use uvicorn to serve the FastAPI app (uvicorn filename:app --reload).
- Production Use: Add authentication, error handling, and logging as needed for enterprise deployment.
Q: Remove duplicate elements from a list without using any inbuilt functions.
- To remove duplicates from a list without using any inbuilt functions (like set(), dict.fromkeys(), or list comprehensions), you can manually iterate through the list and build a new list by checking if each element is already present.
- This approach demonstrates understanding of basic data structures and control flow.
🔑 Key Steps: - Initialize an empty list to store unique elements. - Iterate through the original list. - For each element, check if it is already in the unique list. - If not, append it to the unique list. - Return or print the unique list.
💻 Code:
# Original list with duplicates
original_list = [1, 2, 2, 3, 4, 4, 5, 1, 6] # List with duplicate elements
# Empty list to store unique elements
unique_list = [] # Initialize empty list
# Iterate through each element in the original list
for item in original_list: # Loop through original list
# Check if the item is not already in unique_list
if item not in unique_list: # If item is not present in unique_list
unique_list.append(item) # Add item to unique_list
# Print the list with duplicates removed
print(unique_list) # Output: [1, 2, 3, 4, 5, 6]
💡 Explanation:
- This code manually checks for duplicates by iterating through the list and only appending unseen elements.
- Time Complexity: O(n^2) in the worst case, since item not in unique_list is O(n) for each of n elements.
- Space Complexity: O(n) for storing the unique elements.
- This approach is simple and does not use any Python inbuilt functions for deduplication, as requested.
Q: Can you create a function to remove duplicates, add a custom message, and handle exceptions using decorators?
- Yes, I can create a function that removes duplicates from a list, adds a custom message, and uses a decorator to handle exceptions and print/log custom error messages.
- This approach demonstrates practical use of Python decorators for exception handling, which is a common pattern in production AI pipelines for robust error management and logging.
🔑 Key Steps: - Define a decorator that wraps any function, catches exceptions, and prints/logs a custom error message. - Implement the deduplication logic inside a function. - Add a custom message to the output. - Apply the decorator to the deduplication function.
💻 Code:
# Define a decorator for exception handling
def exception_handler(func): # Decorator function
def wrapper(*args, **kwargs): # Wrapper function
try:
return func(*args, **kwargs) # Try to execute the function
except Exception as e: # Catch any exception
print(f"Custom Error: {str(e)}") # Print custom error message
return None # Return None on error
return wrapper # Return the wrapper
# Function to remove duplicates and add a custom message
@exception_handler # Apply the decorator
def remove_duplicates_with_message(input_list): # Function definition
unique_list = [] # Initialize empty list for unique elements
for item in input_list: # Iterate through input list
if item not in unique_list: # Check if item is not already in unique_list
unique_list.append(item) # Add item to unique_list
# Add a custom message to the result
return f"Unique elements: {unique_list}" # Return custom message with unique elements
# Example usage
original_list = [1, 2, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] # List with duplicates
result = remove_duplicates_with_message(original_list) # Call the function
print(result) # Print the result
💡 Explanation:
- The exception_handler decorator wraps the deduplication function, catching and reporting any errors with a custom message.
- The deduplication logic is the same as before, ensuring no inbuilt deduplication functions are used.
- The function returns a string with a custom message and the list of unique elements.
- This pattern is widely used in production code for robust error handling and clear user feedback.
- In enterprise AI pipelines, similar decorators are used for logging, monitoring, and exception management (as seen in the provided project documentation with centralized logging modules).
Q: Convert the deduplication logic into a function and add error handling using a decorator.
- I'll encapsulate the deduplication logic in a function and use a decorator for error handling, following best practices for modularity and robustness.
- This approach is similar to how centralized logging and error handling are implemented in production pipelines (as referenced in your project docs with
log_manager.pyand logging best practices).
🔑 Key Steps: - Define an error-handling decorator that catches exceptions and prints/logs a custom error message. - Implement the deduplication logic inside a function. - Apply the decorator to the function.
💻 Code:
# Decorator for error handling
def error_handler(func): # Define the decorator function
def wrapper(*args, **kwargs): # Wrapper to catch errors
try:
return func(*args, **kwargs) # Try to execute the function
except Exception as e: # Catch any exception
print(f"Error occurred: {str(e)}") # Print custom error message
return None # Return None if error occurs
return wrapper # Return the wrapper
# Function to remove duplicates from a list
@error_handler # Apply the error handler decorator
def remove_duplicates(input_list): # Define the deduplication function
unique_list = [] # Initialize empty list for unique elements
for item in input_list: # Iterate through each item in the input list
if item not in unique_list: # If item is not already in unique_list
unique_list.append(item) # Add item to unique_list
return unique_list # Return the list of unique elements
# Example usage
original_list = [1, 2, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] # List with duplicates
result = remove_duplicates(original_list) # Call the function
print(f"Unique elements: {result}") # Print the result
💡 Explanation:
- The error_handler decorator wraps the deduplication function, catching and reporting any errors with a custom message.
- This pattern is widely used in production code for robust error handling and aligns with centralized logging and monitoring practices described in your project documentation.
- The deduplication logic is simple and does not use any inbuilt deduplication functions, as requested.
- This modular approach makes the code easy to maintain and extend, supporting best practices for enterprise AI systems.
Q: Refactor the deduplication code into a function with error handling using a decorator.
- I'll encapsulate your deduplication logic inside a function and add a decorator for error handling, following best practices for modularity and robustness.
- This approach aligns with the centralized logging and error handling patterns used in your enterprise AI projects (as referenced in your documentation with
log_manager.pyfor debugging and monitoring).
🔑 Key Steps:
- Define an error-handling decorator.
- Move the deduplication logic into a function.
- Use a local unique_list inside the function for better encapsulation.
- Apply the decorator to the function.
💻 Code:
# Decorator for error handling
def error_handler(func): # Define the decorator function
def wrapper(*args, **kwargs): # Wrapper to catch errors
try:
return func(*args, **kwargs) # Try to execute the function
except Exception as e: # Catch any exception
print(f"Error occurred: {str(e)}") # Print custom error message
return None # Return None if error occurs
return wrapper # Return the wrapper
# Function to remove duplicates from a list
@error_handler # Apply the error handler decorator
def remove_duplicate(original_list): # Define the deduplication function
unique_list = [] # Initialize empty list for unique elements
for item in original_list: # Iterate through each item in the input list
if item not in unique_list: # If item is not already in unique_list
unique_list.append(item) # Add item to unique_list
print(unique_list) # Print the list of unique elements
# Example usage
original_list = [1, 2, 2, 3, 4, 4, 5, 1, 6] # List with duplicates
remove_duplicate(original_list) # Call the function
💡 Explanation:
- The error_handler decorator wraps the deduplication function, catching and reporting any errors with a custom message.
- The deduplication logic is encapsulated within the function, using a local unique_list for better practice.
- This pattern is consistent with enterprise best practices for error handling and logging, as seen in your project documentation (e.g., log_manager.py for centralized logging).
- The code is modular, easy to maintain, and ready for integration into larger AI pipelines.
Q: Can you handle exceptions gracefully inside the deduplication function?
- Yes, handling exceptions gracefully inside the function is a best practice, especially for production-grade AI pipelines where robustness and clear error reporting are critical.
- In my enterprise AI projects (as referenced in the UIDS documentation), centralized logging and error handling are implemented using modules like
log_manager.pyto ensure all errors are captured and monitored for debugging and operational visibility. - Here’s how I would handle exceptions inside the deduplication function, using Python’s
try-exceptblock and logging the error for traceability:
🔑 Key Steps:
- Import the logging module and configure a logger (as per project best practices).
- Wrap the deduplication logic in a try-except block.
- Log any exceptions with a clear, custom message.
- Return None or an appropriate response if an error occurs.
💻 Code:
import logging # Import logging module
# Configure logger for centralized logging (as per project best practices)
logger = logging.getLogger("deduplication_logger") # Create a logger instance
logger.setLevel(logging.INFO) # Set logging level
def remove_duplicate(original_list): # Define the deduplication function
try:
unique_list = [] # Initialize empty list for unique elements
for item in original_list: # Iterate through each item in the input list
if item not in unique_list: # If item is not already in unique_list
unique_list.append(item) # Add item to unique_list
logger.info(f"Unique elements extracted: {unique_list}") # Log the result
return unique_list # Return the list of unique elements
except Exception as e: # Catch any exception
logger.error(f"Error in remove_duplicate: {str(e)}") # Log the error with a custom message
return None # Return None if error occurs
# Example usage
original_list = [1, 2, 2, 3, 4, 4, 5, 1, 6] # List with duplicates
result = remove_duplicate(original_list) # Call the function
print(result) # Print the result
- This approach ensures that any unexpected errors are logged centrally, making it easier to debug and monitor in production environments.
- It aligns with the modular, extensible, and cloud-native best practices described in your project documentation, supporting robust and maintainable AI pipelines.
Q: Can you write pseudocode for chunking data, generating embeddings, and indexing them for RAG pipelines?
- Absolutely. In RAG (Retrieval-Augmented Generation) pipelines, the standard workflow involves:
- Chunking raw documents into manageable sections.
- Generating embeddings for each chunk using an embedding model (e.g., OpenAI, Azure, etc.).
- Indexing these chunks (with embeddings and metadata) into a vector store (like ElasticSearch, OpenSearch, or ChromaDB) for efficient semantic retrieval.
- This process is core to both the Knowledge-GPT and UIDS-RAG projects, as described in the documentation, ensuring traceability, efficient retrieval, and hybrid search capabilities.
🔑 Key Steps: - Load and preprocess documents. - Chunk documents using logical or structural rules. - Generate embeddings for each chunk. - Prepare metadata for traceability. - Index each chunk (content, embedding, metadata) into the vector store.
💻 Pseudocode:
# 1. Load documents (from local or S3)
documents = load_documents(source_path) # Load raw documents
# 2. Chunk documents
chunks = []
for doc in documents: # Iterate over each document
doc_chunks = chunk_document(doc) # Split document into logical chunks (headings, paragraphs, etc.)
for chunk in doc_chunks: # For each chunk
chunks.append({
"content": chunk, # Store chunk text
"metadata": extract_metadata(doc, chunk) # Extract and attach metadata (doc_id, section, etc.)
})
# 3. Generate embeddings for each chunk
for chunk in chunks: # Iterate over all chunks
chunk["embedding"] = embedding_model.encode(chunk["content"]) # Generate embedding vector using LLM API
# 4. Index chunks into vector store (e.g., ElasticSearch/OpenSearch)
for chunk in chunks: # For each chunk
index_document(
index_name="knowledge-index", # Target index name
content=chunk["content"], # Chunk text
embedding=chunk["embedding"], # Embedding vector
metadata=chunk["metadata"] # Metadata for traceability
)
💡 Explanation:
- Chunking: Uses logical rules (headings, paragraphs, sections) to split documents, as described in Knowledge-GPT docs.
- Embedding: Each chunk is converted to a high-dimensional vector using an embedding model (e.g., OpenAI’s text-embedding-ada-002).
- Metadata: Each chunk is tagged with metadata (document ID, section, etc.) for traceability and filtering.
- Indexing: Chunks, embeddings, and metadata are stored in a vector store (ElasticSearch/OpenSearch/ChromaDB), enabling hybrid search (semantic + keyword).
- This modular pipeline is production-ready and aligns with industry best practices for scalable, efficient RAG systems.
Q: Explain the step-by-step process for chunking, embedding, and indexing a simple list or string for RAG.
- Sure, I’ll walk through the practical steps, referencing how it’s implemented in the Knowledge-GPT pipeline and adapting it for a simple list or string scenario.
- The process is modular and production-ready, supporting both document-scale and granular (list/string) data, as required in enterprise RAG systems.
Step-by-Step Process:
- Load/Prepare Data
- Start with your data source. For this example, let’s use a simple Python list of strings (each string could represent a sentence, phrase, or word).
-
Example:
data = ["reset printer", "connect to wifi", "replace cartridge", ...] -
Chunking
- For a list of short strings, each element can be treated as a chunk.
- For longer text (like a paragraph or document), use a chunking utility (e.g., LangChain’s
MarkdownHeaderTextSplitter) to split by headings, paragraphs, or logical sections. -
Attach metadata to each chunk for traceability (e.g., index, section, source).
-
Embedding Generation
- Pass each chunk (string) to an embedding model (e.g., OpenAI’s
text-embedding-ada-002). -
The model returns a high-dimensional vector for each chunk.
-
Indexing
- Package each chunk with its content, embedding, and metadata.
- Insert into a vector store (e.g., ElasticSearch, OpenSearch, ChromaDB) for semantic retrieval.
Pseudocode Example:
# 1. Prepare your data (list of strings)
data = ["reset printer", "connect to wifi", "replace cartridge", "print test page", "check ink levels"]
# 2. Chunking (for a list, each item is a chunk)
chunks = []
for idx, text in enumerate(data): # Enumerate to attach index as metadata
chunks.append({
"content": text, # The chunk text
"metadata": {"chunk_id": idx} # Metadata for traceability
})
# 3. Generate embeddings for each chunk
for chunk in chunks:
chunk["embedding"] = embedding_model.encode(chunk["content"]) # Call embedding API
# 4. Index each chunk into the vector store
for chunk in chunks:
vector_store.index(
index="knowledge-gpt-index",
body={
"content": chunk["content"],
"embedding": chunk["embedding"],
"metadata": chunk["metadata"]
}
)
- In the actual Knowledge-GPT pipeline, chunking is more advanced (splitting by headings, paragraphs, etc.), and metadata includes document ID, section, page, etc. (see Knowledge-GPT.docx).
- Embeddings are generated using OpenAI’s API, and all data (content, embedding, metadata) is indexed in ElasticSearch/OpenSearch for hybrid search.
- This modular approach ensures scalability, traceability, and efficient retrieval, which are critical for enterprise RAG systems.
Q: What is the difference between building a custom RAG-based solution and using out-of-the-box agent modes or platforms like TCP?
- The main difference lies in flexibility, control, scalability, and alignment with enterprise requirements.
- Out-of-the-box agent platforms (like TCP, Copilot Studio, or low-code tools) are great for rapid prototyping, standard workflows, and scenarios where customization is minimal.
- However, for enterprise-grade solutions—especially in regulated environments like [Company] or Infosys—custom RAG-based architectures offer significant advantages:
Custom RAG-Based Solution: - Full Control & Customization: - You can design the chunking, embedding, retrieval, and orchestration logic to fit specific business needs, data structures, and compliance requirements. - Enables integration with internal knowledge bases, custom metadata, and advanced retrieval strategies (e.g., hybrid search, context-aware ranking). - Scalability & Performance: - Architected for high throughput, low latency, and large-scale document handling (as in Knowledge-GPT and UIDS pipelines). - Can optimize for cost, performance, and security (e.g., using EKS, ALB, custom vector DBs). - Security & Compliance: - Direct control over data flow, storage, and access policies—critical for enterprise and regulated domains. - Easier to implement custom logging, monitoring, and audit trails (as described in MCP and UIDS-RAG-Labelling_Process docs). - Advanced Orchestration: - Ability to build complex, multi-step workflows (e.g., using LangGraph, custom state machines) and integrate with other enterprise systems (via MCP, Apigee, etc.). - Extensibility: - Easily add new features (e.g., hybrid evaluation, ensemble models, custom evaluation metrics) as business needs evolve.
Out-of-the-Box Agent Platforms (e.g., TCP, Copilot Studio): - Rapid Prototyping: - Quick to set up and use for standard use cases. - Minimal coding required; good for demos or simple automation. - Limited Customization: - Restricted to the features and integrations provided by the platform. - Less flexibility for custom data pipelines, retrieval logic, or compliance needs. - Vendor Lock-In: - Dependent on the platform’s roadmap, pricing, and data handling policies. - Security Constraints: - May not meet strict enterprise security or data residency requirements.
Summary: While agent platforms are useful for quick solutions and standard workflows, custom RAG-based solutions are essential for enterprise scenarios requiring deep customization, integration, security, and scalability. This is why, in my experience at [Company] and [Company], we architected and deployed custom RAG pipelines using frameworks like LangChain, OpenAI, and ElasticSearch/OpenSearch, orchestrated via MCP and integrated with enterprise APIs and monitoring systems. This approach ensures our solutions are robust, compliant, and future-proof for evolving business needs.