Skip to content

Commit

Permalink
update qdrant settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger12 committed Sep 28, 2024
1 parent 2cb850c commit 2575cdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
57 changes: 38 additions & 19 deletions database/qdrant_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context" // understand this and usage in file
"fmt"
"os"
"sync"
"time"

"github.com/google/uuid"
Expand All @@ -12,7 +13,12 @@ import (
"github.com/rs/zerolog/log"
)

var collectionName = os.Getenv("QDRANT_COLLECTION")
var (
qdrantClientInstance *qdrant.Client
qdrantClientOnce sync.Once
collectionName = os.Getenv("QDRANT_COLLECTION")
qdrantHost = os.Getenv("QDRANT_HOST")
)

type ScoredPoint struct {
Id *qdrant.PointId `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` // Point id
Expand All @@ -32,35 +38,33 @@ type GetOutputJSON struct {
ModelResponse string `json:"model_response"`
}

func InitializeQdrant() *qdrant.Client {
func initializeQdrant() (*qdrant.Client, error) {
client, err := qdrant.NewClient(&qdrant.Config{
Host: os.Getenv("QDRANT_HOST"),
Host: qdrantHost,
Port: 6334,
UseTLS: false,
})
if err != nil {
panic(err)
return nil, err
}

// Get a context for a minute
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// Execute health check
healthCheckResult, err := client.HealthCheck(ctx)
if err != nil {
log.Fatal().Msgf("Could not get health: %v", err)
return nil, err
}
log.Printf("Qdrant version: %s", healthCheckResult.GetVersion())
log.Info().Msgf("Qdrant version: %s", healthCheckResult.GetVersion())

// check if collection exists
exists, err := client.CollectionExists(context.Background(), collectionName)
if err != nil {
log.Fatal().Msgf("Could not check if collection exists: %v", err)
return nil, err
}

if exists {
log.Info().Msgf("Collection %s exists", collectionName)
return client
return client, nil
}

err = client.CreateCollection(ctx, &qdrant.CreateCollection{
Expand All @@ -74,14 +78,28 @@ func InitializeQdrant() *qdrant.Client {
Type: qdrant.QuantizationType_Int8,
AlwaysRam: qdrant.PtrOf(true),
}),
OptimizersConfig: &qdrant.OptimizersConfigDiff{
DefaultSegmentNumber: qdrant.PtrOf(uint64(16)), // used to minimize latency set to 2 to maximize throughput
},
})
if err != nil {
log.Fatal().Msgf("Could not create collection: %v", err)
} else {
log.Info().Msgf("Collection %s created", collectionName)
return nil, err
}

return client
log.Info().Msgf("Collection %s created", collectionName)
return client, nil
}

// GetQdrantClient returns a singleton instance of the Qdrant client
func GetQdrantClient() *qdrant.Client {
qdrantClientOnce.Do(func() {
var err error
qdrantClientInstance, err = initializeQdrant()
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize Qdrant client")
}
})
return qdrantClientInstance
}

func GetQdrant(client *qdrant.Client, vectors []float32) ([]GetOutputJSON, error) {
Expand All @@ -91,13 +109,16 @@ func GetQdrant(client *qdrant.Client, vectors []float32) ([]GetOutputJSON, error
Query: qdrant.NewQueryDense(vectors),
WithPayload: qdrant.NewWithPayloadInclude("model_response", "user_message"),
ScoreThreshold: qdrant.PtrOf(float32(0.7)), // TODO: make this configurable
Params: &qdrant.SearchParams{
Quantization: &qdrant.QuantizationSearchParams{
Rescore: qdrant.PtrOf(true), // remove if results are inaccruate
},
},
})
if err != nil {
log.Fatal().Msgf("Could not search points: %v", err)
}

client.Close()

log.Info().Msg("Searched points")

var outputData []GetOutputJSON
Expand Down Expand Up @@ -141,7 +162,5 @@ func PutQdrant(client *qdrant.Client, vectors []float32, message string, modelRe
}
fmt.Println("Upsert", len(upsertPoints), "points")

client.Close()

return operationInfo
}
5 changes: 2 additions & 3 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type PutResponseBody struct {

func HandleGetRequest(c *fiber.Ctx) error {
c.Accepts("text/plain", "application/json")
c.Accepts("json", "text")

log.Info().Msg("Handling GET request")
// Parse the JSON body using Sonic
Expand Down Expand Up @@ -60,7 +59,7 @@ func HandleGetRequest(c *fiber.Ctx) error {

// query qdrant for response
// initialize databases
qdrantClient := database.InitializeQdrant()
qdrantClient := database.GetQdrantClient()

log.Info().Msg("Initialized Qdrant client")

Expand Down Expand Up @@ -143,7 +142,7 @@ func HandlePutRequest(c *fiber.Ctx) error {

// query qdrant for response
// initialize databases
qdrantClient := database.InitializeQdrant()
qdrantClient := database.GetQdrantClient()

log.Info().Msg("Initialized Qdrant client")

Expand Down

0 comments on commit 2575cdf

Please sign in to comment.