generated from google-gemini/aistudio-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_query.sh
More file actions
executable file
·51 lines (42 loc) · 1.33 KB
/
ai_query.sh
File metadata and controls
executable file
·51 lines (42 loc) · 1.33 KB
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
#!/usr/bin/env bash
# AI Query Wrapper - Refined
set -euo pipefail
DB="${DB:-$HOME/_/ai/core.db}"
AI_BRIDGE="$HOME/_/ai/ai.sh"
LOG_DIR="$HOME/_/ai/logs"
mkdir -p "$LOG_DIR"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; }
query="$*"
if [ -z "$query" ]; then
log "Please provide a prompt. Usage: $0 'your prompt'"
exit 1
fi
# Force English/German, translate Chinese to English
processed_query=$(python3 - <<PYTHON
import sys, re
q = sys.argv[1]
# Remove Chinese characters
q_clean = re.sub(r'[\u4e00-\u9fff]+', '', q)
print(q_clean)
PYTHON
"$query")
# Run via AI Bridge if available
if [ -x "$AI_BRIDGE" ]; then
response=$("$AI_BRIDGE" query CORE "$processed_query")
else
# Fallback
response=$(ollama run 2244:latest <<<"$processed_query" 2>/dev/null || true)
fi
if [ -z "$response" ]; then
log "[ERROR] AI query failed or returned empty."
exit 1
fi
# Save to cache
if command -v sqlite3 >/dev/null; then
prompt_hash=$(echo -n "$processed_query" | sha256sum | awk '{print $1}')
# Ensure table exists
sqlite3 "$DB" "CREATE TABLE IF NOT EXISTS cache (prompt_hash TEXT PRIMARY KEY, final_answer TEXT);" 2>/dev/null || true
sqlite3 "$DB" "INSERT OR REPLACE INTO cache(prompt_hash, final_answer) VALUES ('$prompt_hash', '$(echo "$response" | sed "s/'/''/g")');" 2>/dev/null || true
fi
# Output
echo "$response"