aboutsummarylogtreecommitdiffstats
path: root/localai.service
blob: c0386e5fc10662dfc3a6a45d204dcf8b31e90576 (plain)
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
[Unit]
Description=LocalAI server

# "%C" Cache directory root: /var/cache (system) or "$XDG_CACHE_HOME" (user)
# "%E" Configuration directory root: /etc/ (system) or "$XDG_CONFIG_HOME" (user)
# "%S" State directory root: /var/lib (system) or $XDG_STATE_HOME (user)
# "%T" Directory for temporary files: /tmp or the path "$TMPDIR", "$TEMP" or "$TMP"
# "%N" Full unit name	Same as "%n", but with the type suffix removed

[Service]
User=localai
Group=localai
Type=exec
WorkingDirectory=%S/%N
Restart=on-failure
# PrivateTmp=yes

# python_backends, default environment and local env
EnvironmentFile=-%E/%N/python_backends.conf
EnvironmentFile=%E/%N/%N.conf
EnvironmentFile=-%S/%N/.env

# start server
ExecStart==bash -c '\
  python_backends=""; \
  if test -n "$ARCH_LOCALAI_PYTHON_BACKENDS"; then \
    python_backends=(${(f)"$(echo $ARCH_LOCALAI_PYTHON_BACKENDS)"}); \
    for line in "${python_backends[@]}"; do \
      entry="${line%% *}:%S/%N/backend-assets/grpc/python/${line%% *}/run.sh"; \
      if test -z "python_backends"; then \
        python_backends="--external-grpc-backends=$entry"; \
      else \
        python_backends="$python_backends,$entry"; \
      fi; \
    done; \
  fi; \
  /usr/bin/localai run \
    --audio-path="%T/%N/audio" \
    --backend-assets-path="%S/%N" \
    --config-path="%S/%N" \
    $python_backends \
    --image-path="%T/%N/images" \
    --localai-config-dir="%S/%N/config" \
    --models-path="%S/%N/models" \
    --upload-path="%T/%N/upload" \
'

# create virtualenvs for python backends, recreate if localai is newer than venv
ExecStartPost=bash -c 'sleep 3;\
if test -n "$ARCH_LOCALAI_PYTHON_BACKENDS"; then \
  python_backends=(${(f)"$(echo $ARCH_LOCALAI_PYTHON_BACKENDS)"}); \
  for line in "${python_backends[@]}"; do \
    backend="${line%% *}"; dependencies=""; \
    if test "$backend" != "$line"; then dependencies="${line#* }"; fi; \
    bedir="%S/%N/backend-assets/grpc/python/$backend"; \
    if test /usr/bin/localai -nt $bedir/venv; then rm -r $bedir/venv; fi; \
    if test ! -d $bedir/venv; then \
      echo "re/creating $bedir"; \
      uv venv --system-site-packages $bedir/venv; \
      if test -n "$dependencies"; then uv pip install $dependencies; fi \
    fi; \
  done; \
fi'

# make some time for virtualenvs to be installed
TimeoutStartSec=180

[Install]
WantedBy=default.target