2023-10-26 01:47:42 +00:00
|
|
|
import os
|
2023-10-27 02:44:30 +00:00
|
|
|
|
|
|
|
from langchain.chains.query_constructor.base import AttributeInfo
|
|
|
|
from langchain.embeddings import OpenAIEmbeddings
|
2023-10-26 01:47:42 +00:00
|
|
|
from langchain.llms.openai import OpenAI
|
|
|
|
from langchain.retrievers.self_query.base import SelfQueryRetriever
|
2023-10-27 02:44:30 +00:00
|
|
|
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
|
2023-10-26 01:47:42 +00:00
|
|
|
from langchain.vectorstores.supabase import SupabaseVectorStore
|
2023-10-27 02:44:30 +00:00
|
|
|
from supabase.client import create_client
|
2023-10-26 01:47:42 +00:00
|
|
|
|
|
|
|
supabase_url = os.environ.get("SUPABASE_URL")
|
|
|
|
supabase_key = os.environ.get("SUPABASE_SERVICE_KEY")
|
|
|
|
supabase = create_client(supabase_url, supabase_key)
|
|
|
|
|
|
|
|
embeddings = OpenAIEmbeddings()
|
|
|
|
|
|
|
|
vectorstore = SupabaseVectorStore(
|
|
|
|
client=supabase,
|
|
|
|
embedding=embeddings,
|
|
|
|
table_name="documents",
|
2023-10-27 02:44:30 +00:00
|
|
|
query_name="match_documents",
|
2023-10-26 01:47:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Adjust this based on the metadata you store in the `metadata` JSON column
|
|
|
|
metadata_field_info = [
|
|
|
|
AttributeInfo(
|
|
|
|
name="genre",
|
|
|
|
description="The genre of the movie",
|
|
|
|
type="string or list[string]",
|
|
|
|
),
|
|
|
|
AttributeInfo(
|
|
|
|
name="year",
|
|
|
|
description="The year the movie was released",
|
|
|
|
type="integer",
|
|
|
|
),
|
|
|
|
AttributeInfo(
|
|
|
|
name="director",
|
|
|
|
description="The name of the movie director",
|
|
|
|
type="string",
|
|
|
|
),
|
|
|
|
AttributeInfo(
|
|
|
|
name="rating", description="A 1-10 rating for the movie", type="float"
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Adjust this based on the type of documents you store
|
|
|
|
document_content_description = "Brief summary of a movie"
|
|
|
|
llm = OpenAI(temperature=0)
|
|
|
|
|
|
|
|
retriever = SelfQueryRetriever.from_llm(
|
2023-10-27 02:44:30 +00:00
|
|
|
llm, vectorstore, document_content_description, metadata_field_info, verbose=True
|
2023-10-26 01:47:42 +00:00
|
|
|
)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
chain = RunnableParallel({"query": RunnablePassthrough()}) | retriever
|