8000 wip by adiom-mark · Pull Request #230 · adiom-data/dsync · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

wip #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

wip #230

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions connectors/vector/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ func (c *conn) WriteUpdates(ctx context.Context, r *connect.Request[adiomv1.Writ
return connect.NewResponse(&adiomv1.WriteUpdatesResponse{}), nil
}

func NewQdrantConn(chunker adiomv1connect.ChunkingServiceClient, embedder adiomv1connect.EmbeddingServiceClient, host string, port int) (adiomv1connect.ConnectorServiceHandler, error) {
c, err := NewQdrantConnector(host, port)
if err != nil {
return nil, err
}
return NewConn(fmt.Sprintf("%v:%v", host, port), "Qdrant", 200, chunker, embedder, c), nil
}

func NewWeaviateConn(chunker adiomv1connect.ChunkingServiceClient, embedder adiomv1connect.EmbeddingServiceClient, url string, groupID string, apiKey string, useIdentityMapper bool) (adiomv1connect.ConnectorServiceHandler, error) {
splitted := strings.SplitN(url, "://", 2)
if len(splitted) != 2 {
Expand Down
43 changes: 43 additions & 0 deletions connectors/vector/fromdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package vector

import (
"context"

"connectrpc.com/connect"
adiomv1 "github.com/adiom-data/dsync/gen/adiom/v1"
"go.mongodb.org/mongo-driver/bson"
)

type fromData struct {
field string
}

// GetEmbedding implements adiomv1connect.EmbeddingServiceHandler.
func (s *fromData) GetEmbedding(ctx context.Context, r *connect.Request[adiomv1.GetEmbeddingRequest]) (*connect.Response[adiomv1.GetEmbeddingResponse], error) {
var embeddings []*adiomv1.Embedding
for _, data := range r.Msg.GetData() {
var d []float64
v := bson.Raw(data).Lookup(s.field)
if v.IsZero() {
embeddings = append(embeddings, &adiomv1.Embedding{})
continue
}
if err := bson.UnmarshalValue(v.Type, v.Value, &d); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
embeddings = append(embeddings, &adiomv1.Embedding{Data: d})
}
return connect.NewResponse(&adiomv1.GetEmbeddingResponse{
Data: embeddings,
}), nil
}

func (s *fromData) GetSupportedDataTypes(context.Context, *connect.Request[adiomv1.GetSupportedDataTypesRequest]) (*connect.Response[adiomv1.GetSupportedDataTypesResponse], error) {
return connect.NewResponse(&adiomv1.GetSupportedDataTypesResponse{
Types: []adiomv1.DataType{adiomv1.DataType_DATA_TYPE_MONGO_BSON},
}), nil
}

func NewFromData(field string) *fromData {
return &fromData{field: field}
}
85 changes: 85 additions & 0 deletions connectors/vector/ollama.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package vector

import (
"bytes"
"context"
"encoding/json"
"net/http"

"connectrpc.com/connect"
adiomv1 "github.com/adiom-data/dsync/gen/adiom/v1"
"go.mongodb.org/mongo-driver/bson"
)

type ollamaDemo struct {
baseURL string
model string
}

type EmbeddingResponse struct {
Embedding []float64
}

func (s *ollamaDemo) getEmbedding(payload interface{}) ([]float64, error) {
payloadData, err := json.Marshal(payload)
if err != nil {
return nil, err
}

body, err := json.Marshal(map[string]string{
"model": s.model,
"prompt": string(payloadData),
})
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, s.baseURL+"/embeddings", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var embeddingResponse EmbeddingResponse
if err := json.NewDecoder(resp.Body).Decode(&embeddingResponse); err != nil {
return nil, err
}

return embeddingResponse.Embedding, nil
}

// GetEmbedding implements adiomv1connect.EmbeddingServiceHandler.
func (s *ollamaDemo) GetEmbedding(ctx context.Context, r *connect.Request[adiomv1.GetEmbeddingRequest]) (*connect.Response[adiomv1.GetEmbeddingResponse], error) {
var embeddings []*adiomv1.Embedding
for _, d := range r.Msg.GetData() {
var m map[string]interface{}
if err := bson.Unmarshal(d, &m); err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
e, err := s.getEmbedding(m)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}

embeddings = append(embeddings, &adiomv1.Embedding{Data: e})
}
return connect.NewResponse(&adiomv1.GetEmbeddingResponse{
Data: embeddings,
}), nil
}

func (s *ollamaDemo) GetSupportedDataTypes(context.Context, *connect.Request[adiomv1.GetSupportedDataTypesRequest]) (*connect.Response[adiomv1.GetSupportedDataTypesResponse], error) {
return connect.NewResponse(&adiomv1.GetSupportedDataTypesResponse{
Types: []adiomv1.DataType{adiomv1.DataType_DATA_TYPE_MONGO_BSON},
}), nil
}

func NewOllama(baseURL string, model string) *ollamaDemo {
return &ollamaDemo{baseURL: baseURL, model: model}
}
90 changes: 90 additions & 0 deletions connectors/vector/qdrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package vector

import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/qdrant/go-client/qdrant"

Check failure on line 8 in connectors/vector/qdrant.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/qdrant/go-client/qdrant; to add it:
)

type qdrantConnector struct {
client *qdrant.Client
}

func NewQdrantConnector(host string, port int) (*qdrantConnector, error) {
client, err := qdrant.NewClient(&qdrant.Config{
Host: host,
Port: port,
})
if err != nil {
return nil, err
}

return &qdrantConnector{client: client}, nil
}

var True bool = true

func (c *qdrantConnector) UpsertDocuments(ctx context.Context, namespace string, docs []*VectorDocument) error {
if len(docs) == 0 {
return nil
}
var points []*qdrant.PointStruct

var keywords []string
for _, doc := range docs {
keywords = append(keywords, doc.ID)
}
deleteRes, err := c.client.Delete(ctx, &qdrant.DeletePoints{
CollectionName: namespace,
Wait: &True,
Points: &qdrant.PointsSelector{
PointsSelectorOneOf: &qdrant.PointsSelector_Filter{
Filter: &qdrant.Filter{
Must: []*qdrant.Condition{
qdrant.NewMatchKeywords("_id", keywords...),
},
},
},
},
})
if err != nil {
return err
}
if deleteRes.GetStatus() != qdrant.UpdateStatus_Completed {
return fmt.Errorf("qdrant delete not completed successfully")
}

for _, doc := range docs {
for _, chunk := range doc.Chunks {
if len(chunk.Vector) == 0 {
continue
}
var vectors []float32
for _, v := range chunk.Vector {
vectors = append(vectors, float32(v))
}
d := chunk.Data.(map[string]interface{})
d["_id"] = doc.ID
points = append(points, &qdrant.PointStruct{
Id: qdrant.NewID(uuid.New().String()),
Payload: qdrant.NewValueMap(d),
Vectors: qdrant.NewVectors(vectors...),
})
}
}

operationInfo, err := c.client.Upsert(ctx, &qdrant.UpsertPoints{
CollectionName: namespace,
Points: points,
Wait: &True,
})
if err != nil {
return err
}
if operationInfo.GetStatus() == qdrant.UpdateStatus_Completed {
return nil
}
return fmt.Errorf("qdrant upsert not completed successfully")
}
61 changes: 61 additions & 0 deletions internal/app/options/connectorflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,44 @@ func GetRegisteredConnectors() []RegisteredConnector {
})(args, as)
},
},
{
Name: "qdrant",
IsConnector: func(s string) bool {
return strings.EqualFold(s, "qdrant")
},
Create: CreateHelperWithRestArgs("qdrant", "qdrant --host localhost --port 6334 --has-chunker [grpc://embedder-host:port] [grpc://chunker-host:port]", QdrantFlags(), func(c *cli.Context, args []string, _ AdditionalSettings) (adiomv1connect.ConnectorServiceHandler, []string, error) {
host := c.String("host")
port := c.Int("port")
chunker := c.Bool("has-chunker")
restArgs := c.Args().Slice()
var chunkerClient adiomv1connect.ChunkingServiceClient = vector.NewSimple()
var embedderClient adiomv1connect.EmbeddingServiceClient = vector.NewSimple()
var err error

if len(restArgs) == 0 {
return nil, nil, ErrMissingEmbedder
}
embedderClient, restArgs, err = ConfigureEmbedder(restArgs)
if err != nil {
return nil, nil, err
}
if chunker {
if len(restArgs) == 0 {
return nil, nil, ErrMissingChunker
}
chunkerClient, restArgs, err = ConfigureChunker(restArgs)
if err != nil {
return nil, nil, err
}
}

conn, err := vector.NewQdrantConn(chunkerClient, embedderClient, host, port)
if err != nil {
return nil, nil, err
}
return conn, restArgs, err
}),
},
{
Name: "weaviate",
IsConnector: func(s string) bool {
Expand Down Expand Up @@ -396,6 +434,29 @@ func GetRegisteredConnectors() []RegisteredConnector {
}
}

func QdrantFlags() []cli.Flag {
return []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "host",
Usage: "e.g. localhost",
Required: true,
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "port",
Usage: "e.g. 6334",
Required: true,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "has-chunker",
Usage: "Specifies that there will be grpc chunker specified (grpc://chunker-host:port). If embedder also specified, first arg is chunker.",
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "has-embedder",
Usage: "Specifies that there will be grpc embedder specified (grpc://embedder-host:port). If chunker also specified, last arg is embedder.",
}),
}
}

func WeaviateFlags() []cli.Flag {
return []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Expand Down
Loading
0