Spaces:
Sleeping
Sleeping
| import os | |
| from supabase.client import create_client | |
| from langchain_community.vectorstores import SupabaseVectorStore | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| # 1. Connect to Supabase | |
| supabase = create_client( | |
| os.environ["SUPABASE_URL"], | |
| os.environ.get("SUPABASE_SERVICE_KEY") or os.environ["SUPABASE_ANON_KEY"], | |
| ) | |
| # 2. Create embeddings object | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") | |
| # 3. Add texts and metadata to load | |
| texts = [ | |
| "Hello world, this is my first document.", | |
| "Second doc on another topic.", | |
| "More example content here.", | |
| ] | |
| metadatas = [ | |
| {"source": "doc1"}, | |
| {"source": "doc2"}, | |
| {"source": "doc3"}, | |
| ] | |
| # 4. Ingest texts into Supabase | |
| vector_store = SupabaseVectorStore.from_texts( | |
| texts=texts, | |
| embedding=embeddings, | |
| metadatas=metadatas, | |
| client=supabase, | |
| table_name="documents", | |
| query_name="match_documents_langchain", | |
| ) | |
| # 5. OPTIONAL: Test retrieval | |
| results = vector_store.similarity_search("Hello world", k=1) | |
| print("Retrieved:", results) | |