Skip to content

Instantly share code, notes, and snippets.

@ken-morel
Last active February 4, 2026 19:25
Show Gist options
  • Select an option

  • Save ken-morel/4ac4e37ac61898a32b9142fcbb80c35b to your computer and use it in GitHub Desktop.

Select an option

Save ken-morel/4ac4e37ac61898a32b9142fcbb80c35b to your computer and use it in GitHub Desktop.
A fish script to query local openapi api to translate piped text to specified language and with a `serve` command to start a `llama-server` and qwen2.5-instruct-1.5b.
#!/usr/bin/env fish
if test "$argv[1]" = serve
rm -rf /tmp/translate-apikey-*
set api /tmp/translate-apikey-(random)
touch $api
echo "api key: $api"
llama-server -m ~/models/qwen2.5-instruct-1.5b.gguf --ctx-size 100 --threads-http 1 --sleep-idle-seconds 300 --threads 1 --threads-batch 1 --no-webui --api-key $api --port 8283
exit 0
end
# Configuration
set API_BASE "http://localhost:8283/v1"
set API_KEY (ls /tmp/translate-apikey-*)
set DEFAULT_MODEL "qwen2.5-instruct-1.5b"
if test "$API_KEY" = ""
echo "Api key file not found, server is surely not running"
end
# Dependencies check
if not command -q jq
echo "Error: 'jq' is required." >&2
exit 1
end
# Arguments: Only target_lang is required now
if test (count $argv) -lt 1
echo "Usage: echo 'text' | translate <target_lang> [<model>]" >&2
echo "Example: echo 'Hello' | translate French" >&2
exit 1
end
set TARGET_LANG $argv[1]
# If a second argument is provided, use it as the model
if test (count $argv) -ge 2
set MODEL $argv[2]
else
set MODEL $DEFAULT_MODEL
end
# Read from pipe
read -z TEXT
if test -z "$TEXT"
echo "Error: No input text provided." >&2
exit 1
end
# System prompt updated for Auto-Detection
set SYSTEM_PROMPT "You are a professional translator. Detect the source language of the input and translate it completely into $TARGET_LANG. Output ONLY the full translated text. No quotes, no explanations, no labels."
# Construct JSON
set JSON_DATA (jq -n \
--arg model "$MODEL" \
--arg sys "$SYSTEM_PROMPT" \
--arg user "$TEXT" \
'{
model: $model,
messages: [
{role: "system", content: $sys},
{role: "user", content: $user}
],
temperature: 0.1,
max_tokens: 1024
}')
# API Call
set RESPONSE (curl -s -X POST "$API_BASE/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$JSON_DATA")
if test $status -ne 0
echo "Error: Could not connect to API at $API_BASE" >&2
exit 1
end
# Extract and Clean Output
# set TRANSLATED (echo "$RESPONSE" | jq -r '.choices[0].message.content')
echo $RESPONSE | jq -r '.choices[0].message.content' | read -z TRANSLATED | string trim
if test "$TRANSLATED" = null -o -z "$TRANSLATED"
echo "Error: API returned an empty result." >&2
exit 1
end
echo "$TRANSLATED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment