Skip to content

customer_database_search

A simple CUI application to visualize and query a customer database using the textual package.

Customer dataclass

Source code in examples/customer_database_search.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@llm_strategy(base_llm)
@dataclass
class Customer:
    key: str
    first_name: str
    last_name: str
    birthdate: str
    address: str

    @property
    def age(self: "Customer") -> int:
        """Return the current age of the customer.

        This is a computed property based on `birthdate` and the current year (2022).
        """

        raise NotImplementedError()

age: int property

Return the current age of the customer.

This is a computed property based on birthdate and the current year (2022).

CustomerDatabase dataclass

Source code in examples/customer_database_search.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@dataclass
class CustomerDatabase:
    customers: list[Customer]

    def find_customer_key(self: "CustomerDatabase", query: str) -> list[str]:
        """Find the keys of the customers that match a natural language query best (sorted by closeness to the match).

        We support semantic queries instead of SQL, so we can search for things like
        "the customer that was born in 1990".

        Args:
            query: Natural language query

        Returns:
            The index of the best matching customer in the database.
        """
        raise NotImplementedError()

    def load(self: "CustomerDatabase"):
        """Load the customer database from a file."""
        raise NotImplementedError()

    def store(self: "CustomerDatabase"):
        """Store the customer database to a file."""
        raise NotImplementedError()

find_customer_key(query)

Find the keys of the customers that match a natural language query best (sorted by closeness to the match).

We support semantic queries instead of SQL, so we can search for things like "the customer that was born in 1990".

Parameters:

Name Type Description Default
query str

Natural language query

required

Returns:

Type Description
list[str]

The index of the best matching customer in the database.

Source code in examples/customer_database_search.py
42
43
44
45
46
47
48
49
50
51
52
53
54
def find_customer_key(self: "CustomerDatabase", query: str) -> list[str]:
    """Find the keys of the customers that match a natural language query best (sorted by closeness to the match).

    We support semantic queries instead of SQL, so we can search for things like
    "the customer that was born in 1990".

    Args:
        query: Natural language query

    Returns:
        The index of the best matching customer in the database.
    """
    raise NotImplementedError()

load()

Load the customer database from a file.

Source code in examples/customer_database_search.py
56
57
58
def load(self: "CustomerDatabase"):
    """Load the customer database from a file."""
    raise NotImplementedError()

store()

Store the customer database to a file.

Source code in examples/customer_database_search.py
60
61
62
def store(self: "CustomerDatabase"):
    """Store the customer database to a file."""
    raise NotImplementedError()

CustomerDatabaseApp

Bases: App

A simple textual application to visualize and query a customer database.

We show all the customers in a table and allow the user to query the database using natural language in a search box at the bottom of the screen.

Source code in examples/customer_database_search.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class CustomerDatabaseApp(App):
    """A simple textual application to visualize and query a customer database.

    We show all the customers in a table and allow the user to query the database using natural language
    in a search box at the bottom of the screen.
    """

    PRIORITY_BINDINGS = False
    BINDINGS = [("q", "quit", "Quit the application"), ("s", "screenshot", "Take a screenshot")]

    database: CustomerDatabase = MockCustomerDatabase([])

    data_table = DataTable(id="customer_table")
    search_box = Input(id="search_box", placeholder="Search for a customer (use any kind of query")
    footer_bar = Horizontal(search_box)

    def on_mount(self) -> None:
        self.database.load()

        self.data_table.add_columns("First Name", "Last Name", "Birthdate", "Address", "Age")
        self.search("")

    def compose(self) -> ComposeResult:
        self.footer_bar.styles.dock = "bottom"
        self.footer_bar.styles.width = "100%"
        self.footer_bar.styles.height = 4

        self.data_table.styles.height = "auto"
        self.data_table.styles.width = "100%"
        self.screen.styles.height = "100%"
        self.search_box.styles.width = "100%"

        yield Header()
        yield self.footer_bar
        yield Footer()

        yield self.data_table

    def search(self, query: str):
        """Search the customer database using a natural language query."""
        self.data_table.clear()
        if not query:
            for customer in self.database.customers:
                self.data_table.add_row(
                    # customer.key,
                    customer.first_name,
                    customer.last_name,
                    customer.birthdate,
                    customer.address,
                    str(customer.age),
                )
        else:
            keys = self.database.find_customer_key(query)
            for key in keys:
                customers_for_key = [customer for customer in self.database.customers if customer.key == key]
                assert len(customers_for_key) == 1
                customer = customers_for_key[0]
                self.data_table.add_row(
                    # customer.key,
                    customer.first_name,
                    customer.last_name,
                    customer.birthdate,
                    customer.address,
                    str(customer.age),
                )

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button is self.exit_button:
            self.exit()

    def on_input_submitted(self, event: Input.Submitted) -> None:
        if event.input is self.search_box:
            self.search(event.value)

search(query)

Search the customer database using a natural language query.

Source code in examples/customer_database_search.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def search(self, query: str):
    """Search the customer database using a natural language query."""
    self.data_table.clear()
    if not query:
        for customer in self.database.customers:
            self.data_table.add_row(
                # customer.key,
                customer.first_name,
                customer.last_name,
                customer.birthdate,
                customer.address,
                str(customer.age),
            )
    else:
        keys = self.database.find_customer_key(query)
        for key in keys:
            customers_for_key = [customer for customer in self.database.customers if customer.key == key]
            assert len(customers_for_key) == 1
            customer = customers_for_key[0]
            self.data_table.add_row(
                # customer.key,
                customer.first_name,
                customer.last_name,
                customer.birthdate,
                customer.address,
                str(customer.age),
            )

MockCustomerDatabase dataclass

Bases: CustomerDatabase

Source code in examples/customer_database_search.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@llm_strategy(base_llm)
@dataclass
class MockCustomerDatabase(CustomerDatabase):
    def load(self):
        self.customers = self.create_mock_customers(10)

    def store(self):
        pass

    @staticmethod
    def create_mock_customers(num_customers: int = 1) -> list[Customer]:
        """
        Create mock customers with believable data (our customers are world citizens).
        """
        raise NotImplementedError()

create_mock_customers(num_customers=1) staticmethod

Create mock customers with believable data (our customers are world citizens).

Source code in examples/customer_database_search.py
74
75
76
77
78
79
@staticmethod
def create_mock_customers(num_customers: int = 1) -> list[Customer]:
    """
    Create mock customers with believable data (our customers are world citizens).
    """
    raise NotImplementedError()