228 lines
5.9 KiB
Go
228 lines
5.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: rag.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pgvector/pgvector-go"
|
|
)
|
|
|
|
const createRagChunk = `-- name: CreateRagChunk :one
|
|
INSERT INTO rag_chunks (
|
|
id, source_type, source_id, project_id, chunk_index, content, embedding
|
|
) VALUES (
|
|
COALESCE($7::uuid, uuidv7()),
|
|
$1, $2, $3, $4, $5, $6
|
|
)
|
|
RETURNING id, source_type, source_id, project_id, chunk_index, content, embedding, created_at, updated_at
|
|
`
|
|
|
|
type CreateRagChunkParams struct {
|
|
SourceType string `json:"source_type"`
|
|
SourceID pgtype.UUID `json:"source_id"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
ChunkIndex int32 `json:"chunk_index"`
|
|
Content string `json:"content"`
|
|
Embedding pgvector.Vector `json:"embedding"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) CreateRagChunk(ctx context.Context, arg CreateRagChunkParams) (RagChunk, error) {
|
|
row := q.db.QueryRow(ctx, createRagChunk,
|
|
arg.SourceType,
|
|
arg.SourceID,
|
|
arg.ProjectID,
|
|
arg.ChunkIndex,
|
|
arg.Content,
|
|
arg.Embedding,
|
|
arg.ID,
|
|
)
|
|
var i RagChunk
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.SourceType,
|
|
&i.SourceID,
|
|
&i.ProjectID,
|
|
&i.ChunkIndex,
|
|
&i.Content,
|
|
&i.Embedding,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteRagChunksBySourceIDs = `-- name: DeleteRagChunksBySourceIDs :exec
|
|
DELETE FROM rag_chunks
|
|
WHERE source_type = $1 AND source_id = ANY($2::uuid[])
|
|
`
|
|
|
|
type DeleteRagChunksBySourceIDsParams struct {
|
|
SourceType string `json:"source_type"`
|
|
Column2 []pgtype.UUID `json:"column_2"`
|
|
}
|
|
|
|
func (q *Queries) DeleteRagChunksBySourceIDs(ctx context.Context, arg DeleteRagChunksBySourceIDsParams) error {
|
|
_, err := q.db.Exec(ctx, deleteRagChunksBySourceIDs, arg.SourceType, arg.Column2)
|
|
return err
|
|
}
|
|
|
|
const searchRagChunks = `-- name: SearchRagChunks :many
|
|
SELECT
|
|
id, source_type, source_id, project_id, chunk_index, content, created_at, updated_at,
|
|
(1 - (embedding <=> $1))::float8 AS similarity
|
|
FROM rag_chunks
|
|
WHERE 1=1
|
|
AND ($2::uuid IS NULL OR project_id = $2::uuid)
|
|
AND ($3::varchar IS NULL OR source_type = $3::varchar)
|
|
AND (1 - (embedding <=> $1))::float8 >= $4::float8
|
|
ORDER BY embedding <=> $1
|
|
LIMIT $5
|
|
`
|
|
|
|
type SearchRagChunksParams struct {
|
|
Embedding pgvector.Vector `json:"embedding"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
SourceType pgtype.Text `json:"source_type"`
|
|
MatchThreshold float64 `json:"match_threshold"`
|
|
MatchCount int32 `json:"match_count"`
|
|
}
|
|
|
|
type SearchRagChunksRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
SourceType string `json:"source_type"`
|
|
SourceID pgtype.UUID `json:"source_id"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
ChunkIndex int32 `json:"chunk_index"`
|
|
Content string `json:"content"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Similarity float64 `json:"similarity"`
|
|
}
|
|
|
|
func (q *Queries) SearchRagChunks(ctx context.Context, arg SearchRagChunksParams) ([]SearchRagChunksRow, error) {
|
|
rows, err := q.db.Query(ctx, searchRagChunks,
|
|
arg.Embedding,
|
|
arg.ProjectID,
|
|
arg.SourceType,
|
|
arg.MatchThreshold,
|
|
arg.MatchCount,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []SearchRagChunksRow{}
|
|
for rows.Next() {
|
|
var i SearchRagChunksRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.SourceType,
|
|
&i.SourceID,
|
|
&i.ProjectID,
|
|
&i.ChunkIndex,
|
|
&i.Content,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Similarity,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const searchRagChunksLexical = `-- name: SearchRagChunksLexical :many
|
|
WITH terms AS (
|
|
SELECT DISTINCT trim(term) AS term
|
|
FROM unnest($2::text[]) AS term
|
|
WHERE trim(term) <> ''
|
|
),
|
|
matched AS (
|
|
SELECT
|
|
r.id,
|
|
r.source_type,
|
|
r.source_id,
|
|
r.project_id,
|
|
r.chunk_index,
|
|
r.content,
|
|
r.created_at,
|
|
r.updated_at,
|
|
COUNT(*)::float8 AS similarity
|
|
FROM rag_chunks r
|
|
JOIN terms t ON r.content ILIKE '%' || t.term || '%'
|
|
WHERE ($3::uuid IS NULL OR r.project_id = $3::uuid)
|
|
GROUP BY
|
|
r.id,
|
|
r.source_type,
|
|
r.source_id,
|
|
r.project_id,
|
|
r.chunk_index,
|
|
r.content,
|
|
r.created_at,
|
|
r.updated_at
|
|
)
|
|
SELECT
|
|
id, source_type, source_id, project_id, chunk_index, content, created_at, updated_at, similarity
|
|
FROM matched
|
|
ORDER BY similarity DESC, chunk_index ASC
|
|
LIMIT $1
|
|
`
|
|
|
|
type SearchRagChunksLexicalParams struct {
|
|
MatchCount int32 `json:"match_count"`
|
|
Terms []string `json:"terms"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type SearchRagChunksLexicalRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
SourceType string `json:"source_type"`
|
|
SourceID pgtype.UUID `json:"source_id"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
ChunkIndex int32 `json:"chunk_index"`
|
|
Content string `json:"content"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Similarity float64 `json:"similarity"`
|
|
}
|
|
|
|
func (q *Queries) SearchRagChunksLexical(ctx context.Context, arg SearchRagChunksLexicalParams) ([]SearchRagChunksLexicalRow, error) {
|
|
rows, err := q.db.Query(ctx, searchRagChunksLexical, arg.MatchCount, arg.Terms, arg.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []SearchRagChunksLexicalRow{}
|
|
for rows.Next() {
|
|
var i SearchRagChunksLexicalRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.SourceType,
|
|
&i.SourceID,
|
|
&i.ProjectID,
|
|
&i.ChunkIndex,
|
|
&i.Content,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Similarity,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|