File size: 7,323 Bytes
a0be26c 0216097 a0be26c 4b7f197 0216097 a0be26c 88a671a 9989004 88a671a a0be26c 4b7f197 88a671a 9989004 fc03337 28cfeaf 0216097 28cfeaf a0be26c fc03337 88a671a a3e8c50 a0be26c 88a671a 9989004 a3e8c50 a0be26c 4b7f197 a3e8c50 88a671a 9989004 fc03337 a1a0a1b a0be26c fc03337 9989004 b924cff 9989004 78bf4b8 9989004 a0be26c 0216097 9989004 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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() }
|