|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | +"""Methods for converting Airbyte records into documents.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +import yaml |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +from airbyte.documents import Document |
| 11 | + |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from collections.abc import Iterable |
| 15 | + |
| 16 | + |
| 17 | +def _to_title_case(name: str, /) -> str: |
| 18 | + """Convert a string to title case. |
| 19 | +
|
| 20 | + Unlike Python's built-in `str.title` method, this function doesn't lowercase the rest of the |
| 21 | + string. This is useful for converting "snake_case" to "Title Case" without negatively affecting |
| 22 | + strings that are already in title case or camel case. |
| 23 | + """ |
| 24 | + return " ".join(word[0].upper() + word[1:] for word in name.split("_")) |
| 25 | + |
| 26 | + |
| 27 | +class CustomRenderingInstructions(BaseModel): |
| 28 | + """Instructions for rendering a stream's records as documents.""" |
| 29 | + |
| 30 | + title_property: str | None |
| 31 | + content_properties: list[str] |
| 32 | + frontmatter_properties: list[str] |
| 33 | + metadata_properties: list[str] |
| 34 | + |
| 35 | + |
| 36 | +class DocumentRenderer(BaseModel): |
| 37 | + """Instructions for rendering a stream's records as documents.""" |
| 38 | + |
| 39 | + title_property: str | None |
| 40 | + content_properties: list[str] | None |
| 41 | + metadata_properties: list[str] | None |
| 42 | + render_metadata: bool = False |
| 43 | + |
| 44 | + # TODO: Add primary key and cursor key support: |
| 45 | + # primary_key_properties: list[str] |
| 46 | + # cursor_property: str | None |
| 47 | + |
| 48 | + def render_document(self, record: dict[str, Any]) -> Document: |
| 49 | + """Render a record as a document. |
| 50 | +
|
| 51 | + The document will be rendered as a markdown document, with content, frontmatter, and an |
| 52 | + optional title. If there are multiple properties to render as content, they will be rendered |
| 53 | + beneath H2 section headers. If there is only one property to render as content, it will be |
| 54 | + rendered without a section header. If a title property is specified, it will be rendered as |
| 55 | + an H1 header at the top of the document. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + A tuple of (content: str, metadata: dict). |
| 59 | + """ |
| 60 | + content = "" |
| 61 | + if not self.metadata_properties: |
| 62 | + self.metadata_properties = [ |
| 63 | + key |
| 64 | + for key in record |
| 65 | + if key not in (self.content_properties or []) and key != self.title_property |
| 66 | + ] |
| 67 | + if self.title_property: |
| 68 | + content += f"# {record[self.title_property]}\n\n" |
| 69 | + if self.render_metadata or not self.content_properties: |
| 70 | + content += "```yaml\n" |
| 71 | + content += yaml.dump({key: record[key] for key in self.metadata_properties}) |
| 72 | + content += "```\n" |
| 73 | + |
| 74 | + # TODO: Add support for primary key and doc ID generation: |
| 75 | + # doc_id: str = ( |
| 76 | + # "-".join(str(record[key]) for key in self.primary_key_properties) |
| 77 | + # if self.primary_key_properties |
| 78 | + # else str(hash(record)) |
| 79 | + # ) |
| 80 | + |
| 81 | + if not self.content_properties: |
| 82 | + pass |
| 83 | + elif len(self.content_properties) == 1: |
| 84 | + # Only one property to render as content; no need for section headers. |
| 85 | + content += str(record[self.content_properties[0]]) |
| 86 | + else: |
| 87 | + # Multiple properties to render as content; use H2 section headers. |
| 88 | + content += "\n".join( |
| 89 | + f"## {_to_title_case(key)}\n\n{record[key]}\n\n" for key in self.content_properties |
| 90 | + ) |
| 91 | + |
| 92 | + return Document( |
| 93 | + # id=doc_id, # TODD: Add support for primary key and doc ID generation. |
| 94 | + content=content, |
| 95 | + metadata={key: record[key] for key in self.metadata_properties}, |
| 96 | + ) |
| 97 | + |
| 98 | + def render_documents(self, records: Iterable[dict[str, Any]]) -> Iterable[Document]: |
| 99 | + """Render an iterable of records as documents.""" |
| 100 | + yield from (self.render_document(record=record) for record in records) |
0 commit comments