Skip to content

Commit c7327da

Browse files
up by default, but run should work
Signed-off-by: Adrian Cole <[email protected]>
1 parent 7f82332 commit c7327da

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

example-apps/chatbot-rag-app/README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,13 @@ working Python environment.
5959

6060
**Double-check you have a `.env` file with all your variables set first!**
6161

62-
#### Create your Elasticsearch index
63-
64-
First, ingest the data into elasticsearch:
65-
```bash
66-
docker compose run -T --rm --pull always create-index
67-
```
68-
69-
*Note*: This may take several minutes to complete
70-
71-
#### Run the application
72-
73-
Now, run the app, which listens on http://localhost:4000
7462
```bash
75-
docker compose run --rm --pull always api-frontend
63+
docker compose up --pull always --force-recreate
7664
```
7765

78-
#### Cleanup when finished
66+
*Note*: The first run may take several minutes to become available.
7967

80-
When you are done, clean up the services above like this:
68+
Clean up when finished, like this:
8169

8270
```bash
8371
docker compose down
@@ -203,9 +191,8 @@ See [Langchain documentation][loader-docs] for more ways to load documents.
203191
To build the app from source instead of using published images, pass the
204192
`--build` flag to Docker Compose instead of `--pull always`
205193

206-
For example, to run the create-index service from source:
207194
```bash
208-
docker compose run --rm --build create-index
195+
docker compose up --build --force-recreate
209196
```
210197

211198
---

example-apps/chatbot-rag-app/data/index_data.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def install_elser():
4949
es.ml.put_trained_model(
5050
model_id=ELSER_MODEL, input={"field_names": ["text_field"]}
5151
)
52+
5253
while True:
5354
status = es.ml.get_trained_models(
5455
model_id=ELSER_MODEL, include="definition_status"
@@ -57,19 +58,31 @@ def install_elser():
5758
break
5859
time.sleep(1)
5960

60-
# Step 1: Ensure ELSER_MODEL is deployed
61-
try:
62-
es.ml.start_trained_model_deployment(
63-
model_id=ELSER_MODEL, wait_for="fully_allocated"
64-
)
65-
print(f'"{ELSER_MODEL}" model is deployed')
66-
except BadRequestError:
67-
# This error means it already exists
68-
pass
61+
# Step 1: Ensure ELSER_MODEL is fully allocated
62+
if not is_elser_fully_allocated():
63+
try:
64+
es.ml.start_trained_model_deployment(
65+
model_id=ELSER_MODEL, wait_for="fully_allocated"
66+
)
67+
print(f'"{ELSER_MODEL}" model is deployed')
68+
except BadRequestError:
69+
pass
70+
71+
while True:
72+
if is_elser_fully_allocated():
73+
break
74+
time.sleep(1)
6975

7076
print(f'"{ELSER_MODEL}" model is ready')
7177

7278

79+
def is_elser_fully_allocated():
80+
stats = es.ml.get_trained_models_stats(model_id=ELSER_MODEL)
81+
deployment_stats = stats["trained_model_stats"][0].get("deployment_stats", {})
82+
allocation_status = deployment_stats.get("allocation_status", {})
83+
return allocation_status.get("state") == "fully_allocated"
84+
85+
7386
def main():
7487
install_elser()
7588

@@ -96,7 +109,7 @@ def main():
96109

97110
print(f"Split {len(workplace_docs)} documents into {len(docs)} chunks")
98111

99-
print(f"Creating Elasticsearch sparse vector store in {ELASTICSEARCH_URL}")
112+
print(f"Creating Elasticsearch sparse vector store for {ELASTICSEARCH_URL}")
100113

101114
store = ElasticsearchStore(
102115
es_connection=es,
@@ -110,6 +123,13 @@ def main():
110123
#
111124
# Once elastic/elasticsearch#107077 is fixed, we can use bulk_kwargs to
112125
# adjust the timeout.
126+
127+
print(f"Adding documents to index {INDEX}")
128+
129+
spinner = Halo(text="Processing bulk operation", spinner="dots")
130+
if stdout.isatty():
131+
spinner.start()
132+
113133
try:
114134
es.indices.delete(index=INDEX, ignore_unavailable=True)
115135
store.add_documents(list(docs))
@@ -124,6 +144,11 @@ def main():
124144
es.indices.delete(index=INDEX, ignore_unavailable=True)
125145
store.add_documents(list(docs))
126146

147+
if stdout.isatty():
148+
spinner.stop()
149+
150+
print(f"Documents added to index {INDEX}")
151+
127152

128153
def await_ml_tasks(max_timeout=600, interval=5):
129154
"""
@@ -141,23 +166,14 @@ def await_ml_tasks(max_timeout=600, interval=5):
141166
if not ml_tasks:
142167
return # likely a lost race on tasks
143168

144-
spinner = Halo(text="Awaiting ML tasks", spinner="dots")
145-
if stdout.isatty():
146-
spinner.start()
147-
else:
148-
print(f"Awaiting {len(ml_tasks)} ML tasks")
169+
print(f"Awaiting {len(ml_tasks)} ML tasks")
149170

150171
while time.time() - start_time < max_timeout:
151172
ml_tasks = get_ml_tasks()
152173
if len(ml_tasks) == 0:
153174
break
154175
time.sleep(interval)
155176

156-
if stdout.isatty():
157-
spinner.stop()
158-
else:
159-
print(f"ML tasks complete")
160-
161177
if ml_tasks:
162178
raise TimeoutError(
163179
f"Timeout reached. ML tasks are still running: {', '.join(ml_tasks)}"
@@ -176,4 +192,4 @@ def get_ml_tasks():
176192

177193
# Unless we run through flask, we can miss critical settings or telemetry signals.
178194
if __name__ == "__main__":
179-
main()
195+
raise RuntimeError("Run via the parent directory: 'flask create-index'")

example-apps/chatbot-rag-app/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
env_file:
1111
- .env
1212
# Add settings that allow `docker compose run` to use tty and accept Ctrl+C
13+
tty: true
1314
stdin_open: true
1415
command: flask create-index
1516
volumes:
@@ -29,6 +30,7 @@ services:
2930
env_file:
3031
- .env
3132
# Add settings that allow `docker compose run` to use tty and accept Ctrl+C
33+
tty: true
3234
stdin_open: true
3335
volumes:
3436
# VertexAI uses a file for GOOGLE_APPLICATION_CREDENTIALS, not an API key

0 commit comments

Comments
 (0)