@freehour/supabase-langchain 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/embedding-service.d.ts.map +1 -1
- package/dist/errors.d.ts +26 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11030 -32
- package/package.json +21 -15
- package/supabase/schemas/supabase-langchain/1_schemas.sql +11 -0
- package/supabase/schemas/supabase-langchain/2_extensions.sql +2 -0
- package/supabase/schemas/supabase-langchain/3_functions.sql +71 -0
- package/supabase/schemas/supabase-langchain/4_tables.sql +48 -0
- package/supabase/schemas/supabase-langchain/5_triggers.sql +8 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"private": false,
|
|
4
4
|
"displayName": "Supabase-LangChain",
|
|
5
5
|
"description": "Integration package for using LangChain with Supabase",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.2",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
19
|
"supabase/migrations/0001_supabase_langchain.sql",
|
|
20
|
-
"supabase/schemas/supabase-langchain
|
|
20
|
+
"supabase/schemas/supabase-langchain",
|
|
21
21
|
"scripts/copy-supabase-assets.js"
|
|
22
22
|
],
|
|
23
23
|
"exports": {
|
|
@@ -27,27 +27,33 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@freehour/mime": "^1.1.
|
|
31
|
-
"@
|
|
32
|
-
"@langchain/classic": "^1.0.29",
|
|
30
|
+
"@freehour/mime": "^1.1.8",
|
|
31
|
+
"@langchain/classic": "^1.0.31",
|
|
33
32
|
"@langchain/community": "^1.1.27",
|
|
34
|
-
"@langchain/core": "^1.1.
|
|
33
|
+
"@langchain/core": "^1.1.41",
|
|
35
34
|
"@langchain/textsplitters": "^1.0.1",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"langchain": "^1.3.1",
|
|
39
|
-
"officeparser": "^6.0.7",
|
|
35
|
+
"langchain": "^1.3.4",
|
|
36
|
+
"officeparser": "^6.1.0",
|
|
40
37
|
"pdf-parse": "^2.4.5",
|
|
41
38
|
"srt-parser-2": "^1.2.3"
|
|
42
39
|
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@freehour/supabase-core": "^2.1.1",
|
|
42
|
+
"@supabase/supabase-js": "^2.104.0"
|
|
43
|
+
},
|
|
44
|
+
"overrides": {
|
|
45
|
+
"@browserbasehq/stagehand": "3.2.1"
|
|
46
|
+
},
|
|
43
47
|
"devDependencies": {
|
|
48
|
+
"@freehour/supabase-core": "^2.1.1",
|
|
44
49
|
"@freehour/eslint-rules": "^1.0.19",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
50
|
+
"@supabase/supabase-js": "^2.104.1",
|
|
51
|
+
"ajv": "^8.20.0",
|
|
52
|
+
"bun-types": "^1.3.13",
|
|
47
53
|
"eslint": "9.39.4",
|
|
48
|
-
"supabase": "^2.
|
|
49
|
-
"typescript": "^6.0.
|
|
50
|
-
"vite": "^8.0.
|
|
54
|
+
"supabase": "^2.95.3",
|
|
55
|
+
"typescript": "^6.0.3",
|
|
56
|
+
"vite": "^8.0.10",
|
|
51
57
|
"vite-plugin-dts": "^4.5.4"
|
|
52
58
|
},
|
|
53
59
|
"trustedDependencies": [
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- This file includes SQL commands for the langchain schema.
|
|
2
|
+
-- This schema is used by services of the supabase-langchain package.
|
|
3
|
+
|
|
4
|
+
-- Ensure the schema exists
|
|
5
|
+
CREATE SCHEMA IF NOT EXISTS langchain;
|
|
6
|
+
|
|
7
|
+
-- Grant access privileges
|
|
8
|
+
GRANT USAGE ON SCHEMA langchain TO "anon";
|
|
9
|
+
GRANT USAGE ON SCHEMA langchain TO "authenticated";
|
|
10
|
+
GRANT USAGE ON SCHEMA langchain TO "service_role";
|
|
11
|
+
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
-- Function to match documents based on a query embedding and optional metadata filter
|
|
2
|
+
-- Returns the top 'match_count' documents ordered by similarity to the query embedding.
|
|
3
|
+
-- The 'filter' parameter allows for additional filtering based on document metadata using json containment.
|
|
4
|
+
-- Must be in the public schema to be compatible with langchain.
|
|
5
|
+
CREATE OR REPLACE FUNCTION public.match_documents(
|
|
6
|
+
query_embedding vector(768),
|
|
7
|
+
match_count int,
|
|
8
|
+
filter jsonb DEFAULT '{}'::jsonb
|
|
9
|
+
)
|
|
10
|
+
RETURNS TABLE (
|
|
11
|
+
id uuid,
|
|
12
|
+
file_id uuid,
|
|
13
|
+
created_at timestamptz,
|
|
14
|
+
content text,
|
|
15
|
+
metadata jsonb,
|
|
16
|
+
embedding vector(768),
|
|
17
|
+
similarity float
|
|
18
|
+
)
|
|
19
|
+
SET search_path = ''
|
|
20
|
+
AS $$
|
|
21
|
+
#variable_conflict use_column
|
|
22
|
+
BEGIN
|
|
23
|
+
RETURN QUERY
|
|
24
|
+
SELECT
|
|
25
|
+
id,
|
|
26
|
+
file_id,
|
|
27
|
+
created_at,
|
|
28
|
+
content,
|
|
29
|
+
metadata,
|
|
30
|
+
embedding,
|
|
31
|
+
1 - (embedding OPERATOR(extensions.<=>) query_embedding) AS similarity
|
|
32
|
+
FROM public.embeddings
|
|
33
|
+
WHERE metadata @> filter
|
|
34
|
+
ORDER BY embedding OPERATOR(extensions.<=>) query_embedding
|
|
35
|
+
LIMIT match_count;
|
|
36
|
+
END;
|
|
37
|
+
$$ LANGUAGE plpgsql;
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
-- Function extracts the 'fileId' from metadata and assigns it to reference file_id.
|
|
41
|
+
-- LangChain inserts embeddings automatically with a user defined metadata;
|
|
42
|
+
-- this function is triggered before insert to populate the file_id column from the metadata.
|
|
43
|
+
CREATE OR REPLACE FUNCTION langchain.embeddings_apply_metadata()
|
|
44
|
+
RETURNS trigger
|
|
45
|
+
SET search_path = ''
|
|
46
|
+
AS $$
|
|
47
|
+
BEGIN
|
|
48
|
+
IF NEW.metadata ? 'fileId' THEN
|
|
49
|
+
NEW.file_id := NEW.metadata ->> 'fileId';
|
|
50
|
+
END IF;
|
|
51
|
+
RETURN NEW;
|
|
52
|
+
END;
|
|
53
|
+
$$ LANGUAGE plpgsql;
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
-- Function to return files in the specified bucket with no or outdated embeddings
|
|
57
|
+
CREATE OR REPLACE FUNCTION langchain.get_outdated_embeddings(bucket text)
|
|
58
|
+
RETURNS TABLE (
|
|
59
|
+
file_id uuid,
|
|
60
|
+
bucket_id uuid,
|
|
61
|
+
path_tokens text[]
|
|
62
|
+
)
|
|
63
|
+
SET search_path = ''
|
|
64
|
+
AS $$
|
|
65
|
+
BEGIN
|
|
66
|
+
RETURN QUERY
|
|
67
|
+
SELECT f.id, f.bucket_id, f.path_tokens
|
|
68
|
+
FROM langchain.outdated_embeddings AS f
|
|
69
|
+
WHERE f.bucket_id = bucket;
|
|
70
|
+
END;
|
|
71
|
+
$$ LANGUAGE plpgsql;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
-- ==========================================================
|
|
2
|
+
-- Tables
|
|
3
|
+
-- ==========================================================
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
-- Create the embeddings table
|
|
7
|
+
-- ---------------------------------------------------------
|
|
8
|
+
-- The table holds embeddings for chunked text files.
|
|
9
|
+
-- Must remain in the `public` schema for compatibility with LangChain.
|
|
10
|
+
-- When a file is deleted from the storage.objects table,
|
|
11
|
+
-- the corresponding embeddings are also deleted.
|
|
12
|
+
CREATE TABLE IF NOT EXISTS public.embeddings (
|
|
13
|
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
14
|
+
file_id UUID NOT NULL REFERENCES storage.objects(id) ON DELETE CASCADE, -- Not unique due to chunking
|
|
15
|
+
created_at timestamptz NOT NULL DEFAULT (now() AT TIME ZONE 'utc'),
|
|
16
|
+
content TEXT NOT NULL,
|
|
17
|
+
metadata JSONB,
|
|
18
|
+
embedding vector(768) -- For nomic-embed-text (768 dimensions)
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
-- =========================================================
|
|
23
|
+
-- Indexes
|
|
24
|
+
-- =========================================================
|
|
25
|
+
|
|
26
|
+
-- Vector similarity index (used by LangChain)
|
|
27
|
+
CREATE INDEX IF NOT EXISTS public_embeddings_embedding_idx
|
|
28
|
+
ON public.embeddings
|
|
29
|
+
USING ivfflat (embedding vector_cosine_ops)
|
|
30
|
+
WITH (lists = 128);
|
|
31
|
+
|
|
32
|
+
-- File lookup index
|
|
33
|
+
CREATE INDEX IF NOT EXISTS public_embeddings_file_id_idx
|
|
34
|
+
ON public.embeddings (file_id);
|
|
35
|
+
|
|
36
|
+
-- =========================================================
|
|
37
|
+
-- Views
|
|
38
|
+
-- =========================================================
|
|
39
|
+
|
|
40
|
+
-- View to return files with no or outdated embeddings
|
|
41
|
+
CREATE OR REPLACE VIEW langchain.outdated_embeddings AS
|
|
42
|
+
SELECT f.*
|
|
43
|
+
FROM storage.objects AS f
|
|
44
|
+
LEFT JOIN public.embeddings AS e
|
|
45
|
+
ON e.file_id = f.id
|
|
46
|
+
WHERE e.file_id IS NULL OR f.updated_at > e.created_at;
|
|
47
|
+
|
|
48
|
+
GRANT SELECT ON langchain.outdated_embeddings TO authenticated, service_role, anon;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
-- Trigger: Apply metadata before insert or update
|
|
2
|
+
-- ---------------------------------------------------------
|
|
3
|
+
-- LangChain inserts embeddings automatically; this trigger
|
|
4
|
+
-- allows customizing the insert by populating columns based on the provided document metadata.
|
|
5
|
+
CREATE TRIGGER langchain_embeddings_apply_metadata
|
|
6
|
+
BEFORE INSERT OR UPDATE ON public.embeddings
|
|
7
|
+
FOR EACH ROW
|
|
8
|
+
EXECUTE FUNCTION langchain.embeddings_apply_metadata();
|