Skip to content

Instantly share code, notes, and snippets.

@brain90
Created May 30, 2025 02:00
Show Gist options
  • Save brain90/99383eb74217c2a090545aa10e8c16e4 to your computer and use it in GitHub Desktop.
Save brain90/99383eb74217c2a090545aa10e8c16e4 to your computer and use it in GitHub Desktop.
Bash Ollama LLM Simple Interfaces
#!/bin/bash
MODEL="wizard-vicuna-uncensored:latest"
API_URL="asupkeun-api-endpoint"
HISTORY="[]"
while true; do
read -p "You: " USER_INPUT
[[ -z "$USER_INPUT" ]] && break
HISTORY=$(jq -n --argjson h "$HISTORY" --arg m "$USER_INPUT" \
'$h + [{"role":"user","content":$m}]')
FULL_REPLY=""
while IFS= read -r line; do
content=$(echo "$line" | jq -r --exit-status 'select(.message.content != null) | .message.content' 2>/dev/null)
if [[ -n "$content" ]]; then
printf "%s" "$content"
FULL_REPLY+="$content"
fi
done < <(curl -sN "$API_URL" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"$MODEL\",
\"messages\": $HISTORY,
\"stream\": true
}")
echo
HISTORY=$(jq -n --argjson h "$HISTORY" --arg m "$FULL_REPLY" \
'$h + [{"role":"assistant","content":$m}]')
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment