-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathretriever.py
More file actions
41 lines (29 loc) · 1.21 KB
/
retriever.py
File metadata and controls
41 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from llama_index.core import VectorStoreIndex, Document
from llama_index_auth0_ai import FGARetriever
from openfga_sdk.client.models import ClientBatchCheckItem
def create_store():
current_dir = os.path.dirname(__file__)
public_doc_path = os.path.join(current_dir, "../assets/docs/public-doc.md")
private_doc_path = os.path.join(current_dir, "../assets/docs/private-doc.md")
with open(public_doc_path, "r", encoding="utf-8") as file:
public_doc_content = file.read()
with open(private_doc_path, "r", encoding="utf-8") as file:
private_doc_content = file.read()
documents = [
Document(text=public_doc_content, doc_id="public-doc"),
Document(text=private_doc_content, doc_id="private-doc"),
]
vectorStoreIndex = VectorStoreIndex.from_documents(documents)
return vectorStoreIndex
def create_retriever(user: str):
base_retriever = create_store().as_retriever()
return FGARetriever(
base_retriever,
build_query=lambda node: ClientBatchCheckItem(
user=f"user:{user}",
object=f"doc:{node.node.ref_doc_id}",
relation="viewer",
),
)
__all__ = ["create_retriever", "create_retriever"]