aboutsummarylogtreecommitdiffstats
path: root/localai.service
blob: aadc6ba2650158aea12bf9e8107f9b4a7c54b476 (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
70
71
72
73
74
75
76
77
78
79
80
81
[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 '\
  grpc_backends=""; \
  if test -n "$ARCH_LOCALAI_PYTHON_BACKENDS"; then \
    while IFS= read -r line; do \
      if test -n "$line"; then \
        name=$(echo "$line" | sed -r "s/([^ ]+) .*/\\1/g"); \
        entry="$name:%S/%N/backend-assets/grpc/python/$name/run.sh"; \
        if test -z "$grpc_backends"; then \
          grpc_backends="--external-grpc-backends=$entry"; \
        else \
          grpc_backends="$grpc_backends,$entry"; \
        fi; \
      fi; \
    done <<< "$ARCH_LOCALAI_PYTHON_BACKENDS"; \
    echo "grpc_backends: $grpc_backends"; \
  fi; \
  /usr/bin/localai run \
    --audio-path="%T/%N/audio" \
    --backend-assets-path="%S/%N" \
    --config-path="%S/%N" \
    --image-path="%T/%N/images" \
    --localai-config-dir="%S/%N/config" \
    --models-path="%S/%N/models" \
    --upload-path="%T/%N/upload" \
    $grpc_backends \
'

# 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 \
    while IFS= read -r line; do \
      if test -n "$line"; then \
        backend=$(echo "$line" | sed -r "s/([^ ]+) .+/\\1/g"); \
        deps=""; \
        if test "$backend" != "$line"; then \
          deps=$(echo "$line" | sed -r "s/[^ ]+ (.+)/\\1/g"); \
        fi; \
        bedir="%S/%N/backend-assets/grpc/python/$backend"; \
        if test /usr/bin/localai -nt $bedir/venv; then \
          if test -e $bedir/venv; then rm -r $bedir/venv; fi; \
        fi; \
        if test ! -d $bedir/venv; then \
          echo "re/creating venv: $backend with packages: $deps, in dir: $bedir"; \
          uv venv --system-site-packages $bedir/venv; \
          if test -n "$deps"; then \
            echo "venv $backend: installing deps: $deps"; \
            VIRTUAL_ENV=$bedir/venv uv pip install $deps; \
          fi \
        fi; \
      fi; \
    done <<< "$ARCH_LOCALAI_PYTHON_BACKENDS"; \
  fi'

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

[Install]
WantedBy=default.target