syntaxhacker commited on
Commit Β·
59b7a59
1
Parent(s): 6d5be16
refactor: remove Google AI, switch to OpenRouter, improve service runner
Browse files- app/main.py +4 -5
- requirements.txt +0 -2
- run.sh +124 -0
- start.sh +0 -25
app/main.py
CHANGED
|
@@ -45,15 +45,13 @@ openrouter_client = OpenAI(
|
|
| 45 |
)
|
| 46 |
|
| 47 |
# Model configuration
|
| 48 |
-
MODEL_NAME = "
|
| 49 |
|
| 50 |
# Initialize pipelines for all datasets
|
| 51 |
pipelines = {}
|
| 52 |
-
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 53 |
|
| 54 |
logger.info(f"Starting RAG Pipeline API")
|
| 55 |
logger.info(f"Port from env: {os.getenv('PORT', 'Not set - will use 8000')}")
|
| 56 |
-
logger.info(f"Google API Key present: {'Yes' if google_api_key else 'No'}")
|
| 57 |
logger.info(f"Available datasets: {list(DATASET_CONFIGS.keys())}")
|
| 58 |
|
| 59 |
# Define tools for the GLM model
|
|
@@ -170,9 +168,9 @@ async def chat_with_ai(request: ChatRequest):
|
|
| 170 |
|
| 171 |
# Execute tool calls
|
| 172 |
for tool_call in message.tool_calls:
|
| 173 |
-
if tool_call.function.name == "rag_qa":
|
| 174 |
# Parse arguments
|
| 175 |
-
args = json.loads(tool_call.function.arguments)
|
| 176 |
question = args.get("question")
|
| 177 |
dataset = args.get("dataset", request.dataset)
|
| 178 |
|
|
@@ -197,6 +195,7 @@ async def chat_with_ai(request: ChatRequest):
|
|
| 197 |
}
|
| 198 |
}
|
| 199 |
for tc in message.tool_calls
|
|
|
|
| 200 |
]
|
| 201 |
}
|
| 202 |
messages.append(assistant_message)
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
# Model configuration
|
| 48 |
+
MODEL_NAME = "nex-agi/nex-n2-pro:free"
|
| 49 |
|
| 50 |
# Initialize pipelines for all datasets
|
| 51 |
pipelines = {}
|
|
|
|
| 52 |
|
| 53 |
logger.info(f"Starting RAG Pipeline API")
|
| 54 |
logger.info(f"Port from env: {os.getenv('PORT', 'Not set - will use 8000')}")
|
|
|
|
| 55 |
logger.info(f"Available datasets: {list(DATASET_CONFIGS.keys())}")
|
| 56 |
|
| 57 |
# Define tools for the GLM model
|
|
|
|
| 168 |
|
| 169 |
# Execute tool calls
|
| 170 |
for tool_call in message.tool_calls:
|
| 171 |
+
if tool_call.function and tool_call.function.name == "rag_qa":
|
| 172 |
# Parse arguments
|
| 173 |
+
args = json.loads(tool_call.function.arguments or "{}")
|
| 174 |
question = args.get("question")
|
| 175 |
dataset = args.get("dataset", request.dataset)
|
| 176 |
|
|
|
|
| 195 |
}
|
| 196 |
}
|
| 197 |
for tc in message.tool_calls
|
| 198 |
+
if tc.function
|
| 199 |
]
|
| 200 |
}
|
| 201 |
messages.append(assistant_message)
|
requirements.txt
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
haystack-ai==2.10.3
|
| 2 |
datasets==3.3.2
|
| 3 |
sentence-transformers==3.4.1
|
| 4 |
-
google-ai-haystack==5.1.0
|
| 5 |
fastapi==0.115.4
|
| 6 |
uvicorn==0.31.0
|
| 7 |
openai==1.57.0
|
| 8 |
python-dotenv==1.0.1
|
| 9 |
-
httpx==0.28.1
|
| 10 |
pydantic==2.10.4
|
|
|
|
| 1 |
haystack-ai==2.10.3
|
| 2 |
datasets==3.3.2
|
| 3 |
sentence-transformers==3.4.1
|
|
|
|
| 4 |
fastapi==0.115.4
|
| 5 |
uvicorn==0.31.0
|
| 6 |
openai==1.57.0
|
| 7 |
python-dotenv==1.0.1
|
|
|
|
| 8 |
pydantic==2.10.4
|
run.sh
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
cd "$(dirname "$0")"
|
| 5 |
+
|
| 6 |
+
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 7 |
+
API_PORT="${API_PORT:-7860}"
|
| 8 |
+
API_LOG="/tmp/rag-pipeline-api.log"
|
| 9 |
+
API_PID="/tmp/rag-pipeline-api.pid"
|
| 10 |
+
|
| 11 |
+
# ββ Prerequisites βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
+
check_prereqs() {
|
| 13 |
+
if [ ! -f .env ]; then
|
| 14 |
+
echo "WARNING: .env file not found. Copy .env.example to .env and configure."
|
| 15 |
+
fi
|
| 16 |
+
if [ ! -d venv ]; then
|
| 17 |
+
echo "ERROR: venv/ not found. Run: python3 -m venv venv && uv pip install -r requirements.txt --python venv/bin/python"
|
| 18 |
+
exit 1
|
| 19 |
+
fi
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# ββ PID helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
is_running() {
|
| 24 |
+
[ -f "$1" ] && kill -0 "$(cat "$1")" 2>/dev/null
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# ββ Service management ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
stop_port() {
|
| 29 |
+
local port="$1" pids=""
|
| 30 |
+
pids="$(lsof -ti:"${port}" 2>/dev/null || true)"
|
| 31 |
+
if [ -z "$pids" ] && command -v fuser >/dev/null 2>&1; then
|
| 32 |
+
pids="$(fuser "${port}/tcp" 2>/dev/null || true)"
|
| 33 |
+
fi
|
| 34 |
+
if [ -z "$pids" ] && command -v ss >/dev/null 2>&1; then
|
| 35 |
+
pids="$(ss -tlnp "sport = :${port}" 2>/dev/null | grep -o "pid=[0-9]*" | cut -d= -f2 || true)"
|
| 36 |
+
fi
|
| 37 |
+
if [ -n "$pids" ]; then
|
| 38 |
+
echo " Killing process(es) on port $port: $pids"
|
| 39 |
+
kill $pids 2>/dev/null || true
|
| 40 |
+
sleep 1
|
| 41 |
+
fi
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
stop_service() {
|
| 45 |
+
local name="$1" port="$2" pid_file="$3"
|
| 46 |
+
stop_port "$port"
|
| 47 |
+
if [ -f "$pid_file" ]; then
|
| 48 |
+
local pid
|
| 49 |
+
pid="$(cat "$pid_file")"
|
| 50 |
+
kill "$pid" 2>/dev/null || true
|
| 51 |
+
rm -f "$pid_file"
|
| 52 |
+
fi
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# ββ Start functions βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
+
start_backend() {
|
| 57 |
+
if is_running "$API_PID"; then
|
| 58 |
+
echo "Backend already running (PID $(cat "$API_PID"))"
|
| 59 |
+
return
|
| 60 |
+
fi
|
| 61 |
+
|
| 62 |
+
echo "Starting backend (port $API_PORT)..."
|
| 63 |
+
|
| 64 |
+
setsid env UVICORN_RELOAD=0 venv/bin/uvicorn app.main:app --host 0.0.0.0 --port "$API_PORT" --log-level info >> "$API_LOG" 2>&1 &
|
| 65 |
+
echo $! > "$API_PID"
|
| 66 |
+
|
| 67 |
+
for i in $(seq 1 30); do
|
| 68 |
+
if curl -s http://localhost:$API_PORT/health > /dev/null 2>&1; then
|
| 69 |
+
echo " Backend started (PID $(cat "$API_PID"))"
|
| 70 |
+
return
|
| 71 |
+
fi
|
| 72 |
+
sleep 1
|
| 73 |
+
done
|
| 74 |
+
|
| 75 |
+
echo " ERROR: Backend failed to start. Check $API_LOG"
|
| 76 |
+
rm -f "$API_PID"
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# ββ Status βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
+
show_status() {
|
| 81 |
+
echo "ββ RAG Pipeline Services ββ"
|
| 82 |
+
if is_running "$API_PID"; then
|
| 83 |
+
echo " Backend: RUNNING (PID $(cat "$API_PID")) β http://localhost:$API_PORT"
|
| 84 |
+
echo " Status: β All services running"
|
| 85 |
+
else
|
| 86 |
+
echo " Backend: STOPPED"
|
| 87 |
+
echo " Status: β Backend is down"
|
| 88 |
+
fi
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
# ββ Commands βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 92 |
+
check_prereqs
|
| 93 |
+
|
| 94 |
+
case "${1:-status}" in
|
| 95 |
+
start)
|
| 96 |
+
start_backend
|
| 97 |
+
show_status
|
| 98 |
+
;;
|
| 99 |
+
stop)
|
| 100 |
+
echo "Stopping services..."
|
| 101 |
+
stop_service "backend" "$API_PORT" "$API_PID"
|
| 102 |
+
echo "All services stopped."
|
| 103 |
+
;;
|
| 104 |
+
restart)
|
| 105 |
+
stop_service "backend" "$API_PORT" "$API_PID"
|
| 106 |
+
sleep 1
|
| 107 |
+
start_backend
|
| 108 |
+
show_status
|
| 109 |
+
;;
|
| 110 |
+
status)
|
| 111 |
+
show_status
|
| 112 |
+
;;
|
| 113 |
+
logs)
|
| 114 |
+
if [ -f "$API_LOG" ]; then
|
| 115 |
+
tail -f "$API_LOG"
|
| 116 |
+
else
|
| 117 |
+
echo "No log file found at $API_LOG"
|
| 118 |
+
fi
|
| 119 |
+
;;
|
| 120 |
+
*)
|
| 121 |
+
echo "Usage: $0 {start|stop|restart|status|logs}"
|
| 122 |
+
exit 1
|
| 123 |
+
;;
|
| 124 |
+
esac
|
start.sh
DELETED
|
@@ -1,25 +0,0 @@
|
|
| 1 |
-
#!/bin/bash
|
| 2 |
-
set -e # Exit on any error
|
| 3 |
-
|
| 4 |
-
echo "=== RAG Pipeline Startup (TESTING)==="
|
| 5 |
-
echo "PORT environment variable: ${PORT:-'not set'}"
|
| 6 |
-
echo "Using port: 7860"
|
| 7 |
-
if [ -n "$GOOGLE_API_KEY" ]; then
|
| 8 |
-
echo "Google API Key present: Yes"
|
| 9 |
-
else
|
| 10 |
-
echo "Google API Key present: No"
|
| 11 |
-
fi
|
| 12 |
-
echo "Starting uvicorn server..."
|
| 13 |
-
echo "=== End Startup Info ==="
|
| 14 |
-
|
| 15 |
-
# Debug the uvicorn command
|
| 16 |
-
echo "Current directory: $(pwd)"
|
| 17 |
-
echo "Python path: $PYTHONPATH"
|
| 18 |
-
echo "Contents of current directory:"
|
| 19 |
-
ls -la
|
| 20 |
-
echo "Contents of app directory:"
|
| 21 |
-
ls -la app/
|
| 22 |
-
echo "Testing Python import:"
|
| 23 |
-
python -c "import app.main; print('Import successful')" || echo "Import failed"
|
| 24 |
-
echo "Starting uvicorn..."
|
| 25 |
-
uvicorn app.main:app --host 0.0.0.0 --port 7860 --log-level info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|