Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions examples/paper_metadata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ def paper_metadata_flow(
)

paper_metadata = data_scope.add_collector()
metadata_embeddings = data_scope.add_collector()
author_papers = data_scope.add_collector()
metadata_embeddings = data_scope.add_collector()

with data_scope["documents"].row() as doc:
# Extract metadata
doc["basic_info"] = doc["content"].transform(extract_basic_info)
doc["first_page_md"] = doc["basic_info"]["first_page"].transform(
pdf_to_markdown
Expand All @@ -100,6 +101,24 @@ def paper_metadata_flow(
instruction="Please extract the metadata from the first page of the paper.",
)
)

# Collect metadata
paper_metadata.collect(
filename=doc["filename"],
title=doc["metadata"]["title"],
authors=doc["metadata"]["authors"],
abstract=doc["metadata"]["abstract"],
num_pages=doc["basic_info"]["num_pages"],
)

# Collect author to filename mapping
with doc["metadata"]["authors"].row() as author:
author_papers.collect(
author_name=author["name"],
filename=doc["filename"],
)

# Embed title and abstract, and collect embeddings
doc["title_embedding"] = doc["metadata"]["title"].transform(
cocoindex.functions.SentenceTransformerEmbed(
model="sentence-transformers/all-MiniLM-L6-v2"
Expand All @@ -119,27 +138,13 @@ def paper_metadata_flow(
min_chunk_size=200,
chunk_overlap=150,
)

paper_metadata.collect(
filename=doc["filename"],
title=doc["metadata"]["title"],
authors=doc["metadata"]["authors"],
abstract=doc["metadata"]["abstract"],
num_pages=doc["basic_info"]["num_pages"],
)
metadata_embeddings.collect(
id=cocoindex.GeneratedField.UUID,
filename=doc["filename"],
location="title",
text=doc["metadata"]["title"],
embedding=doc["title_embedding"],
)
with doc["metadata"]["authors"].row() as author:
author_papers.collect(
author_name=author["name"],
filename=doc["filename"],
)

with doc["abstract_chunks"].row() as chunk:
chunk["embedding"] = chunk["text"].transform(
cocoindex.functions.SentenceTransformerEmbed(
Expand All @@ -159,6 +164,11 @@ def paper_metadata_flow(
cocoindex.targets.Postgres(),
primary_key_fields=["filename"],
)
author_papers.export(
"author_papers",
cocoindex.targets.Postgres(),
primary_key_fields=["author_name", "filename"],
)
metadata_embeddings.export(
"metadata_embeddings",
cocoindex.targets.Postgres(),
Expand All @@ -170,8 +180,3 @@ def paper_metadata_flow(
)
],
)
author_papers.export(
"author_papers",
cocoindex.targets.Postgres(),
primary_key_fields=["author_name", "filename"],
)