feat(matching): rank with semantic embeddings and explain matches by evidence
This commit is contained in:
1 parent
facc1789b1
commit
24b64d698a
215 files changed
+16951
-327
No files matched your search
@@ -0,0 +1,427 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: semantic.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const deleteSemanticVectorsOfModel = `-- name: DeleteSemanticVectorsOfModel :exec
|
||||
|
||||
DELETE FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = $1
|
||||
AND subject_id = $2
|
||||
AND model_id = $3
|
||||
AND model_revision = $4
|
||||
AND prompt_version = $5
|
||||
`
|
||||
|
||||
type DeleteSemanticVectorsOfModelParams struct {
|
||||
SubjectKind string
|
||||
SubjectID pgtype.UUID
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
}
|
||||
|
||||
// Stored embedding vectors. A candidate keeps one set of rows for a model:
|
||||
// the job deletes every other row of the candidate and model before it
|
||||
// inserts the new set, in one transaction. A need keeps the rows of its
|
||||
// latest computed requirements snapshot the same way.
|
||||
func (q *Queries) DeleteSemanticVectorsOfModel(ctx context.Context, arg DeleteSemanticVectorsOfModelParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteSemanticVectorsOfModel,
|
||||
arg.SubjectKind,
|
||||
arg.SubjectID,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSemanticVectorsOfSubject = `-- name: DeleteSemanticVectorsOfSubject :exec
|
||||
DELETE FROM semantic_vectors
|
||||
WHERE subject_kind = $1 AND subject_id = $2
|
||||
`
|
||||
|
||||
type DeleteSemanticVectorsOfSubjectParams struct {
|
||||
SubjectKind string
|
||||
SubjectID pgtype.UUID
|
||||
}
|
||||
|
||||
// Removes every vector of a subject, of any model: a closed card or a need
|
||||
// without requirements keeps no derived data.
|
||||
func (q *Queries) DeleteSemanticVectorsOfSubject(ctx context.Context, arg DeleteSemanticVectorsOfSubjectParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteSemanticVectorsOfSubject, arg.SubjectKind, arg.SubjectID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertSemanticVector = `-- name: InsertSemanticVector :exec
|
||||
INSERT INTO semantic_vectors (
|
||||
subject_kind, subject_id, candidate_id, need_id, model_id, model_revision,
|
||||
prompt_version, item_key, content_sha256, label_sha256, dimension, vector,
|
||||
computed_at
|
||||
) VALUES (
|
||||
$1, $2, $3,
|
||||
$4, $5, $6,
|
||||
$7, $8, $9,
|
||||
$10, $11, $12,
|
||||
$13
|
||||
)
|
||||
`
|
||||
|
||||
type InsertSemanticVectorParams struct {
|
||||
SubjectKind string
|
||||
SubjectID pgtype.UUID
|
||||
CandidateID pgtype.UUID
|
||||
NeedID pgtype.UUID
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
ItemKey string
|
||||
ContentSha256 []byte
|
||||
LabelSha256 []byte
|
||||
Dimension int32
|
||||
Vector []byte
|
||||
ComputedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) InsertSemanticVector(ctx context.Context, arg InsertSemanticVectorParams) error {
|
||||
_, err := q.db.Exec(ctx, insertSemanticVector,
|
||||
arg.SubjectKind,
|
||||
arg.SubjectID,
|
||||
arg.CandidateID,
|
||||
arg.NeedID,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
arg.ItemKey,
|
||||
arg.ContentSha256,
|
||||
arg.LabelSha256,
|
||||
arg.Dimension,
|
||||
arg.Vector,
|
||||
arg.ComputedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listCandidateItemVectors = `-- name: ListCandidateItemVectors :many
|
||||
SELECT candidate_id, item_key, label_sha256, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'candidate'
|
||||
AND item_key <> 'whole'
|
||||
AND model_id = $1
|
||||
AND model_revision = $2
|
||||
AND prompt_version = $3
|
||||
AND candidate_id = ANY($4::uuid [])
|
||||
ORDER BY candidate_id, item_key
|
||||
`
|
||||
|
||||
type ListCandidateItemVectorsParams struct {
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
CandidateIds []pgtype.UUID
|
||||
}
|
||||
|
||||
type ListCandidateItemVectorsRow struct {
|
||||
CandidateID pgtype.UUID
|
||||
ItemKey string
|
||||
LabelSha256 []byte
|
||||
Dimension int32
|
||||
Vector []byte
|
||||
}
|
||||
|
||||
// The item vectors of the candidates that a search returns, at most a few
|
||||
// dozen candidates.
|
||||
func (q *Queries) ListCandidateItemVectors(ctx context.Context, arg ListCandidateItemVectorsParams) ([]ListCandidateItemVectorsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listCandidateItemVectors,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
arg.CandidateIds,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListCandidateItemVectorsRow
|
||||
for rows.Next() {
|
||||
var i ListCandidateItemVectorsRow
|
||||
if err := rows.Scan(
|
||||
&i.CandidateID,
|
||||
&i.ItemKey,
|
||||
&i.LabelSha256,
|
||||
&i.Dimension,
|
||||
&i.Vector,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listCandidateWholeVectors = `-- name: ListCandidateWholeVectors :many
|
||||
SELECT candidate_id, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'candidate'
|
||||
AND item_key = 'whole'
|
||||
AND model_id = $1
|
||||
AND model_revision = $2
|
||||
AND prompt_version = $3
|
||||
AND candidate_id = ANY($4::uuid [])
|
||||
ORDER BY candidate_id
|
||||
`
|
||||
|
||||
type ListCandidateWholeVectorsParams struct {
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
CandidateIds []pgtype.UUID
|
||||
}
|
||||
|
||||
type ListCandidateWholeVectorsRow struct {
|
||||
CandidateID pgtype.UUID
|
||||
Dimension int32
|
||||
Vector []byte
|
||||
}
|
||||
|
||||
// The whole-view vectors of the candidates of one search.
|
||||
func (q *Queries) ListCandidateWholeVectors(ctx context.Context, arg ListCandidateWholeVectorsParams) ([]ListCandidateWholeVectorsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listCandidateWholeVectors,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
arg.CandidateIds,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListCandidateWholeVectorsRow
|
||||
for rows.Next() {
|
||||
var i ListCandidateWholeVectorsRow
|
||||
if err := rows.Scan(&i.CandidateID, &i.Dimension, &i.Vector); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listNeedVectors = `-- name: ListNeedVectors :many
|
||||
SELECT item_key, content_sha256, dimension, vector
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = 'need'
|
||||
AND need_id = $1
|
||||
AND model_id = $2
|
||||
AND model_revision = $3
|
||||
AND prompt_version = $4
|
||||
AND content_sha256 = ANY($5::bytea [])
|
||||
ORDER BY item_key
|
||||
`
|
||||
|
||||
type ListNeedVectorsParams struct {
|
||||
NeedID pgtype.UUID
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
ContentDigests [][]byte
|
||||
}
|
||||
|
||||
type ListNeedVectorsRow struct {
|
||||
ItemKey string
|
||||
ContentSha256 []byte
|
||||
Dimension int32
|
||||
Vector []byte
|
||||
}
|
||||
|
||||
// The vectors of a need whose texts match the given digests of the
|
||||
// requirements snapshot that the search renders.
|
||||
func (q *Queries) ListNeedVectors(ctx context.Context, arg ListNeedVectorsParams) ([]ListNeedVectorsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listNeedVectors,
|
||||
arg.NeedID,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
arg.ContentDigests,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListNeedVectorsRow
|
||||
for rows.Next() {
|
||||
var i ListNeedVectorsRow
|
||||
if err := rows.Scan(
|
||||
&i.ItemKey,
|
||||
&i.ContentSha256,
|
||||
&i.Dimension,
|
||||
&i.Vector,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listNeedsAfter = `-- name: ListNeedsAfter :many
|
||||
SELECT id, organization_id, requirements_version
|
||||
FROM organization_needs
|
||||
WHERE id > $1
|
||||
ORDER BY id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListNeedsAfterParams struct {
|
||||
AfterID pgtype.UUID
|
||||
PageSize int32
|
||||
}
|
||||
|
||||
type ListNeedsAfterRow struct {
|
||||
ID pgtype.UUID
|
||||
OrganizationID pgtype.UUID
|
||||
RequirementsVersion int64
|
||||
}
|
||||
|
||||
// Needs and their current requirements snapshot for the backfill.
|
||||
func (q *Queries) ListNeedsAfter(ctx context.Context, arg ListNeedsAfterParams) ([]ListNeedsAfterRow, error) {
|
||||
rows, err := q.db.Query(ctx, listNeedsAfter, arg.AfterID, arg.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListNeedsAfterRow
|
||||
for rows.Next() {
|
||||
var i ListNeedsAfterRow
|
||||
if err := rows.Scan(&i.ID, &i.OrganizationID, &i.RequirementsVersion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listProfileCandidatesAfter = `-- name: ListProfileCandidatesAfter :many
|
||||
SELECT profiles.user_id
|
||||
FROM candidate_profiles AS profiles
|
||||
WHERE profiles.user_id > $1
|
||||
ORDER BY profiles.user_id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListProfileCandidatesAfterParams struct {
|
||||
AfterID pgtype.UUID
|
||||
PageSize int32
|
||||
}
|
||||
|
||||
// Candidates with a saved profile for the backfill, in identifier order
|
||||
// after a cursor. The job of a candidate who is not published removes the
|
||||
// stored vectors, so a backfill also cleans up after a closed card.
|
||||
func (q *Queries) ListProfileCandidatesAfter(ctx context.Context, arg ListProfileCandidatesAfterParams) ([]pgtype.UUID, error) {
|
||||
rows, err := q.db.Query(ctx, listProfileCandidatesAfter, arg.AfterID, arg.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []pgtype.UUID
|
||||
for rows.Next() {
|
||||
var user_id pgtype.UUID
|
||||
if err := rows.Scan(&user_id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, user_id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSemanticVectorDigests = `-- name: ListSemanticVectorDigests :many
|
||||
SELECT item_key, content_sha256
|
||||
FROM semantic_vectors
|
||||
WHERE
|
||||
subject_kind = $1
|
||||
AND subject_id = $2
|
||||
AND model_id = $3
|
||||
AND model_revision = $4
|
||||
AND prompt_version = $5
|
||||
ORDER BY item_key, content_sha256
|
||||
`
|
||||
|
||||
type ListSemanticVectorDigestsParams struct {
|
||||
SubjectKind string
|
||||
SubjectID pgtype.UUID
|
||||
ModelID string
|
||||
ModelRevision string
|
||||
PromptVersion string
|
||||
}
|
||||
|
||||
type ListSemanticVectorDigestsRow struct {
|
||||
ItemKey string
|
||||
ContentSha256 []byte
|
||||
}
|
||||
|
||||
// The digests of the stored rows of a subject and model, for the job that
|
||||
// decides whether anything changed.
|
||||
func (q *Queries) ListSemanticVectorDigests(ctx context.Context, arg ListSemanticVectorDigestsParams) ([]ListSemanticVectorDigestsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listSemanticVectorDigests,
|
||||
arg.SubjectKind,
|
||||
arg.SubjectID,
|
||||
arg.ModelID,
|
||||
arg.ModelRevision,
|
||||
arg.PromptVersion,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListSemanticVectorDigestsRow
|
||||
for rows.Next() {
|
||||
var i ListSemanticVectorDigestsRow
|
||||
if err := rows.Scan(&i.ItemKey, &i.ContentSha256); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockSemanticSubject = `-- name: LockSemanticSubject :exec
|
||||
SELECT pg_advisory_xact_lock($1::bigint)
|
||||
`
|
||||
|
||||
// Serializes the vector jobs of one subject: a job takes this lock before it
|
||||
// reads the subject, so the job that runs last also read last and its vectors
|
||||
// describe the latest committed state. User requests never take it.
|
||||
func (q *Queries) LockSemanticSubject(ctx context.Context, lockKey int64) error {
|
||||
_, err := q.db.Exec(ctx, lockSemanticSubject, lockKey)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user