prosty-rag / prosty-rag.cmd
jglowa's picture
Update prosty-rag.cmd
0216097 verified
echo \" >/dev/null ">NUL "\" \`" <#"; shopt -s expand_aliases || setopt aliases; alias @set='' @echo='echo'
@echo Prosty RAG v0.5 - Jerzy Glowacki na licencji Apache 2.0
:; # Zmienne:
@set llamafile=prosty-rag.llamafile
@set modelfile="Prosty RAG - Bielik-4.5B-v3.0-Instruct.gguf"
@set modelURL=https://huggingface.co/gaianet/Bielik-4.5B-v3.0-Instruct-GGUF/resolve/main/Bielik-4.5B-v3.0-Instruct-Q6_K.gguf?download=true
@set embedfile=prosty-rag.embedfile
@set embedmodelfile=multilingual-e5-large-instruct.gguf
@set embedmodelURL=https://huggingface.co/kcccat/multilingual-e5-large-instruct-Q6_K-GGUF/resolve/main/multilingual-e5-large-instruct-q6_k.gguf?download=true
@set dbfile=prosty-rag.db
@set indeksator=indeksator.cmd
@set logfile=prosty-rag.log
@set systemPrompt="Jesteś pomocnym asystentem. Odpowiedz szczegółowo na pytanie użytkownika korzystając tylko z podanego kontekstu, zawierającego wybrane fragmenty plików. Na końcu odpowiedzi podaj odwołanie do pliku źródła. Jeśli nie udało ci się odpowiedzieć na pytanie na podstawie kontekstu, to napisz to. Nie zmyślaj."
@set topK=3
:; # *NIX:
:; # Instalacja
:; [ ! -f $llamafile ] && echo Pobieranie $llamafile... && curl -Lo $llamafile https://huggingface.co/jglowa/prosty-rag/resolve/main/prosty-rag.llamafile?download=true && chmod +x $llamafile && echo Gotowe!
:; [ ! -f "$modelfile" ] && echo Pobieranie $modelfile... && curl -Lo "$modelfile" $modelURL && echo Gotowe!
:; [ ! -f $embedfile ] && echo Pobieranie $embedfile... && curl -Lo $embedfile https://github.com/niutech/llamafile/releases/download/0.9.3/embedfile && chmod +x $embedfile && echo Gotowe!
:; [ ! -f "$embedmodelfile" ] && echo Pobieranie $embedmodelfile... && curl -Lo "$embedmodelfile" $embedmodelURL && echo Gotowe!
:; [ ! -f $indeksator ] && echo Pobieranie $indeksator... && curl -Lo $indeksator https://huggingface.co/jglowa/prosty-rag/resolve/main/indeksator.cmd?download=true && chmod +x $indeksator && echo Gotowe!
:; [ ! -f $dbfile ] && ./$indeksator
:; # Uruchamianie
:; >$logfile
:; echo "Uruchamianie serwera embedfile na http://localhost:8081..."
:; perl -MIO::Socket::INET -e 'my $embedfile=shift; my $embedmodelfile=shift; my $dbfile=shift; my $topK=shift; $SIG{INT}=$SIG{TERM}=sub { exit(0); }; my $s=IO::Socket::INET->new(LocalAddr=>"localhost",LocalPort=>8081,Proto=>"tcp",Listen=>1,ReuseAddr=>1) or die $!; while(my $c=$s->accept) { $c->autoflush(1); my $req=<$c>//""; my ($q)=$req=~/\?q=([^ ]+)/; if(defined $q && $q ne "") { $q=~s/%([0-9A-Fa-f]{2})/chr(hex $1)/eg; $q=~s/[\n+]/ /g; $q=~s/['\''"]//g; open my $fh, "-|", "./$embedfile","-m","$embedmodelfile","sh","$dbfile","-separator"," ",".param init",".param set :q '\''\"$q\"'\''",".param set :qe5 '\''Instruct: Given a query, retrieve relevant passages that answer the query.\nQuery: $q'\''",".param set :k $topK","WITH vm AS (SELECT rowid, ROW_NUMBER() OVER (ORDER BY distance) vec_rank FROM vec_items WHERE line_embedding MATCH lembed(:qe5) AND k=:k), fm AS (SELECT rowid, ROW_NUMBER() OVER (ORDER BY rank) fts_rank FROM fts_items WHERE fts_items MATCH :q LIMIT :k) SELECT items.rowid, COALESCE(1.0/(60+vm.vec_rank),0) + COALESCE(1.0/(60+fm.fts_rank),0) AS rrf_score, items.line FROM items LEFT JOIN vm ON items.rowid = vm.rowid LEFT JOIN fm ON items.rowid = fm.rowid ORDER BY rrf_score DESC LIMIT :k" or die $!; my @out=<$fh>; close $fh; print $c "HTTP/1.0 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n",@out; } else { print $c "HTTP/1.0 400 Bad Request\r\nAccess-Control-Allow-Origin: *\r\n\r\nBrak zapytania"; } close $c; }' $embedfile $embedmodelfile $dbfile $topK &
:; echo "Uruchamianie serwera llamafile na http://localhost:8080..."
:; ./$llamafile --server --v2 -m "$modelfile" --prompt "$systemPrompt" --flash-attn 1 $@ 2>&1 | tee $logfile &
:; (tail -f $logfile &) | grep -q "server listen" && echo "Uruchamianie czatu na stronie http://localhost:8080 w przeglądarce..." && xdg-open "http://localhost:8080" || open "http://localhost:8080"; wait; exit $?
:; # Windows:
@echo off
chcp 65001 >nul
title Prosty RAG
setlocal enabledelayedexpansion
:; # Instalacja
if not exist %llamafile% echo Pobieranie %llamafile%... && curl -Lo %llamafile% https://huggingface.co/jglowa/prosty-rag/resolve/main/prosty-rag.llamafile?download=true && echo Gotowe^^!
if not exist %modelfile% echo Pobieranie %modelfile%... && curl -Lo %modelfile% %modelURL% && echo Gotowe^^!
if not exist %embedfile% echo Pobieranie %embedfile%... && curl -Lo %embedfile% https://github.com/niutech/llamafile/releases/download/0.9.3/embedfile && echo Gotowe^^!
if not exist %embedmodelfile% echo Pobieranie %embedmodelfile%... && curl -Lo %embedmodelfile% %embedmodelURL% && echo Gotowe^^!
if not exist %indeksator% echo Pobieranie %indeksator%... && curl -Lo %indeksator% https://huggingface.co/jglowa/prosty-rag/resolve/main/indeksator.cmd?download=true && echo Gotowe^^!
if not exist %dbfile% call %indeksator%
:; # Uruchamianie
break>%logfile%
start "Prosty RAG - embedfile" cmd /c "powershell -Command "$embedfile='%embedfile%'; $embedmodelfile='%embedmodelfile%'; $dbfile='%dbfile%'; $topK=%topK%; iex $([System.IO.File]::ReadAllText('%~f0'))""
start "Prosty RAG - llamafile" cmd /c "echo Uruchamianie serwera llamafile na http://localhost:8080... && %llamafile% --server --v2 -m %modelfile% --prompt %systemPrompt% --flash-attn 1 %* 2>&1 | find /v "" >%logfile%"
:waitForReady
findstr /c:"server listen" %logfile% >nul && echo Uruchamianie czatu na http://localhost:8080 w przeglądarce... && start http://localhost:8080 || (timeout 1 >nul & goto waitForReady)
endlocal
goto :eof
:; # PowerShell
#> | Out-Null
Add-Type -AssemblyName System.Web
$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$listener = [System.Net.HttpListener]::new()
$listener.Prefixes.Add('http://localhost:8081/')
$listener.Start()
echo 'Uruchamianie serwera embedfile na http://localhost:8081...'
try {
while ($listener.IsListening) {
$ctx = $listener.GetContext()
$q = ([System.Web.HttpUtility]::ParseQueryString($ctx.Request.Url.Query))['q'] -replace "['`"]", '' -replace "`n", ' '
$output = if ($q) { cmd /c $embedfile -m $embedmodelfile sh $dbfile -separator " " ".param init" ".param set :q '\`"$q\`"'" ".param set :qe5 'Instruct: Given a query, retrieve relevant passages that answer the query.\n Query: $q'" ".param set :k $topK" "WITH vm AS (SELECT rowid, ROW_NUMBER() OVER (ORDER BY distance) vec_rank FROM vec_items WHERE line_embedding MATCH lembed(:qe5) AND k=:k), fm AS (SELECT rowid, ROW_NUMBER() OVER (ORDER BY rank) fts_rank FROM fts_items WHERE fts_items MATCH :q LIMIT :k) SELECT items.rowid, COALESCE(1.0/(60+vm.vec_rank),0) + COALESCE(1.0/(60+fm.fts_rank),0) AS rrf_score, items.line FROM items LEFT JOIN vm ON items.rowid = vm.rowid LEFT JOIN fm ON items.rowid = fm.rowid ORDER BY rrf_score DESC LIMIT :k" } else { @('Brak zapytania') }
$response = $output | out-string
$bytes = [Text.Encoding]::UTF8.GetBytes($response)
$ctx.Response.AddHeader('Access-Control-Allow-Origin', '*')
$ctx.Response.ContentType = 'text/plain; charset=utf-8'
$ctx.Response.ContentLength64 = $bytes.Length
$ctx.Response.OutputStream.Write($bytes, 0, $bytes.Length)
$ctx.Response.Close()
}
} finally { $listener.Close() }