-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathmemmachine-compose.sh
More file actions
executable file
·1157 lines (1034 loc) · 44.2 KB
/
memmachine-compose.sh
File metadata and controls
executable file
·1157 lines (1034 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# MemMachine Docker Startup Script
# This script helps you get MemMachine running with Docker Compose
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color
is_first_run=false
## Function to run a command with a timeout
timeout() {
local duration=$1
shift
# Run the command in the background
"$@" &
local cmd_pid=$!
# Start a background sleep that will kill the command
(
sleep "$duration"
kill -0 "$cmd_pid" 2>/dev/null && kill -TERM "$cmd_pid" 2>/dev/null
) &
local watchdog_pid=$!
# Wait for the command to finish and suppress termination messages
wait "$cmd_pid" 2>/dev/null
local status=$?
# Clean up watchdog if command finished early - suppress termination message
kill -TERM "$watchdog_pid" 2>/dev/null || true
wait "$watchdog_pid" 2>/dev/null || true
return $status
}
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_prompt() {
echo -ne "${MAGENTA}[PROMPT]${NC} " >&2
}
safe_sed_inplace() {
if sed --version >/dev/null 2>&1; then
# GNU/Linux sed
sed -i "$1" "$2"
else
# BSD/macOS sed
sed -i '' "$1" "$2"
fi
}
# Function to escape special characters for sed replacement string
# In sed replacement strings, we only need to escape: & (ampersand) and \ (backslash)
escape_for_sed() {
# Remove newlines and carriage returns first
local cleaned=$(echo "$1" | tr -d '\n\r')
# Escape backslashes first (must be done before escaping &)
cleaned=$(echo "$cleaned" | sed 's/\\/\\\\/g')
# Escape ampersands (used for matched text in sed replacement)
echo "$cleaned" | sed 's/&/\\&/g'
}
# Find docker compose available
find_docker_compose() {
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
# Use docker compose or docker-compose based on what's available
if docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
elif command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
else
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
print_success "Docker and Docker Compose are available"
}
# Check if .env file exists
check_env_file() {
if [ ! -f ".env" ]; then
print_warning ".env file not found. Creating from template..."
sleep 1
if [ -f "sample_configs/env.dockercompose" ]; then
cp sample_configs/env.dockercompose .env
print_success "Created .env file from sample_configs/env.dockercompose"
else
print_error "sample_configs/env.dockercompose file not found. Please create .env file manually."
exit 1
fi
else
print_success ".env file found"
fi
}
# Prompt user for LLM model selection based on provider
select_llm_model() {
local provider="$1"
local llm_model=""
case "$provider" in
"OPENAI")
print_prompt
read -p "Which OpenAI LLM model would you like to use? [gpt-4o-mini]: " llm_model
llm_model=$(echo "${llm_model:-gpt-4o-mini}" | tr -d '\n\r')
print_success "Selected OpenAI LLM model: $llm_model" >&2
;;
"BEDROCK")
print_prompt
read -p "Which AWS Bedrock LLM model would you like to use? [openai.gpt-oss-20b-1:0]: " llm_model
llm_model=$(echo "${llm_model:-openai.gpt-oss-20b-1:0}" | tr -d '\n\r')
print_success "Selected AWS Bedrock LLM model: $llm_model" >&2
;;
"OLLAMA")
print_prompt
read -p "Which Ollama LLM model would you like to use? [llama3]: " llm_model
llm_model=$(echo "${llm_model:-llama3}" | tr -d '\n\r')
print_success "Selected Ollama LLM model: $llm_model" >&2
;;
"OPENAI_COMPATIBLE")
print_prompt
read -p "Which OpenAI-compatible LLM model would you like to use? [qwen-flash]: " llm_model
llm_model=$(echo "${llm_model:-qwen-flash}" | tr -d '\n\r')
print_success "Selected OpenAI-compatible LLM model: $llm_model" >&2
;;
*)
print_warning "Unknown provider: $provider. Using default LLM model." >&2
llm_model="gpt-4o-mini"
;;
esac
echo "$llm_model"
}
# Prompt user for embedding model selection based on provider
select_embedding_model() {
local provider="$1"
local embedding_model=""
case "$provider" in
"OPENAI")
print_prompt
read -p "Which OpenAI embedding model would you like to use? [text-embedding-3-small]: " embedding_model
embedding_model=$(echo "${embedding_model:-text-embedding-3-small}" | tr -d '\n\r')
print_success "Selected OpenAI embedding model: $embedding_model" >&2
;;
"BEDROCK")
print_prompt
read -p "Which AWS Bedrock embedding model would you like to use? [amazon.titan-embed-text-v2:0]: " embedding_model
embedding_model=$(echo "${embedding_model:-amazon.titan-embed-text-v2:0}" | tr -d '\n\r')
print_success "Selected AWS Bedrock embedding model: $embedding_model" >&2
;;
"OLLAMA")
print_prompt
read -p "Which Ollama embedding model would you like to use? [nomic-embed-text]: " embedding_model
embedding_model=$(echo "${embedding_model:-nomic-embed-text}" | tr -d '\n\r')
print_success "Selected Ollama embedding model: $embedding_model" >&2
;;
"OPENAI_COMPATIBLE")
print_prompt
read -p "Which OpenAI-compatible embedding model would you like to use? [text-embedding-v4]: " embedding_model
embedding_model=$(echo "${embedding_model:-text-embedding-v4}" | tr -d '\n\r')
print_success "Selected OpenAI-compatible embedding model: $embedding_model" >&2
;;
*)
print_warning "Unknown provider: $provider. Using default embedding model." >&2
embedding_model="text-embedding-3-small"
;;
esac
echo "$embedding_model"
}
# Generate configuration file with only the needed sections for the selected provider
generate_config_for_provider() {
local config_source="$1"
local provider="$2"
local llm_model="$3"
local embedding_model="$4"
local escaped_llm_model=$(escape_for_sed "$llm_model")
local escaped_embedding_model=$(escape_for_sed "$embedding_model")
print_info "Generating configuration file for $provider provider..."
# Determine which model and embedder to use based on provider
case "$provider" in
"OPENAI")
local model_name="openai_model"
local embedder_name="openai_embedder"
local model_field="model"
local embedder_field="model"
;;
"BEDROCK")
local model_name="aws_model"
local embedder_name="aws_embedder_id"
local model_field="model_id"
local embedder_field="model_id"
;;
"OLLAMA")
local model_name="ollama_model"
local embedder_name="ollama_embedder"
local model_field="model"
local embedder_field="model"
;;
"OPENAI_COMPATIBLE")
# OpenAI-compatible providers (e.g. self-hosted / compatible APIs)
# Uses sample config entries: openai_compatible_model / openai_compatible_embedder
local model_name="openai_compatible_model"
local embedder_name="openai_compatible_embedder"
local model_field="model"
local embedder_field="model"
;;
*)
print_error "Unknown provider: $provider"
return 1
;;
esac
# Use awk to extract and build the configuration file
awk -v provider="$provider" \
-v model_name="$model_name" \
-v embedder_name="$embedder_name" \
-v llm_model="$llm_model" \
-v embedding_model="$embedding_model" \
-v model_field="$model_field" \
-v embedder_field="$embedder_field" \
-f- "$config_source" <<'AWK_SCRIPT' > configuration.yml
BEGIN {
in_model_section = 0
in_embedder_section = 0
in_current_model = 0
in_current_embedder = 0
current_section = ""
in_episodic = 0
in_semantic = 0
in_long_term = 0
in_short_term = 0
}
# Track embedders and language_models sections (2 spaces, under resources:)
/^ embedders:$/ {
if (in_model_section || in_embedder_section) {
print ""
}
in_embedder_section = 1
in_model_section = 0
in_current_embedder = 0
print
next
}
/^ language_models:$/ {
if (in_model_section || in_embedder_section) {
print ""
}
in_model_section = 1
in_embedder_section = 0
in_current_model = 0
print
next
}
# Exit embedders/language_models when hitting another 2-space section
/^ [a-zA-Z_][a-zA-Z0-9_]*:$/ && !/^ (embedders|language_models):$/ && !in_episodic && !in_semantic {
if (in_model_section || in_embedder_section) {
print ""
in_model_section = 0
in_embedder_section = 0
}
print
next
}
# Track current top-level section
/^[a-zA-Z_][a-zA-Z0-9_]*:$/ && !/^ / {
if (in_model_section || in_embedder_section) {
print ""
}
current_section = substr($1, 1, length($1) - 1)
in_model_section = 0
in_embedder_section = 0
in_current_model = 0
in_current_embedder = 0
# Track episodic_memory and semantic_memory sections
if (current_section == "episodic_memory") {
in_episodic = 1
in_long_term = 0
in_short_term = 0
} else {
in_episodic = 0
in_long_term = 0
in_short_term = 0
}
if (current_section == "semantic_memory") {
in_semantic = 1
} else {
in_semantic = 0
}
print
next
}
# Handle language_models section
in_model_section {
# Check if this is a model definition line (4 spaces)
if (/^ [a-zA-Z_][a-zA-Z0-9_]*:$/) {
model_key = substr($1, 1, length($1) - 1) # Remove trailing :
in_current_model = (model_key == model_name)
print
next
}
if (in_current_model) {
# Replace model field value if this is the model line
if (model_field == "model" && /^ model:/) {
print " model: \"" llm_model "\""
next
} else if (model_field == "model_id" && /^ model_id:/) {
print " model_id: \"" llm_model "\""
next
}
}
print
next
}
# Handle embedders section
in_embedder_section {
# Check if this is an embedder definition line (4 spaces)
if (/^ [a-zA-Z_][a-zA-Z0-9_]*:$/) {
embedder_key = substr($1, 1, length($1) - 1) # Remove trailing :
in_current_embedder = (embedder_key == embedder_name)
print
next
}
if (in_current_embedder) {
# Replace embedder model field value
if (embedder_field == "model" && /^ model:/) {
print " model: \"" embedding_model "\""
next
} else if (embedder_field == "model_id" && /^ model_id:/) {
print " model_id: \"" embedding_model "\""
next
}
}
print
next
}
# Handle episodic_memory section
in_episodic {
# Track long_term_memory subsection
if (/^ long_term_memory:/) {
in_long_term = 1
in_short_term = 0
print
next
}
# Track short_term_memory subsection
if (/^ short_term_memory:/) {
in_short_term = 1
in_long_term = 0
print
next
}
# Update embedder reference in long_term_memory
if (in_long_term && /^ embedder:/) {
print " embedder: " embedder_name
next
}
# Update llm_model reference in long_term_memory
if (in_long_term && /^ llm_model:/) {
print " llm_model: " model_name
next
}
# Update llm_model reference in short_term_memory
if (in_short_term && /^ llm_model:/) {
print " llm_model: " model_name
next
}
print
next
}
# Handle semantic_memory section - update model references
in_semantic {
if (/^ llm_model:/) {
print " llm_model: " model_name
next
} else if (/^ embedding_model:/) {
print " embedding_model: " embedder_name
next
}
print
next
}
# Default: print all other lines
{ print }
AWK_SCRIPT
print_success "Generated configuration file with $provider provider settings"
}
# In lieu of yq, use awk to read over the configuration.yml file line-by-line,
# and set the database credentials using the same environment variables as in docker-compose.yml
set_config_defaults() {
awk -v pg_user="${POSTGRES_USER:-memmachine}" \
-v pg_pass="${POSTGRES_PASSWORD:-memmachine_password}" \
-v pg_db="${POSTGRES_DB:-memmachine}" \
-v neo4j_user="${NEO4J_USER:-neo4j}" \
-v neo4j_pass="${NEO4J_PASSWORD:-neo4j_password}" '
/^storage:/ || /^vector_graph_store:/ {
vendor = ""
}
/^[a-zA-Z][^:]*:/ && !/^storage:/ && !/^vector_graph_store:/ {
vendor = ""
}
/vendor_name:/ {
vendor = $2
gsub(/^[ \t]+|[ \t]+$/, "", vendor) # trim whitespace
}
/provider:/ && /neo4j/ {
vendor = "neo4j"
}
/provider:/ && /postgres/ {
vendor = "postgres"
}
vendor == "neo4j" && /host:/ { sub(/localhost/, "neo4j") }
vendor == "neo4j" && /uri:/ { sub(/localhost/, "neo4j") }
vendor == "neo4j" && /password:/ { sub(/<YOUR_PASSWORD_HERE>/, neo4j_pass) }
# Handle postgres configurations
vendor == "postgres" && /host:/ { sub(/localhost/, "postgres") }
vendor == "postgres" && /user:/ { sub(/postgres/, pg_user) }
vendor == "postgres" && /db_name:/ { sub(/postgres/, pg_db) }
vendor == "postgres" && /password:/ { sub(/<YOUR_PASSWORD_HERE>/, pg_pass) }
{ print }
' configuration.yml > configuration.yml.tmp && mv configuration.yml.tmp configuration.yml
}
# Check if configuration.yml file exists
check_config_file() {
if [ ! -f "configuration.yml" ]; then
print_warning "configuration.yml file not found. Creating from template..."
sleep 1
# Ask user for CPU or GPU configuration, defaulting to CPU
print_prompt
read -p "Which configuration would you like to use for the Docker Image? (CPU/GPU) [CPU]: " config_type_input
local config_type=$(echo "${config_type_input:-CPU}" | tr '[:lower:]' '[:upper:]')
if [ "$config_type" = "GPU" ]; then
CONFIG_SOURCE="sample_configs/episodic_memory_config.gpu.sample"
MEMMACHINE_IMAGE="memmachine/memmachine:latest-gpu"
print_info "GPU configuration selected."
else
if [ -n "$config_type_input" ] && [ "$config_type" != "CPU" ]; then
print_warning "Invalid selection. Defaulting to CPU."
else
print_info "CPU configuration selected."
fi
CONFIG_SOURCE="sample_configs/episodic_memory_config.cpu.sample"
MEMMACHINE_IMAGE="memmachine/memmachine:latest-cpu"
fi
# Ask user for provider path (OpenAI, Bedrock, Ollama or OpenAI-compatible)
print_prompt
read -p "Which provider would you like to use? (OpenAI/Bedrock/Ollama/OpenAI-compatible) [OpenAI]: " provider_input
# Clean the input and set default
provider_input=$(echo "${provider_input:-OpenAI}" | tr -d '\n\r' | tr '[:lower:]' '[:upper:]' | tr '-' '_')
local provider="$provider_input"
# Validate provider selection
if [[ "$provider" != "OPENAI" && "$provider" != "BEDROCK" && "$provider" != "OLLAMA" && "$provider" != "OPENAI_COMPATIBLE" ]]; then
print_warning "Invalid provider selection: '$provider'. Defaulting to OpenAI."
provider="OPENAI"
fi
print_info "Selected provider: $provider"
# Update .env file with the selected image
if [ -f ".env" ]; then
# Remove existing MEMMACHINE_IMAGE from .env if it exists
safe_sed_inplace '/^MEMMACHINE_IMAGE=/d' .env
fi
echo "MEMMACHINE_IMAGE=${MEMMACHINE_IMAGE}" >> .env
print_success "Set MEMMACHINE_IMAGE to ${MEMMACHINE_IMAGE} in .env file"
if [ -f "$CONFIG_SOURCE" ]; then
# LLM model selection
local selected_llm_model=$(select_llm_model "$provider")
# embedding model selection
local selected_embedding_model=$(select_embedding_model "$provider")
# Generate configuration file with only needed sections for the selected provider
generate_config_for_provider "$CONFIG_SOURCE" "$provider" "$selected_llm_model" "$selected_embedding_model"
else
print_error "$CONFIG_SOURCE file not found. Please create configuration.yml file manually."
exit 1
fi
set_config_defaults
is_first_run=true
else
print_success "configuration.yml file found"
fi
}
select_openai_compatible_base_url() {
local llm_base_url=""
local embedder_base_url=""
local reply=""
if [ "$is_first_run" = true ]; then
print_prompt
read -p "Model base URL is not set. Would you like to configure custom base URLs? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -p "LLM base URL [https://api.openai.com/v1]: " llm_base_url
llm_base_url=$(echo "${llm_base_url:-https://api.openai.com/v1}" | tr -d '\n\r')
print_prompt
read -p "Use a different base URL for embedding? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -p "Embedding base URL [${llm_base_url}]: " embedder_base_url
embedder_base_url=$(echo "${embedder_base_url:-$llm_base_url}" | tr -d '\n\r')
else
embedder_base_url="$llm_base_url"
fi
if [ -n "$llm_base_url" ]; then
safe_sed_inplace "/openai_compatible_model:/,/base_url:/ s|base_url: .*|base_url: \"$llm_base_url\"|" configuration.yml
print_success "Set LLM base URL to $llm_base_url"
fi
if [ -n "$embedder_base_url" ]; then
safe_sed_inplace "/openai_compatible_embedder:/,/base_url:/ s|base_url: .*|base_url: \"$embedder_base_url\"|" configuration.yml
print_success "Set embedding base URL to $embedder_base_url"
fi
fi
else
print_success "Model base URL appears to be configured"
fi
}
# Prompt user if they would like to set their API keys based on provider; then set it in the .env file and configuration.yml file
set_provider_api_keys() {
local api_key=""
local aws_access_key=""
local aws_secret_key=""
local aws_region=""
local model_id=""
local base_url=""
local reply=""
if [ -f ".env" ]; then
source .env
# Get the actual model being used from configuration
local llm_model=$(grep "llm_model:" configuration.yml | awk '{print $2}' | tr -d ' ')
local embedder_model=$(grep "embedding_model:" configuration.yml | awk '{print $2}' | tr -d ' ')
# Configure OpenAI if selected
if [[ "$llm_model" == "openai_model" ]] || [[ "$embedder_model" == "openai_embedder" ]]; then
if grep -q "<YOUR_API_KEY>" configuration.yml; then
print_prompt
read -p "OpenAI API key is not set. Would you like to set your OpenAI API key? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -sp "Enter your OpenAI API key: " api_key
echo
# Use different delimiters to avoid conflicts with special characters
safe_sed_inplace "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=$api_key|" .env
safe_sed_inplace "s|api_key: <YOUR_API_KEY>|api_key: $api_key|g" configuration.yml
print_success "Set OPENAI_API_KEY in .env and configuration.yml"
fi
else
print_success "OpenAI API key appears to be configured"
fi
fi
# Configure OpenAI-compatible provider (OPENAI_COMPATIBLE)
if [[ "$llm_model" == "openai_compatible_model" ]] || [[ "$embedder_model" == "openai_compatible_embedder" ]]; then
if grep -q "<YOUR_API_KEY>" configuration.yml; then
print_prompt
read -p "API key is not set. Would you like to set your API key for the OpenAI-compatible provider? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -sp "Enter your API key: " api_key
echo
safe_sed_inplace "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=$api_key|" .env
safe_sed_inplace "s|api_key: <YOUR_API_KEY>|api_key: $api_key|g" configuration.yml
print_success "Set OPENAI_API_KEY in .env and configuration.yml"
else
# Auto-populate with EMPTY to prevent runtime errors (e.g. vLLM doesn't require API keys)
safe_sed_inplace "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=EMPTY|" .env
safe_sed_inplace "s|api_key: <YOUR_API_KEY>|api_key: EMPTY|g" configuration.yml
print_warning "API key set to 'EMPTY'. Update it later if your provider requires authentication."
fi
else
print_success "API key for OpenAI-compatible provider appears to be configured"
fi
# Base URL is configured only for OPENAI_COMPATIBLE.
select_openai_compatible_base_url
fi
# Configure Bedrock if selected
if [[ "$llm_model" == "aws_model" ]] || [[ "$embedder_model" == "aws_embedder_id" ]]; then
if grep -q "<AWS_ACCESS_KEY_ID>" configuration.yml || grep -q "<AWS_SECRET_ACCESS_KEY>" configuration.yml; then
print_prompt
read -p "AWS credentials are not set. Would you like to set your AWS credentials for Bedrock? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -sp "Enter your AWS Access Key ID: " aws_access_key
echo
print_prompt
read -sp "Enter your AWS Secret Access Key: " aws_secret_key
echo
print_prompt
read -p "Enter your AWS Region [us-west-2]: " aws_region
aws_region=${aws_region:-us-west-2}
# Use different delimiters to avoid conflicts with special characters
safe_sed_inplace "s|aws_access_key_id: <AWS_ACCESS_KEY_ID>|aws_access_key_id: $aws_access_key|g" configuration.yml
safe_sed_inplace "s|aws_secret_access_key: <AWS_SECRET_ACCESS_KEY>|aws_secret_access_key: $aws_secret_key|g" configuration.yml
safe_sed_inplace "s|region: .*|region: \"$aws_region\"|g" configuration.yml
print_success "Set AWS credentials and configuration in configuration.yml"
fi
else
print_success "AWS credentials appear to be configured"
fi
fi
# Configure Ollama if selected
if [[ "$llm_model" == "ollama_model" ]] || [[ "$embedder_model" == "ollama_embedder" ]]; then
print_prompt
read -p "Ollama base URL [http://host.docker.internal:11434/v1]: " base_url
base_url=${base_url:-http://host.docker.internal:11434/v1}
safe_sed_inplace "s|base_url: .*|base_url: \"$base_url\"|g" configuration.yml
print_success "Set Ollama base URL: $base_url"
fi
fi
}
# Check if required environment variables are set
check_required_env() {
if [ -f ".env" ]; then
source .env
# Get the actual model being used from configuration
local llm_model=$(grep "llm_model:" configuration.yml | awk '{print $2}' | tr -d ' ')
local embedder_model=$(grep "embedding_model:" configuration.yml | awk '{print $2}' | tr -d ' ')
# Check OpenAI API key if OpenAI is configured
if [[ "$llm_model" == "openai_model" ]] || [[ "$embedder_model" == "openai_embedder" ]]; then
if [ -z "$OPENAI_API_KEY" ] || [ "$OPENAI_API_KEY" = "your_openai_api_key_here" ]; then
print_warning "OPENAI_API_KEY is not set or is using placeholder value"
print_warning "Please set your OpenAI API key in the .env file"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
else
print_success "OPENAI_API_KEY is configured"
fi
fi
# Check OpenAI-compatible provider API key if configured
if [[ "$llm_model" == "openai_compatible_model" ]] || [[ "$embedder_model" == "openai_compatible_embedder" ]]; then
if [ -z "$OPENAI_API_KEY" ] || [ "$OPENAI_API_KEY" = "your_openai_api_key_here" ]; then
print_warning "OPENAI_API_KEY is not set or is using placeholder value"
print_warning "Please set your API key in the .env file for the OpenAI-compatible provider"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
elif [ "$OPENAI_API_KEY" = "EMPTY" ]; then
print_info "OPENAI_API_KEY is set to 'EMPTY' (no authentication - OK for providers like vLLM)"
else
print_success "OPENAI_API_KEY is configured (OpenAI-compatible provider)"
fi
if grep -q "openai_compatible_model:" configuration.yml && grep -q "base_url:" configuration.yml; then
print_success "OpenAI-compatible base URL appears to be configured"
else
print_warning "OpenAI-compatible base URL may be missing in configuration.yml"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
fi
fi
# Check AWS credentials if Bedrock is configured
if [[ "$llm_model" == "aws_model" ]] || [[ "$embedder_model" == "aws_embedder_id" ]]; then
if grep -q "<AWS_ACCESS_KEY_ID>" configuration.yml || grep -q "<AWS_SECRET_ACCESS_KEY>" configuration.yml; then
print_warning "AWS credentials are not set or are using placeholder values"
print_warning "Please set your AWS credentials in the configuration.yml file"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
else
print_success "AWS credentials appear to be configured"
fi
fi
# Check Ollama configuration
if [[ "$llm_model" == "ollama_model" ]] || [[ "$embedder_model" == "ollama_embedder" ]]; then
if grep -q "base_url: \"http://host.docker.internal:11434/v1\"" configuration.yml; then
print_success "Ollama configuration detected with default base URL"
else
print_success "Ollama configuration detected with custom base URL"
fi
fi
fi
}
# Check if configuration.yml has required fields
check_required_config() {
if [ -f "configuration.yml" ]; then
# Check for API key in configuration.yml - look for actual placeholder patterns
if grep -q "api_key.*your_.*_api_key_here" configuration.yml || grep -q "api_key.*sk-example" configuration.yml || grep -q "api_key.*sk-test" configuration.yml; then
print_warning "API key in configuration.yml appears to be a placeholder or example value"
print_warning "Please set your actual API key in the configuration.yml file"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
else
print_success "API key in configuration.yml appears to be configured"
fi
# Check for database credentials - look for generic placeholder passwords
if grep -q "password.*password" configuration.yml && ! grep -q "password.*memmachine_password" configuration.yml; then
print_warning "Database password in configuration.yml appears to be a placeholder"
print_warning "Please set your actual database password in the configuration.yml file"
print_prompt
read -p "Press Enter to continue anyway (some features may not work)..."
else
print_success "Database credentials in configuration.yml appear to be configured"
fi
fi
}
# Pull and start services
start_services() {
local memmachine_image_tmp="${ENV_MEMMACHINE_IMAGE:-}"
print_info "Pulling and starting MemMachine services..."
# Determine the target image
local target_image="${memmachine_image_tmp:-${MEMMACHINE_IMAGE:-memmachine/memmachine:latest}}"
print_info "Pulling latest images... (Target: $target_image)"
# Try to pull; if it fails (e.g. local image), warn and proceed with PULL_POLICY=if_not_present
# We capture the output to suppress "manifest unknown" errors for local images
if pull_output=$(MEMMACHINE_IMAGE="${target_image}" $COMPOSE_CMD pull 2>&1); then
# Pull successful
echo "$pull_output"
export PULL_POLICY="always"
else
# Pull failed
if echo "$pull_output" | grep -q 'manifest unknown'; then
# This is the expected error for local-only images
print_info "Image '${target_image}' not found in Docker Hub registry (manifest unknown). Assuming local image."
else
# Some other error (auth, network, etc) - show it!
print_error "Docker pull failed with unexpected error:"
echo "$pull_output"
fi
export PULL_POLICY="if_not_present"
fi
# Start services (override the image if specified in memmachine-compose.sh start <image>:<tag>)
if [ -n "${memmachine_image_tmp:-}" ]; then
MEMMACHINE_IMAGE="${memmachine_image_tmp}" $COMPOSE_CMD up -d
else
$COMPOSE_CMD up -d
fi
print_success "Services started successfully!"
}
# Wait for services to be healthy
wait_for_health() {
print_info "Waiting for services to be healthy..."
# Wait for services to be healthy
$COMPOSE_CMD ps
print_info "Checking service health..."
# Wait for PostgreSQL
print_info "Waiting for PostgreSQL to be ready..."
if timeout 120 bash -c "until docker exec memmachine-postgres pg_isready -U ${POSTGRES_USER:-memmachine} -d ${POSTGRES_DB:-memmachine}; do sleep 2; done"; then
print_success "PostgreSQL is ready"
else
print_error "PostgreSQL failed to become ready in 120 seconds. Check container logs and configuration."
exit 1
fi
# Wait for Neo4j
print_info "Waiting for Neo4j to be ready..."
if timeout 120 bash -c "until docker exec memmachine-neo4j cypher-shell -u ${NEO4J_USER:-neo4j} -p ${NEO4J_PASSWORD:-neo4j_password} 'RETURN 1' > /dev/null 2>&1; do sleep 2; done"; then
print_success "Neo4j is ready"
else
print_error "Neo4j failed to become ready in 120 seconds. Check container logs and configuration."
exit 1
fi
# Wait for MemMachine
print_info "Waiting for MemMachine to be ready..."
if timeout 120 bash -c "until curl -f http://localhost:${MEMORY_SERVER_PORT:-8080}/api/v2/health > /dev/null 2>&1; do sleep 5; done"; then
print_success "MemMachine is ready"
else
print_error "MemMachine failed to become ready in 120 seconds. Check container logs and configuration."
exit 1
fi
}
# Show service information
show_service_info() {
print_success "🎉 MemMachine is now running!"
echo ""
echo "Service URLs:"
echo " 📊 MemMachine API Docs: http://localhost:${MEMORY_SERVER_PORT:-8080}/docs"
echo " 🗄️ Neo4j Browser: http://localhost:${NEO4J_HTTP_PORT:-7474}"
echo " 📈 Health Check: http://localhost:${MEMORY_SERVER_PORT:-8080}/api/v2/health"
echo " 📊 Metrics: http://localhost:${MEMORY_SERVER_PORT:-8080}/api/v2/metrics"
echo ""
echo "Database Access:"
echo " 🐘 PostgreSQL: localhost:${POSTGRES_PORT:-5432} (user: ${POSTGRES_USER:-memmachine}, db: ${POSTGRES_DB:-memmachine})"
echo " 🔗 Neo4j Bolt: localhost:${NEO4J_PORT:-7687} (user: ${NEO4J_USER:-neo4j})"
echo ""
echo "Useful Commands:"
echo " 📋 View logs: ${COMPOSE_CMD} logs -f"
echo " 🛑 Stop services: ${COMPOSE_CMD} down"
echo " 🔄 Restart: ${COMPOSE_CMD} restart"
echo " 🧹 Clean up: ${COMPOSE_CMD} down -v"
echo ""
}
build_image() {
local name=""
local force="false"
local gpu="false" # default to false
local reply=""
local key=""
local value=""
while [[ $# -gt 0 ]]; do
# This section splits the key and value if they are separated by an "=" sign
if [[ "$1" == --* ]]; then
if [[ "$1" == *=* ]]; then
key=$(echo "$1" | cut -d '=' -f 1)
value=$(echo "$1" | cut -d '=' -f 2-)
shift
else
key="$1"
value="$2"
if [[ "$#" -ge 2 ]]; then
shift 2
else
print_error "Missing value for argument: $1"
exit 1
fi
fi
else
# If no leading "--", then this is not an option, so just use put the argument in $key
key="$1"
value=""
shift
fi
case "$key" in
--gpu)
gpu="$value"
;;
-f|--force)
force="true"
;;
*)
name="$key"
;;
esac
done
if [[ -z "$name" ]]; then
print_info "No name specified."
print_info "Using default name: memmachine/memmachine:latest"
name="memmachine/memmachine:latest"
fi
if [[ "$force" == "false" ]]; then
print_prompt
read -p "Building $name with '--build-arg GPU=[true|false]' (default: false): " reply
gpu=$(echo "${reply:-false}" | tr '[:upper:]' '[:lower:]')
if [[ "$gpu" != "true" && "$gpu" != "false" ]]; then
print_error "Invalid value for GPU: $gpu"
exit 1
fi
else
print_info "Building $name with '--build-arg GPU=$gpu'"
fi
# Proceed with build after validation passes
name="${name//+/_}"
# Generate PEP 440 compliant version from git describe
# Step 1: Get git describe output (e.g., "0.2.3-12-g2b5fd82" or "v0.2.3-12-g2b5fd82")
local git_version=$(git describe --tags --always 2>/dev/null || echo "")
# Step 2: Convert to PEP 440 format
# - Remove leading 'v' prefix if present
# - Convert "-12-g2b5fd82" to ".dev12+g2b5fd82" (PEP 440 compliant)
# - Use extended regex (-E) for better compatibility across systems
local scm_version=""
if [[ -n "$git_version" ]]; then
scm_version=$(echo "$git_version" | sed -E 's/^v//;s/-([0-9]+)-g([0-9a-f]+)/.dev\1+g\2/')
fi
# Step 3: Ensure we have a valid version (fallback to 0.0.0 if empty)
scm_version="${scm_version:-0.0.0}"
print_info "Building $name with GPU=$gpu (SCM_VERSION: $scm_version)"
docker build --build-arg GPU=$gpu --build-arg SCM_VERSION="$scm_version" -t "$name" .
}
# Configure reranker settings
configure_reranker() {
if [ "$is_first_run" != true ]; then
return
fi
if [ ! -f "configuration.yml" ]; then
return
fi
local is_gpu=false
if [[ "$MEMMACHINE_IMAGE" == *"latest-gpu"* ]]; then
is_gpu=true
fi
if [ "$is_gpu" = true ]; then
print_info "Default reranker: RRF hybrid (identity + BM25 + cross-encoder)"
print_prompt
read -p "Replace cross-encoder provider? (None/Cohere/AWS) [None]: " reranker_choice
else
print_info "Default reranker: RRF hybrid (identity + BM25)"
print_prompt
read -p "Add optional neural reranker? (None/Cohere/AWS) [None]: " reranker_choice
fi
local reranker=$(echo "${reranker_choice:-None}" | tr '[:lower:]' '[:upper:]')
case "$reranker" in
COHERE)
print_prompt
read -p "Would you like to set your Cohere API key? (y/N) " reply
if [[ $reply =~ ^[Yy]$ ]]; then
print_prompt
read -sp "Enter your Cohere API key: " cohere_key
echo
safe_sed_inplace "s|cohere_key: <COHERE_API_KEY>|cohere_key: $cohere_key|g" configuration.yml
print_success "Set Cohere API key in configuration.yml"
else
safe_sed_inplace "s|cohere_key: <COHERE_API_KEY>|cohere_key: EMPTY|g" configuration.yml
print_warning "Cohere API key set to 'EMPTY'. Update it later if needed."
fi