-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add cosine similarity query (#3464)
## Relevant issue(s) Resolves #3349 ## Description This PR adds the possibility to calculate the cosine similarity between a vector field and a given vector. To achieve this we added the `_similarity` system field which take a target field (part of the parent object) and vector as parameter. ```gql query { User{ _similarity(pointsList: {vector: [1, 2, 0]}) } } ``` Note that the added code to mapper and planner is more of a "bolt on" addition given the current state of that part of the code base. A refactor is expected in the future. Future work will allow giving a `content` parameter instead of the `vector` if the target field has embedding generation configured. This will enable out-of-the-box RAG queries.
- Loading branch information
Showing
18 changed files
with
1,320 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2025 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package request | ||
|
||
// Similarity is a functional field that defines the | ||
// parameters to calculate the cosine similarity between two vectors. | ||
type Similarity struct { | ||
Field | ||
// Vector contains the vector to compare the target field to. | ||
// | ||
// It will be of type Int, Float32 or Float64. It must be the same type and length as Target. | ||
Vector any | ||
|
||
// Target is the field in the host object that we will compare the the vector to. | ||
// | ||
// It must be a field of type Int, Float32 or Float64. It must be the same type and length as Vector. | ||
Target string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2025 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package mapper | ||
|
||
import "github.com/sourcenetwork/defradb/internal/core" | ||
|
||
// Similarity represents an cosine similarity operation definition. | ||
type Similarity struct { | ||
Field | ||
// The mapping of this aggregate's parent/host. | ||
*core.DocumentMapping | ||
|
||
// The targetted field for the cosine similarity | ||
SimilarityTarget Targetable | ||
|
||
// The vector to compare the target field to. | ||
Vector any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.