Skip to content

Commit 876f604

Browse files
committed
adding readme methos for create mappings and inference endpoint
1 parent f2d67f3 commit 876f604

File tree

4 files changed

+134
-14
lines changed

4 files changed

+134
-14
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Local RAG with Elasticsearch & Local AI
2+
3+
Simple RAG (Retrieval-Augmented Generation) system using Elasticsearch for semantic search and Local AI as model provider.
4+
5+
## Prerequisites
6+
7+
- Docker
8+
- Python 3.11+
9+
10+
## Quick Start
11+
12+
### 1. Activate Virtual Environment
13+
14+
```bash
15+
python -m venv venv
16+
source venv/bin/activate
17+
```
18+
19+
### 2. Install Python Dependencies
20+
21+
```bash
22+
pip install -r requirements.txt
23+
```
24+
25+
### 3. Run the Script
26+
27+
```bash
28+
python script.py
29+
```
30+
31+

supporting-blog-content/local-rag-with-lightweight-elasticsearch/app-logs/why-elasticsearch-is-so-cool.md

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
>>> Why Elastic is so cool?
2+
3+
## Raw Response
4+
5+
```json
6+
{"created":1763994585,"object":"chat.completion","id":"d96f7e7a-003f-4167-bb07-501aa41ef8e7","model":"dolphin3.0-qwen2.5-0.5b","choices":[{"index":0,"finish_reason":"stop","message":{"role":"assistant","content":"The sky being blue is primarily due to the scattering of sunlight by the Earth's atmosphere. Here are a few key factors contributing to the blue color of the sky:\n\n1. **Scattering of Light**: Blue light is scattered more than other colors by the Earth's atmosphere. This scattering occurs because blue light has a shorter wavelength, which is more likely to travel through the Earth's atmosphere than other wavelengths. The scattering occurs as the light bounces off particles in the atmosphere, such as dust and water vapor, which have a higher concentration in the upper atmosphere.\n\n2. **Atmospheric Composition**: The Earth's atmosphere is composed of gases like nitrogen, oxygen, and water vapor. These gases have different properties, and their interactions with sunlight can affect the colors they transmit. Nitrogen and oxygen gases have a scattering effect that is more intense at shorter wavelengths than water vapor, which results in the blue color observed in the sky.\n\n3. **Atmospheric Reflection**: The blue color of the sky also depends on the amount of light reflecting off the Earth's surface. Clouds, for instance, can reflect a lot of sunlight, particularly blue and green light, which creates a blue tint to the sky. Additionally, the reflection of sunlight from the Earth's surface can contribute to the blue color observed in the sky.\n\n4. **Clouds and Precipitation**: The presence of clouds and precipitation can also affect the color of the sky. Clouds can scatter blue light, but they can also trap it, making the sky appear a bit less blue. Precipitation, particularly heavy rain or snow, can also scatter light, making the sky appear a bit darker and more gray.\n\nIn summary, the blue color of the sky is a result of the scattering of sunlight by the Earth's atmosphere, with the primary contributor being the scattering of blue light and the absorption of other wavelengths by particles in the atmosphere."}}],"usage":{"prompt_tokens":14,"completion_tokens":384,"total_tokens":398}}
7+
```
8+
9+
## Answer
10+
11+
The sky being blue is primarily due to the scattering of sunlight by the Earth's atmosphere. Here are a few key factors contributing to the blue color of the sky:
12+
13+
1. **Scattering of Light**: Blue light is scattered more than other colors by the Earth's atmosphere. This scattering occurs because blue light has a shorter wavelength, which is more likely to travel through the Earth's atmosphere than other wavelengths. The scattering occurs as the light bounces off particles in the atmosphere, such as dust and water vapor, which have a higher concentration in the upper atmosphere.
14+
2. **Atmospheric Composition**: The Earth's atmosphere is composed of gases like nitrogen, oxygen, and water vapor. These gases have different properties, and their interactions with sunlight can affect the colors they transmit. Nitrogen and oxygen gases have a scattering effect that is more intense at shorter wavelengths than water vapor, which results in the blue color observed in the sky.
15+
3. **Atmospheric Reflection**: The blue color of the sky also depends on the amount of light reflecting off the Earth's surface. Clouds, for instance, can reflect a lot of sunlight, particularly blue and green light, which creates a blue tint to the sky. Additionally, the reflection of sunlight from the Earth's surface can contribute to the blue color observed in the sky.
16+
4. **Clouds and Precipitation**: The presence of clouds and precipitation can also affect the color of the sky. Clouds can scatter blue light, but they can also trap it, making the sky appear a bit less blue. Precipitation, particularly heavy rain or snow, can also scatter light, making the sky appear a bit darker and more gray.
17+
18+
In summary, the blue color of the sky is a result of the scattering of sunlight by the Earth's atmosphere, with the primary contributor being the scattering of blue light and the absorption of other wavelengths by particles in the atmosphere.

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

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from openai import OpenAI
66

77
ES_URL = "http://localhost:9200"
8-
ES_API_KEY = "your-api-key-here"
8+
ES_API_KEY = "NDdDQWM1b0JPSDBFTV9JQzA0WVo6eHFXcWFJQmFYNzBwS1RjUllpRUNHZw=="
99
INDEX_NAME = "team-data"
1010
LOCAL_AI_URL = "http://localhost:8080/v1" # Local AI server URL
1111
DATASET_FOLDER = "./Dataset"
@@ -15,6 +15,79 @@
1515
ai_client = OpenAI(base_url=LOCAL_AI_URL, api_key="sk-x")
1616

1717

18+
def setup_inference_endpoint():
19+
inference_id = "e5-small-model"
20+
try:
21+
es_client.inference.put(
22+
inference_id=inference_id,
23+
task_type="text_embedding",
24+
body={
25+
"service": "elasticsearch",
26+
"service_settings": {
27+
"num_allocations": 1,
28+
"num_threads": 1,
29+
"model_id": ".multilingual-e5-small",
30+
},
31+
},
32+
)
33+
print(f"✅ Inference endpoint '{inference_id}' created successfully")
34+
except Exception as e:
35+
print(f"❌ Error creating inference endpoint: {str(e)}")
36+
37+
38+
def setup_inference_endpoint():
39+
inference_id = "e5-small-model"
40+
41+
try:
42+
es_client.inference.get(inference_id=inference_id)
43+
print(f"✅ Inference endpoint '{inference_id}' already exists")
44+
except Exception:
45+
print(f"📦 Creating inference endpoint '{inference_id}'...")
46+
47+
try:
48+
es_client.inference.put(
49+
inference_id=inference_id,
50+
task_type="text_embedding",
51+
body={
52+
"service": "elasticsearch",
53+
"service_settings": {
54+
"num_allocations": 1,
55+
"num_threads": 1,
56+
"model_id": ".multilingual-e5-small",
57+
},
58+
},
59+
)
60+
print(f"✅ Inference endpoint '{inference_id}' created successfully")
61+
except Exception as e:
62+
print(f"❌ Error creating inference endpoint: {str(e)}")
63+
64+
65+
def setup_index():
66+
try:
67+
if es_client.indices.exists(index=INDEX_NAME):
68+
print(f"✅ Index '{INDEX_NAME}' already exists")
69+
70+
print(f"📦 Creating index '{INDEX_NAME}'...")
71+
es_client.indices.create(
72+
index=INDEX_NAME,
73+
body={
74+
"mappings": {
75+
"properties": {
76+
"file_title": {"type": "text", "copy_to": "semantic_field"},
77+
"file_content": {"type": "text", "copy_to": "semantic_field"},
78+
"semantic_field": {
79+
"type": "semantic_text",
80+
"inference_id": "e5-small-model",
81+
},
82+
}
83+
}
84+
},
85+
)
86+
print(f"✅ Index '{INDEX_NAME}' created successfully")
87+
except Exception as e:
88+
print(f"❌ Error creating index: {str(e)}")
89+
90+
1891
def build_documents(dataset_folder, index_name):
1992
for filename in os.listdir(dataset_folder):
2093
if filename.endswith(".txt"):
@@ -88,7 +161,13 @@ def query_local_ai(prompt, model):
88161

89162

90163
if __name__ == "__main__":
91-
print("📥 Indexing documents...")
164+
print("🚀 Setting up infrastructure...")
165+
166+
# Setup inference endpoint and index
167+
setup_inference_endpoint()
168+
setup_index()
169+
170+
print("\n📥 Indexing documents...")
92171
success, bulk_latency = index_documents()
93172

94173
time.sleep(2) # Wait for indexing to complete
@@ -106,7 +185,10 @@ def query_local_ai(prompt, model):
106185

107186
prompt = f"{context}\nQuestion: {query}\nAnswer:"
108187

109-
ai_model = "dolphin3.0-qwen2.5-0.5b"
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"
191+
ai_model = "smollm2-1.7b-instruct"
110192

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

0 commit comments

Comments
 (0)