Skip to content

Commit e1401d9

Browse files
committed
docs: move descriptions from docstring to field description
1 parent 8a6d88b commit e1401d9

File tree

1 file changed

+21
-25
lines changed
  • examples/product_recommendation

1 file changed

+21
-25
lines changed

examples/product_recommendation/main.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This example shows how to extract relationships from Markdown documents and build a knowledge graph.
33
"""
44

5-
import dataclasses
5+
from pydantic import BaseModel, Field
66
import datetime
77
import cocoindex
88
from jinja2 import Template
@@ -21,7 +21,6 @@
2121
GraphDbDeclaration = cocoindex.targets.Neo4jDeclaration
2222
conn_spec = neo4j_conn_spec
2323

24-
2524
# Template for rendering product information as markdown to provide information to LLMs
2625
PRODUCT_TEMPLATE = """
2726
# {{ title }}
@@ -42,48 +41,45 @@
4241
"""
4342

4443

45-
@dataclasses.dataclass
46-
class ProductInfo:
44+
class ProductInfo(BaseModel):
4745
id: str
4846
title: str
4947
price: float
5048
detail: str
5149

5250

53-
@dataclasses.dataclass
54-
class ProductTaxonomy:
51+
class ProductTaxonomy(BaseModel):
5552
"""
5653
Taxonomy for the product.
57-
58-
A taxonomy is a concise noun (or short noun phrase), based on its core functionality, without specific details such as branding, style, etc.
59-
60-
Always use the most common words in US English.
61-
62-
Use lowercase without punctuation, unless it's a proper noun or acronym.
63-
64-
A product may have multiple taxonomies. Avoid large categories like "office supplies" or "electronics". Use specific ones, like "pen" or "printer".
6554
"""
6655

67-
name: str
56+
name: str = Field(
57+
description="A taxonomy is a concise noun (or short noun phrase), based on its core functionality, "
58+
"without specific details such as branding, style, etc. Always use the most common words in US "
59+
"English. Use lowercase without punctuation, unless it's a proper noun or acronym. A product may "
60+
"have multiple taxonomies. Avoid large categories like 'office supplies' or 'electronics'. Use "
61+
"specific ones, like 'pen' or 'printer'."
62+
)
6863

6964

70-
@dataclasses.dataclass
71-
class ProductTaxonomyInfo:
65+
class ProductTaxonomyInfo(BaseModel):
7266
"""
7367
Taxonomy information for the product.
74-
75-
Fields:
76-
- taxonomies: Taxonomies for the current product.
77-
- complementary_taxonomies: Think about when customers buy this product, what else they might need as complementary products. Put labels for these complentary products.
7868
"""
7969

80-
taxonomies: list[ProductTaxonomy]
81-
complementary_taxonomies: list[ProductTaxonomy]
70+
taxonomies: list[ProductTaxonomy] = Field(
71+
description="Taxonomies for the current product."
72+
)
73+
complementary_taxonomies: list[ProductTaxonomy] = Field(
74+
"Think about when customers buy this product, what else they might need as complementary products. Put labels "
75+
"for these complementary products."
76+
)
8277

8378

8479
@cocoindex.op.function(behavior_version=2)
8580
def extract_product_info(product: cocoindex.Json, filename: str) -> ProductInfo:
86-
# Print markdown for LLM to extract the taxonomy and complimentary taxonomy
81+
"""Print markdown for LLM to extract the taxonomy and complimentary taxonomy."""
82+
8783
return ProductInfo(
8884
id=f"{filename.removesuffix('.json')}",
8985
title=product["title"],
@@ -97,7 +93,7 @@ def store_product_flow(
9793
flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope
9894
) -> None:
9995
"""
100-
Define an example flow that extracts triples from files and build knowledge graph.
96+
Define an example flow that extracts triples from files and build the knowledge graph.
10197
"""
10298
data_scope["products"] = flow_builder.add_source(
10399
cocoindex.sources.LocalFile(path="products", included_patterns=["*.json"]),

0 commit comments

Comments
 (0)