|
| 1 | +import cocoindex |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +@cocoindex.flow_def(name="PostgresMessageIndexing") |
| 6 | +def postgres_message_indexing_flow( |
| 7 | + flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope |
| 8 | +) -> None: |
| 9 | + """ |
| 10 | + Define a flow that reads data from a PostgreSQL table, generates embeddings, |
| 11 | + and stores them in another PostgreSQL table with pgvector. |
| 12 | + """ |
| 13 | + |
| 14 | + data_scope["messages"] = flow_builder.add_source( |
| 15 | + cocoindex.sources.Postgres( |
| 16 | + table_name="source_messages", |
| 17 | + # Optional. Use the default CocoIndex database if not specified. |
| 18 | + database=cocoindex.add_transient_auth_entry( |
| 19 | + cocoindex.sources.DatabaseConnectionSpec( |
| 20 | + url=os.getenv("SOURCE_DATABASE_URL"), |
| 21 | + ) |
| 22 | + ), |
| 23 | + # Optional. |
| 24 | + ordinal_column="created_at", |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + indexed_messages = data_scope.add_collector() |
| 29 | + with data_scope["messages"].row() as message_row: |
| 30 | + # Use the indexing column for embedding generation |
| 31 | + message_row["embedding"] = message_row["message"].transform( |
| 32 | + cocoindex.functions.SentenceTransformerEmbed( |
| 33 | + model="sentence-transformers/all-MiniLM-L6-v2" |
| 34 | + ) |
| 35 | + ) |
| 36 | + # Collect the data - include key columns and content |
| 37 | + indexed_messages.collect( |
| 38 | + id=message_row["id"], |
| 39 | + author=message_row["author"], |
| 40 | + message=message_row["message"], |
| 41 | + embedding=message_row["embedding"], |
| 42 | + ) |
| 43 | + |
| 44 | + indexed_messages.export( |
| 45 | + "output", |
| 46 | + cocoindex.targets.Postgres(), |
| 47 | + primary_key_fields=["id"], |
| 48 | + vector_indexes=[ |
| 49 | + cocoindex.VectorIndexDef( |
| 50 | + field_name="embedding", |
| 51 | + metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY, |
| 52 | + ) |
| 53 | + ], |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@cocoindex.op.function() |
| 58 | +def calculate_total_value( |
| 59 | + price: float, |
| 60 | + amount: int, |
| 61 | +) -> float: |
| 62 | + return price * amount |
| 63 | + |
| 64 | + |
| 65 | +@cocoindex.op.function() |
| 66 | +def make_full_description( |
| 67 | + category: str, |
| 68 | + name: str, |
| 69 | + description: str, |
| 70 | +) -> str: |
| 71 | + return f"Category: {category}\nName: {name}\n\n{description}" |
| 72 | + |
| 73 | + |
| 74 | +@cocoindex.flow_def(name="PostgresProductIndexing") |
| 75 | +def postgres_product_indexing_flow( |
| 76 | + flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope |
| 77 | +) -> None: |
| 78 | + """ |
| 79 | + Define a flow that reads data from a PostgreSQL table, generates embeddings, |
| 80 | + and stores them in another PostgreSQL table with pgvector. |
| 81 | + """ |
| 82 | + data_scope["products"] = flow_builder.add_source( |
| 83 | + cocoindex.sources.Postgres( |
| 84 | + table_name="source_products", |
| 85 | + # Optional. Use the default CocoIndex database if not specified. |
| 86 | + database=cocoindex.add_transient_auth_entry( |
| 87 | + cocoindex.sources.DatabaseConnectionSpec( |
| 88 | + url=os.getenv("SOURCE_DATABASE_URL"), |
| 89 | + ) |
| 90 | + ), |
| 91 | + # Optional. |
| 92 | + ordinal_column="modified_time", |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + indexed_product = data_scope.add_collector() |
| 97 | + with data_scope["products"].row() as product: |
| 98 | + product["full_description"] = flow_builder.transform( |
| 99 | + make_full_description, |
| 100 | + product["_key"]["product_category"], |
| 101 | + product["_key"]["product_name"], |
| 102 | + product["description"], |
| 103 | + ) |
| 104 | + product["total_value"] = flow_builder.transform( |
| 105 | + calculate_total_value, |
| 106 | + product["price"], |
| 107 | + product["amount"], |
| 108 | + ) |
| 109 | + product["embedding"] = product["full_description"].transform( |
| 110 | + cocoindex.functions.SentenceTransformerEmbed( |
| 111 | + model="sentence-transformers/all-MiniLM-L6-v2" |
| 112 | + ) |
| 113 | + ) |
| 114 | + indexed_product.collect( |
| 115 | + product_category=product["_key"]["product_category"], |
| 116 | + product_name=product["_key"]["product_name"], |
| 117 | + description=product["description"], |
| 118 | + price=product["price"], |
| 119 | + amount=product["amount"], |
| 120 | + total_value=product["total_value"], |
| 121 | + embedding=product["embedding"], |
| 122 | + ) |
| 123 | + |
| 124 | + indexed_product.export( |
| 125 | + "output", |
| 126 | + cocoindex.targets.Postgres(), |
| 127 | + primary_key_fields=["product_category", "product_name"], |
| 128 | + vector_indexes=[ |
| 129 | + cocoindex.VectorIndexDef( |
| 130 | + field_name="embedding", |
| 131 | + metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY, |
| 132 | + ) |
| 133 | + ], |
| 134 | + ) |
0 commit comments