Skip to content

[EXT] Non Issue, local llm #1

Description

@alexzk1

Connector bash script to use with local LLM based on llama.cpp / OpenAI

#!/bin/bash

# --- CONFIGURATION ---
PRIMARY_URL="${LLM_PRIMARY_URL:-http://192.168.0.110:18080/v1/chat/completions}"
FALLBACK_URL="${LLM_SECONDARY_URL:-http://127.0.0.1:18083/v1/chat/completions}"
MODEL_NAME="${LLM_MODEL:-default-model}"

# --- CORE LOGIC ---

send_request() {
    local url="$1"
    local system_prompt="$2"
    local user_content="$3"

    if [[ -z "$system_prompt" ]]; then
        echo "[ERROR] System prompt is empty." >&2
        return 1
    fi

    local json_payload
    json_payload=$(jq -n \
        --arg msg "$user_content" \
        --arg sys "$system_prompt" \
        --arg mdl "$MODEL_NAME" \
        '{model: $mdl, messages: [{role: "system", content: $sys}, {role: "user", content: $msg}], temperature: 0.1}')

    curl -s -X POST "$url" \
        -H "Content-Type: application/json" \
        -d "$json_payload"
}

input_data=$(cat)

if [[ -z "$input_data" ]]; then
    echo "[ERROR] No input received via stdin." >&2
    exit 1
fi

if [[ "$input_data" == *$'\n\n'* ]]; then
    # ${var%%pattern} — удаляет самый длинный фрагмент, соответствующий паттерну с конца.
    system_msg="${input_data%%$'\n\n'*}"
    # ${var#*pattern} — удаляет кратчайший фрагмент, соответствующий паттерту с начала.
    user_msg="${input_data#*$'\n\n'}"
else
    system_msg="You are a security auditor."
    user_msg="$input_data"
fi

# --- EXECUTION WITH FAILOVER ---

RESPONSE=$(send_request "$PRIMARY_URL" "$system_msg" "$user_msg")

if [[ $? -eq 0 ]] && echo "$RESPONSE" | jq -e '.choices[0].message.content' >/dev/null 2>&1; then
    echo "$RESPONSE" | jq -r '.choices[0].message.content'
else
    echo "[WARN] Primary LLM failed or returned invalid JSON. Trying fallback..." >&2
    
    RESPONSE=$(send_request "$FALLBACK_URL" "$system_msg" "$user_msg")

    if [[ $? -eq 0 ]] && echo "$RESPONSE" | jq -e '.choices[0].message.content' >/dev/null 2>&1; then
        echo "$RESPONSE" | jq -r '.choices[0].message.content'
    else
        echo "[ERROR] Both LLM hosts failed or returned invalid JSON." >&2
        exit 1
    fi
fi

It is written by LLM, but it was re-written 3 times at my demand as it had logical errors I could see (i.e. it is reviewed by human).
It passes test from this repo:

 aurscan ./firefox-patch-bin/
  scanning firefox-patch-bin (1 files) ...

[ MAL! ] firefox-patch-bin  confidence 100%
         The PKGBUILD uses a source entry disguised as 'patches' to pull from an unrelated personal git repository and executes an arbitrary script during the build process.
         [critical] PKGBUILD: A source entry named 'patches' points to an unrelated personal git repository instead of upstream sources.
             > "patches::git+https://github.com/danikpapas/zenbrowser-patch.git"
         [critical] PKGBUILD: Executes an arbitrary script from the untrusted/unrelated source directory during build.
             > ./apply-patch.sh
         ↳ tokens: ~964 in / ~184 out · cost n/a

scanner usage: 1 call(s) · tokens: ~964 in / ~184 out · cost n/a
!! Installation blocked: 1 package(s) flagged MALICIOUS.
  [A]bort (default) / [r]eport to mailing list & abort / [c]ontinue anyway: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions