Reranking di testi con Ollama e Qwen3 Embedding LLM - in Go
Implementate RAG? Ecco alcuni snippet di codice in Golang..
Questo piccolo esempio di codice Go per il Reranking chiama Ollama per generare gli embedding per la query e per ciascun documento candidato, ordinando poi in ordine decrescente in base alla similarità coseno.
Abbiamo già svolto un’attività simile - Reranking con modelli di embedding ma in Python, con un LLM diverso e quasi un anno fa.
Per una guida completa sulla costruzione di sistemi di generazione aumentata dal recupero (RAG), consulta il Tutorial sulla Generazione Aumentata dal Recupero (RAG): Architettura, Implementazione e Guida alla Produzione.
Un altro codice simile, ma che utilizza il Reranker Qwen3:

TL;DR
Il risultato appare molto buono, la velocità è di 0,128s per documento. La domanda è conteggiata come un documento. E anche l’ordinamento e la stampa sono inclusi in questa statistica.
Consumo di memoria LLM:
Nonostante la dimensione del modello su disco (ollama ls) sia inferiore a 3GB
dengcao/Qwen3-Embedding-4B:Q5_K_M 7e8c9ad6885b 2.9 GB
Nella VRAM della GPU ne richiede (non di poco) di più: 5.5GB. (ollama ps)
NAME ID SIZE
dengcao/Qwen3-Embedding-4B:Q5_K_M 7e8c9ad6885b 5.5 GB
Se hai una GPU da 8GB, dovrebbe andare bene.
Test di Reranking con Embedding su Ollama - Output di esempio
In tutti e tre i casi di test, il reranking con embedding utilizzando il modello Ollama dengcao/Qwen3-Embedding-4B:Q5_K_M è stato eccellente! Vedetelo voi stessi.
Abbiamo 7 file contenenti alcuni testi che descrivono ciò che dice il loro nome del file:
- ai_introduction.txt
- machine_learning.md
- qwen3-reranking-models.md
- ollama-parallelism.md
- ollama-reranking-models.md
- programming_basics.txt
- setup.log
eseguimento dei test:
Test di Reranking: Cos’è l’intelligenza artificiale e come funziona l’apprendimento automatico?
./rnk example_query.txt example_docs/
Using embedding model: dengcao/Qwen3-Embedding-4B:Q5_K_M
Ollama base URL: http://localhost:11434
Processing query file: example_query.txt, target directory: example_docs/
Query: Cos'è l'intelligenza artificiale e come funziona l'apprendimento automatico?
Found 7 documents
Extracting query embedding...
Processing documents...
=== RANKING BY SIMILARITY ===
1. example_docs/ai_introduction.txt (Score: 0.451)
2. example_docs/machine_learning.md (Score: 0.388)
3. example_docs/qwen3-reranking-models.md (Score: 0.354)
4. example_docs/ollama-parallelism.md (Score: 0.338)
5. example_docs/ollama-reranking-models.md (Score: 0.318)
6. example_docs/programming_basics.txt (Score: 0.296)
7. example_docs/setup.log (Score: 0.282)
Processed 7 documents in 0.899s (avg: 0.128s per document)
Test di Reranking: Come gestisce Ollama le richieste parallele?
./rnk example_query2.txt example_docs/
Using embedding model: dengcao/Qwen3-Embedding-4B:Q5_K_M
Ollama base URL: http://localhost:11434
Processing query file: example_query2.txt, target directory: example_docs/
Query: Come gestisce Ollama le richieste parallele?
Found 7 documents
Extracting query embedding...
Processing documents...
=== RANKING BY SIMILARITY ===
1. example_docs/ollama-parallelism.md (Score: 0.557)
2. example_docs/qwen3-reranking-models.md (Score: 0.532)
3. example_docs/ollama-reranking-models.md (Score: 0.498)
4. example_docs/ai_introduction.txt (Score: 0.366)
5. example_docs/machine_learning.md (Score: 0.332)
6. example_docs/programming_basics.txt (Score: 0.307)
7. example_docs/setup.log (Score: 0.257)
Processed 7 documents in 0.858s (avg: 0.123s per document)
Test di Reranking: Come possiamo fare il reranking dei documenti con Ollama?
./rnk example_query3.txt example_docs/
Using embedding model: dengcao/Qwen3-Embedding-4B:Q5_K_M
Ollama base URL: http://localhost:11434
Processing query file: example_query3.txt, target directory: example_docs/
Query: Come possiamo fare il reranking dei documenti con Ollama?
Found 7 documents
Extracting query embedding...
Processing documents...
=== RANKING BY SIMILARITY ===
1. example_docs/ollama-reranking-models.md (Score: 0.552)
2. example_docs/ollama-parallelism.md (Score: 0.525)
3. example_docs/qwen3-reranking-models.md (Score: 0.524)
4. example_docs/ai_introduction.txt (Score: 0.369)
5. example_docs/machine_learning.md (Score: 0.346)
6. example_docs/programming_basics.txt (Score: 0.316)
7. example_docs/setup.log (Score: 0.279)
Processed 7 documents in 0.882s (avg: 0.126s per document)
Codice Sorgente Go
Metti tutto in una cartella e compilalo come segue:
go build -o rnk
Sentiti libero di usarlo per scopi di intrattenimento o commerciali o caricarlo su GitHub se ti piace. Licenza MIT.
main.go
package main
import (
"fmt"
"log"
"os"
"sort"
"time"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "rnk [query-file] [target-directory]",
Short: "RAG system using Ollama embeddings",
Long: "A simple RAG system that extracts embeddings and ranks documents using Ollama",
Args: cobra.ExactArgs(2),
Run: runRnk,
}
var (
embeddingModel string
ollamaBaseURL string
)
func init() {
rootCmd.Flags().StringVarP(&embeddingModel, "model", "m", "dengcao/Qwen3-Embedding-4B:Q5_K_M", "Embedding model to use")
rootCmd.Flags().StringVarP(&ollamaBaseURL, "url", "u", "http://localhost:11434", "Ollama base URL")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runRnk(cmd *cobra.Command, args []string) {
queryFile := args[0]
targetDir := args[1]
startTime := time.Now()
fmt.Printf("Using embedding model: %s\n", embeddingModel)
fmt.Printf("Ollama base URL: %s\n", ollamaBaseURL)
fmt.Printf("Processing query file: %s, target directory: %s\n", queryFile, targetDir)
// Read query from file
query, err := readQueryFromFile(queryFile)
if err != nil {
log.Fatalf("Error reading query file: %v", err)
}
fmt.Printf("Query: %s\n", query)
// Find all text files in target directory
documents, err := findTextFiles(targetDir)
if err != nil {
log.Fatalf("Error finding text files: %v", err)
}
fmt.Printf("Found %d documents\n", len(documents))
// Extract embeddings for query
fmt.Println("Extracting query embedding...")
queryEmbedding, err := getEmbedding(query, embeddingModel, ollamaBaseURL)
if err != nil {
log.Fatalf("Error getting query embedding: %v", err)
}
// Process documents
fmt.Println("Processing documents...")
validDocs := make([]Document, 0)
for _, doc := range documents {
embedding, err := getEmbedding(doc.Content, embeddingModel, ollamaBaseURL)
if err != nil {
fmt.Printf("Warning: Failed to get embedding for %s: %v\n", doc.Path, err)
continue
}
similarity := cosineSimilarity(queryEmbedding, embedding)
doc.Score = similarity
validDocs = append(validDocs, doc)
}
if len(validDocs) == 0 {
log.Fatalf("No documents could be processed successfully")
}
// Sort by similarity score (descending)
sort.Slice(validDocs, func(i, j int) bool {
return validDocs[i].Score > validDocs[j].Score
})
// Display results
fmt.Println("\n=== RANKING BY SIMILARITY ===")
for i, doc := range validDocs {
fmt.Printf("%d. %s (Score: %.3f)\n", i+1, doc.Path, doc.Score)
}
totalTime := time.Since(startTime)
avgTimePerDoc := totalTime / time.Duration(len(validDocs))
fmt.Printf("\nProcessed %d documents in %.3fs (avg: %.3fs per document)\n",
len(validDocs), totalTime.Seconds(), avgTimePerDoc.Seconds())
}
documents.go
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func readQueryFromFile(filename string) (string, error) {
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return strings.TrimSpace(string(content)), nil
}
func findTextFiles(dir string) ([]Document, error) {
var documents []Document
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && isTextFile(path) {
content, err := os.ReadFile(path)
if err != nil {
fmt.Printf("Warning: Could not read file %s: %v\n", path, err)
return nil
}
documents = append(documents, Document{
Path: path,
Content: string(content),
})
}
return nil
})
return documents, err
}
func isTextFile(filename string) bool {
ext := strings.ToLower(filepath.Ext(filename))
textExts := []string{".txt", ".md", ".rst", ".csv", ".json", ".xml", ".html", ".htm", ".log"}
for _, textExt := range textExts {
if ext == textExt {
return true
}
}
return false
}
embeddings.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func getEmbedding(text string, model string, ollamaBaseURL string) ([]float64, error) {
req := OllamaEmbeddingRequest{
Model: model,
Prompt: text,
}
jsonData, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := http.Post(ollamaBaseURL+"/api/embeddings", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("ollama API error: %s", string(body))
}
var embeddingResp OllamaEmbeddingResponse
if err := json.NewDecoder(resp.Body).Decode(&embeddingResp); err != nil {
return nil, err
}
return embeddingResp.Embedding, nil
}
similarity.go
package main
func cosineSimilarity(a, b []float64) float64 {
if len(a) != len(b) {
return 0
}
var dotProduct, normA, normB float64
for i := range a {
dotProduct += a[i] * b[i]
normA += a[i] * a[i]
normB += b[i] * b[i]
}
if normA == 0 || normB == 0 {
return 0
}
return dotProduct / (sqrt(normA) * sqrt(normB))
}
func sqrt(x float64) float64 {
if x == 0 {
return 0
}
z := x
for i := 0; i < 10; i++ {
z = (z + x/z) / 2
}
return z
}
types.go
package main
// OllamaEmbeddingRequest represents the request payload for Ollama embedding API
type OllamaEmbeddingRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
}
// OllamaEmbeddingResponse represents the response from Ollama embedding API
type OllamaEmbeddingResponse struct {
Embedding []float64 `json:"embedding"`
}
// Document represents a document with its metadata
type Document struct {
Path string
Content string
Score float64
}
Link utili
- Ollama cheatsheet
- Reranking di documenti testuali con Ollama e modello Qwen3 Reranker - in Go
- Modelli di Embedding e Reranker Qwen3 su Ollama: Prestazioni all’avanguardia
- https://it.wikipedia.org/wiki/Generazione_aumentata_dal_recupero
- Installazione e configurazione della posizione dei modelli Ollama
- Scrittura di prompt efficaci per LLM
- Testing LLM: gemma2, qwen2 e Mistral Nemo su Ollama
- Confronto LLM: Mistral Small, Gemma 2, Qwen 2.5, Mistral Nemo, LLama3 e Phi - Su Ollama
- Test: Come Ollama utilizza le prestazioni della CPU Intel e i core efficienti
- Reranking con modelli di embedding su Ollama in Python
- Confronto delle capacità di riassunto dei LLM
- Fornitori di LLM Cloud