Skip to content

Commit 6866f1e

Browse files
committed
adding dotenv support
1 parent ac05850 commit 6866f1e

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

supporting-blog-content/local-rag-with-lightweight-elasticsearch/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Local RAG with Elasticsearch & Local AI
1+
# Build a Local lightweight RAG System with Elasticsearch
22

3-
Simple RAG (Retrieval-Augmented Generation) system using Elasticsearch for semantic search and Local AI as model provider.
3+
Simple RAG (Retrieval-Augmented Generation) system using Elasticsearch for semantic search and Local AI as model provider. This application serves as supporting content for the blog post [Build a Local lightweight RAG System with Elasticsearch](https://www.elastic.co/search-labs/blog/local-rag-with-lightweight-elasticsearch)
44

55
## Prerequisites
66

@@ -22,7 +22,24 @@ source venv/bin/activate
2222
pip install -r requirements.txt
2323
```
2424

25-
### 3. Run the Script
25+
### 3. Configure Environment Variables
26+
27+
Create an `.env` and put there your settings:
28+
29+
```yaml
30+
# Elasticsearch Configuration
31+
ES_URL=http://localhost:9200
32+
ES_API_KEY="your_elasticsearch_api_key_here"
33+
INDEX_NAME=team-data
34+
35+
# Local AI Configuration
36+
LOCAL_AI_URL=http://localhost:8080/v1
37+
38+
# Dataset Configuration
39+
DATASET_FOLDER=./Dataset
40+
```
41+
42+
### 4. Run the Script
2643

2744
```bash
2845
python script.py

supporting-blog-content/local-rag-with-lightweight-elasticsearch/script.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import os
22
import time
33

4+
from dotenv import load_dotenv
45
from elasticsearch import Elasticsearch, helpers
56
from openai import OpenAI
67

7-
ES_URL = "http://localhost:9200"
8-
ES_API_KEY = "NDdDQWM1b0JPSDBFTV9JQzA0WVo6eHFXcWFJQmFYNzBwS1RjUllpRUNHZw=="
9-
INDEX_NAME = "team-data"
10-
LOCAL_AI_URL = "http://localhost:8080/v1" # Local AI server URL
11-
DATASET_FOLDER = "./Dataset"
8+
load_dotenv()
9+
10+
ES_URL = os.getenv("ES_URL", "http://localhost:9200")
11+
ES_API_KEY = os.getenv("ES_API_KEY")
12+
INDEX_NAME = os.getenv("INDEX_NAME", "team-data")
13+
LOCAL_AI_URL = os.getenv("LOCAL_AI_URL", "http://localhost:8080/v1")
14+
DATASET_FOLDER = os.getenv("DATASET_FOLDER", "./Dataset")
1215

1316

1417
es_client = Elasticsearch(ES_URL, api_key=ES_API_KEY)
@@ -177,23 +180,34 @@ def query_local_ai(prompt, model):
177180
print(f"🔍 Search: '{query}'")
178181
search_results, search_latency = semantic_search(query)
179182

180-
context = "Information found:\n"
181-
for hit in search_results:
183+
context = ""
184+
citations = []
185+
for idx, hit in enumerate(search_results, 1):
182186
source = hit["_source"]
183-
context += f"File: {source['file_title']}\n"
187+
context += f"[{idx}] File: {source['file_title']}\n"
184188
context += f"Content: {source['file_content']}\n\n"
189+
citations.append(f"[{idx}] {source['file_title']}")
190+
191+
prompt = f"""Based on the following documents, answer the user's question.
192+
You MUST cite your sources using the format [1], [2], etc. when referencing information from the documents.
193+
194+
Documents:
195+
{context}
196+
197+
User Question: {query}
185198
186-
prompt = f"{context}\nQuestion: {query}\nAnswer:"
199+
Answer (remember to include citations [1], [2], etc. when referencing specific information)
200+
"""
187201

188-
# ai_model = "llama-smoltalk-3.2-1b-instruct"
189-
# ai_model = "dolphin3.0-qwen2.5-0.5b"
190-
# ai_model = "fastllama-3.2-1b-instruct"
191202
ai_model = "smollm2-1.7b-instruct"
192203

193204
print(f"🤖 Asking to model: {ai_model}")
194205
response, ai_latency, tokens_per_second = query_local_ai(prompt, ai_model)
195206

196207
print(f"\n💡 Question: {query}\n📝 Answer: {response}")
208+
print("\n📚 Citations:")
209+
for citation in citations:
210+
print(f" {citation}")
197211

198212
print(f"✅ Indexed {success} documents in {bulk_latency:.0f}ms")
199213
print(f"🔍 Search Latency: {search_latency:.0f}ms")

0 commit comments

Comments
 (0)