Skip to content

Commit 09f9ace

Browse files
committed
Implement persistent embedding cache, add demo output, and LinkedIn-ready documentation for RAG pipeline
1 parent 9516e3d commit 09f9ace

38 files changed

+6038
-73
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example environment variables for K11 Tech Lab RAG Framework
2+
# Copy to .env and fill in your real values. Do not commit .env with secrets!
3+
4+
OPENAI_API_KEY=
5+
HUGGINGFACE_API_KEY=
6+
EMBEDDING_PROVIDER=
7+
EMBEDDING_MODEL=
8+
OLLAMA_URL=http://localhost:11434

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### Environment Variables ###
2+
.env
13
target/
24
allure-results
35
lib

AI_DOCUMENTATION_INDEX.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ New to AI-powered testing? Start here:
88
2. **[🎪 Live Demo Guide](testartifacts/docs/AI_Testing_Assistant/Self_Healing/SELF_HEALING_DEMO_GUIDE.md)** - See self-healing in action
99
3. **[📖 API Reference](AI_API_REFERENCE.md)** - Detailed API documentation
1010

11+
1112
## 📋 Documentation Suite
1213

14+
### 🧠 RAG (Retrieval-Augmented Generation)
15+
- **[RAG Documentation Index](RAG_DOCUMENTATION_INDEX.md)** - All RAG-related guides and architecture
16+
- **[RAG Architecture Guide](testartifacts/docs/AI_Testing_Assistant/RAG_Architecture/RAG_ARCHITECTURE_GUIDE.md)**
17+
- **[RAG Benefits Analysis](testartifacts/docs/AI_Testing_Assistant/RAG_Architecture/RAG_BENEFITS_ANALYSIS.md)**
18+
- **[RAG Architecture Overview](testartifacts/docs/AI_Testing_Assistant/RAG_Architecture/README.md)**
19+
1320
### 🏗️ Architecture & Design
1421
- **[🏠 Framework Architecture](AI_ARCHITECTURE.md)** - System design, components, and data flow
1522
- **[🔧 Configuration Guide](AI_FRAMEWORK_README.md#-configuration)** - Setup and configuration options
@@ -30,7 +37,6 @@ New to AI-powered testing? Start here:
3037

3138
### 🚀 CI/CD & Professional Showcase
3239
- **[🔄 GitHub Actions Integration](testartifacts/docs/GITHUB_ACTIONS_LMSTUDIO_GUIDE.md)** - Run AI tests in CI/CD pipelines
33-
- **[💼 LinkedIn Professional Article](testartifacts/docs/LINKEDIN_AI_SELFHEALING_ARTICLE.md)** - Share your AI testing expertise
3440
- **[🏗️ LM Studio Setup Guide](testartifacts/docs/AI_Testing_Assistant/AI_Providers/LMStudio_Setup_Guide.md)** - Complete LM Studio integration
3541

3642
## 🎪 Interactive Demos
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Implementing Retrieval-Augmented Generation (RAG) in Test Automation: A Practical Guide
2+
3+
## Introduction
4+
Retrieval-Augmented Generation (RAG) is revolutionizing how AI systems provide context-aware, accurate, and domain-specific answers. By combining the strengths of information retrieval and generative AI, RAG enables your test automation framework to deliver smarter, more reliable, and explainable results.
5+
6+
This article walks you through the practical implementation of RAG in the K11 Tech Lab Selenium Java Test Automation Framework, highlighting key architectural choices, integration steps, and real-world benefits.
7+
8+
---
9+
10+
## What is RAG?
11+
RAG (Retrieval-Augmented Generation) enhances traditional AI by retrieving relevant knowledge from curated sources (like documentation, FAQs, or code examples) and injecting it into the prompt for the language model. This approach:
12+
- Reduces hallucinations
13+
- Provides up-to-date, domain-specific answers
14+
- Makes AI outputs more explainable and trustworthy
15+
16+
---
17+
18+
## RAG Architecture in K11 Tech Lab Framework
19+
20+
### 1. Knowledge Base & Indexing
21+
- Markdown docs, FAQs, and guides are indexed recursively from the `testartifacts/docs/` directory.
22+
- Each document is split into semantic chunks and embedded for fast similarity search.
23+
24+
### 2. Embedding Providers
25+
- Supports OpenAI, HuggingFace, and local Ollama for embedding generation.
26+
- Provider and model are selected dynamically via `.env` configuration.
27+
- Local AI (Ollama) enables fully offline, private RAG pipelines.
28+
29+
### 3. Retrieval & Ranking
30+
- User queries are embedded and compared to document vectors using cosine similarity.
31+
- Top-k most relevant chunks are retrieved for each query.
32+
33+
### 4. Answer Synthesis
34+
- Retrieved chunks are deduplicated and merged.
35+
- The answer synthesizer extracts the most relevant sections and provides a fallback with up to 1200 characters for completeness.
36+
37+
### 5. Error Handling
38+
- Handles empty or malformed vectors gracefully.
39+
- Provides clear error messages for embedding API issues.
40+
41+
---
42+
43+
## Implementation Steps
44+
45+
1. **Configure Your Provider**
46+
- Set `EMBEDDING_PROVIDER` and `EMBEDDING_MODEL` in `.env`.
47+
- For local RAG, ensure Ollama is running and the required model is pulled.
48+
49+
2. **Add Knowledge Sources**
50+
- Place markdown files in `testartifacts/docs/`.
51+
- The indexer will automatically include new docs for retrieval.
52+
53+
3. **Run Demos**
54+
- Use `RAGComponentsDemo` for cloud-based RAG.
55+
- Use `RAGLocalOllamaDemo` for fully local RAG.
56+
57+
4. **Extend and Troubleshoot**
58+
- Add new embedding providers by implementing the `EmbeddingFunction` interface.
59+
- Check logs for detailed error messages if issues arise.
60+
61+
---
62+
63+
## Real-World Benefits
64+
- **Multi-provider flexibility:** Choose the best embedding provider for your needs (cost, privacy, performance).
65+
- **Dynamic configuration:** No code changes needed to switch providers or models.
66+
- **Richer, more complete answers:** Improved synthesis logic delivers context-rich responses.
67+
- **Custom knowledge integration:** Easily add and retrieve project-specific docs and FAQs.
68+
- **Robust error handling:** Prevents crashes and provides actionable feedback.
69+
70+
---
71+
72+
## Conclusion
73+
RAG transforms your test automation AI from a generic assistant into a domain expert. With flexible provider support, dynamic configuration, and seamless knowledge integration, your framework is ready for the next generation of intelligent, explainable, and reliable automation.
74+
75+
**Ready to get started?**
76+
- Configure your `.env` and add your docs.
77+
- Run the demo classes.
78+
- Experience the power of RAG in your daily testing workflow!

RAGComponentsDemo_output.txt

74.5 KB
Binary file not shown.

RAG_Demo.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# How to Run the RAG Embedding Cache Demo
2+
3+
1. **Build the project:**
4+
```sh
5+
mvn clean package
6+
```
7+
2. **Run the demo:**
8+
```sh
9+
java -cp target/classes org.k11techlab.framework.ai.rag.demo.RAGComponentsDemo
10+
```
11+
Or, if using Maven:
12+
```sh
13+
mvn exec:java -Dexec.mainClass="org.k11techlab.framework.ai.rag.demo.RAGComponentsDemo"
14+
```
15+
3. **View the output:**
16+
- Output will be printed to the console.
17+
- To save output to a file:
18+
```sh
19+
java -cp target/classes org.k11techlab.framework.ai.rag.demo.RAGComponentsDemo > RAGComponentsDemo_output.txt
20+
```
21+
4. **Check the embedding cache:**
22+
- The file `embedding_cache.json` will be created/updated in your project directory.
23+
- On subsequent runs, you should see `[EMBED] Cache HIT` for all unchanged chunks.
24+
25+
# 🚀 How I Optimized RAG Embedding Efficiency in Java (with Live Demo Output!)
26+
27+
Retrieval-Augmented Generation (RAG) is a game-changer for AI-powered search and Q&A, but embedding computation can be slow and costly if not managed well. In my latest project, I implemented a persistent embedding cache for our Java-based RAG pipeline—ensuring embeddings are only computed once per document chunk and reused on every run.
28+
29+
## What’s the impact?
30+
- ⚡️ Massive speedup on repeated queries
31+
- 💸 Reduced API costs (no redundant embedding calls)
32+
- 🧠 Efficient, production-grade RAG for test automation
33+
34+
## How does it work?
35+
- On first run, embeddings are computed and cached.
36+
- On subsequent runs, the cache is hit for all unchanged files—no recomputation!
37+
- Only new or modified files trigger fresh embedding calls.
38+
39+
## See it in action:
40+
- [RAGComponentsDemo.java (Test Class)](https://github.com/your-org/your-repo/blob/main/src/main/java/org/k11techlab/framework/ai/rag/demo/RAGComponentsDemo.java)
41+
- [Sample Output (Cache Hits!)](https://github.com/your-org/your-repo/blob/main/RAGComponentsDemo_output.txt)
42+
43+
## Sample Output
44+
```text
45+
[EMBED] Cache HIT for chunk: testartifacts/docs/AI_TESTING_ASSISTANT_GUIDE.md
46+
[EMBED] Cache HIT for chunk: testartifacts/docs/AI_Testing_Assistant/RAG_Architecture/RAG_ARCHITECTURE_GUIDE.md
47+
...
48+
Top relevant docs for: How do I handle NoSuchElementException?
49+
- testartifacts/docs/AI_Testing_Assistant/RAG_Architecture/RAG_ARCHITECTURE_GUIDE.md
50+
...
51+
===== Synthesized Answer =====
52+
...
53+
```
54+
55+
## Takeaway
56+
With a simple file-based cache, you can make your RAG pipeline blazing fast and cost-effective. If you’re building AI search or Q&A in Java, check out the code and try it yourself!
57+
58+
#AI #Java #RAG #MachineLearning #TestAutomation #OpenSource

0 commit comments

Comments
 (0)