跳转到内容

Yugabytedb

YugabyteDB聊天存储 #

基类:EventBaseChatStore

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
class YugabyteDBChatStore(BaseChatStore):
    table_name: Optional[str] = Field(
        default="chatstore", description="YugabyteDB table name."
    )
    schema_name: Optional[str] = Field(
        default="public", description="YugabyteDB schema name."
    )

    _table_class: Optional[Any] = PrivateAttr()
    _session: Optional[sessionmaker] = PrivateAttr()

    def __init__(
        self,
        session: sessionmaker,
        table_name: str,
        schema_name: str = "public",
        use_jsonb: bool = False,
    ):
        super().__init__(
            table_name=table_name.lower(),
            schema_name=schema_name.lower(),
        )

        # sqlalchemy model
        base = declarative_base()
        self._table_class = get_data_model(
            base,
            table_name,
            schema_name,
            use_jsonb=use_jsonb,
        )
        self._session = session
        self._initialize(base)

    @classmethod
    def from_params(
        cls,
        host: Optional[str] = None,
        port: Optional[str] = None,
        database: Optional[str] = None,
        user: Optional[str] = None,
        password: Optional[str] = None,
        load_balance: Optional[bool] = False,
        topology_keys: Optional[str] = None,
        yb_servers_refresh_interval: Optional[int] = 300,
        fallback_to_topology_keys_only: Optional[bool] = False,
        failed_host_ttl_seconds: Optional[int] = 5,
        table_name: str = "chatstore",
        schema_name: str = "public",
        connection_string: Optional[str] = None,
        debug: bool = False,
        use_jsonb: bool = False,
    ) -> "YugabyteDBChatStore":
        """
        Return connection string from database parameters.

        Args:
            host (str): YugabyteDB host.
            port (str): YugabyteDB port.
            database (str): YugabyteDB database name.
            user (str): YugabyteDB user.
            password (str): YugabyteDB password.
            load_balance (bool, optional): Enables uniform load balancing. Defaults to False.
            topology_keys (str, optional): Enables topology-aware load balancing.
                Specify comma-separated geo-locations in the form of cloud.region.zone:priority.
                Ignored if load_balance is false. Defaults to None.
            yb_servers_refresh_interval (int, optional): The interval in seconds to refresh the servers list;
                ignored if load_balance is false. Defaults to 300.
            fallback_to_topology_keys_only (bool, optional): If set to true and topology_keys are specified,
                the driver only tries to connect to nodes specified in topology_keys
                Defaults to False.
            failed_host_ttl_seconds (int, optional): Time, in seconds, to wait before trying to connect to failed nodes.
                Defaults to 5.
            connection_string (Union[str, sqlalchemy.engine.URL]): Connection string to yugabytedb db.
            table_name (str): Table name.
            schema_name (str): Schema name.
            debug (bool, optional): Debug mode. Defaults to False.
            use_jsonb (bool, optional): Use JSONB instead of JSON. Defaults to False.

        """
        from urllib.parse import urlencode

        query_params = {"load_balance": str(load_balance)}

        if topology_keys is not None:
            query_params["topology_keys"] = topology_keys
        if yb_servers_refresh_interval is not None:
            query_params["yb_servers_refresh_interval"] = yb_servers_refresh_interval
        if fallback_to_topology_keys_only:
            query_params["fallback_to_topology_keys_only"] = (
                fallback_to_topology_keys_only
            )
        if failed_host_ttl_seconds is not None:
            query_params["failed_host_ttl_seconds"] = failed_host_ttl_seconds

        query_str = urlencode(query_params)

        conn_str = (
            connection_string
            or f"yugabytedb+psycopg2://{user}:{password}@{host}:{port}/{database}?{query_str}"
        )

        session = cls._connect(conn_str, debug)
        return cls(
            session=session,
            table_name=table_name,
            schema_name=schema_name,
            use_jsonb=use_jsonb,
        )

    @classmethod
    def from_uri(
        cls,
        uri: str,
        table_name: str = "chatstore",
        schema_name: str = "public",
        debug: bool = False,
        use_jsonb: bool = False,
    ) -> "YugabyteDBChatStore":
        """Return connection string from database parameters."""
        params = params_from_uri(uri)
        return cls.from_params(
            **params,
            table_name=table_name,
            schema_name=schema_name,
            debug=debug,
            use_jsonb=use_jsonb,
        )

    @classmethod
    def _connect(
        cls, connection_string: str, debug: bool
    ) -> tuple[sessionmaker, sessionmaker]:
        _engine = create_engine(connection_string, echo=debug)

        return sessionmaker(_engine)

    def _create_schema_if_not_exists(self) -> None:
        with self._session() as session, session.begin():
            # Check if the specified schema exists with "CREATE" statement
            check_schema_statement = text(
                f"SELECT schema_name FROM information_schema.schemata WHERE schema_name = '{self.schema_name}'"
            )
            result = session.execute(check_schema_statement).fetchone()

            # If the schema does not exist, then create it
            if not result:
                create_schema_statement = text(
                    f"CREATE SCHEMA IF NOT EXISTS {self.schema_name}"
                )
                session.execute(create_schema_statement)

            session.commit()

    def _create_tables_if_not_exists(self, base) -> None:
        with self._session() as session, session.begin():
            base.metadata.create_all(session.connection())

    def _initialize(self, base) -> None:
        self._create_schema_if_not_exists()
        self._create_tables_if_not_exists(base)

    def set_messages(self, key: str, messages: list[ChatMessage]) -> None:
        """Set messages for a key."""
        with self._session() as session:
            stmt = (
                insert(self._table_class)
                .values(
                    key=bindparam("key"), value=cast(bindparam("value"), ARRAY(JSONB))
                )
                .on_conflict_do_update(
                    index_elements=["key"],
                    set_={"value": cast(bindparam("value"), ARRAY(JSONB))},
                )
            )

            params = {
                "key": key,
                "value": [message.model_dump_json() for message in messages],
            }

            # Execute the bulk upsert
            session.execute(stmt, params)
            session.commit()

    def get_messages(self, key: str) -> list[ChatMessage]:
        """Get messages for a key."""
        with self._session() as session:
            result = session.execute(select(self._table_class).filter_by(key=key))
            result = result.scalars().first()
            if result:
                return [
                    ChatMessage.model_validate(removed_message)
                    for removed_message in result.value
                ]
            return []

    def add_message(self, key: str, message: ChatMessage) -> None:
        """Add a message for a key."""
        with self._session() as session:
            stmt = (
                insert(self._table_class)
                .values(
                    key=bindparam("key"), value=cast(bindparam("value"), ARRAY(JSONB))
                )
                .on_conflict_do_update(
                    index_elements=["key"],
                    set_={"value": cast(bindparam("value"), ARRAY(JSONB))},
                )
            )
            params = {"key": key, "value": [message.model_dump_json()]}
            session.execute(stmt, params)
            session.commit()

    def delete_messages(self, key: str) -> Optional[list[ChatMessage]]:
        """Delete messages for a key."""
        with self._session() as session:
            session.execute(delete(self._table_class).filter_by(key=key))
            session.commit()
        return None

    def delete_message(self, key: str, idx: int) -> Optional[ChatMessage]:
        """Delete specific message for a key."""
        with self._session() as session:
            # First, retrieve the current list of messages
            stmt = select(self._table_class.value).where(self._table_class.key == key)
            result = session.execute(stmt).scalar_one_or_none()

            if result is None or idx < 0 or idx >= len(result):
                # If the key doesn't exist or the index is out of bounds
                return None

            # Remove the message at the given index
            removed_message = result[idx]

            stmt = text(
                f"""
                UPDATE {self._table_class.__tablename__}
                SET value = array_cat(
                               {self._table_class.__tablename__}.value[: :idx],
                               {self._table_class.__tablename__}.value[:idx+2:]
                           )
                WHERE key = :key;
                """
            )

            params = {"key": key, "idx": idx}
            session.execute(stmt, params)
            session.commit()

            return ChatMessage.model_validate(removed_message)

    def delete_last_message(self, key: str) -> Optional[ChatMessage]:
        """Delete last message for a key."""
        with self._session() as session:
            # First, retrieve the current list of messages
            stmt = select(self._table_class.value).where(self._table_class.key == key)
            result = session.execute(stmt).scalar_one_or_none()

            if result is None or len(result) == 0:
                # If the key doesn't exist or the array is empty
                return None

            # Remove the message at the given index
            removed_message = result[-1]

            stmt = text(
                f"""
                UPDATE {self._table_class.__tablename__}
                SET value = value[1:array_length(value, 1) - 1]
                WHERE key = :key;
                """
            )
            params = {"key": key}
            session.execute(stmt, params)
            session.commit()

            return ChatMessage.model_validate(removed_message)

    def get_keys(self) -> list[str]:
        """Get all keys."""
        with self._session() as session:
            stmt = select(self._table_class.key)

            return session.execute(stmt).scalars().all()

from_params classmethod #

from_params(host: Optional[str] = None, port: Optional[str] = None, database: Optional[str] = None, user: Optional[str] = None, password: Optional[str] = None, load_balance: Optional[bool] = False, topology_keys: Optional[str] = None, yb_servers_refresh_interval: Optional[int] = 300, fallback_to_topology_keys_only: Optional[bool] = False, failed_host_ttl_seconds: Optional[int] = 5, table_name: str = 'chatstore', schema_name: str = 'public', connection_string: Optional[str] = None, debug: bool = False, use_jsonb: bool = False) -> YugabyteDBChatStore

根据数据库参数返回连接字符串。

参数:

名称 类型 描述 默认
host str

YugabyteDB 主机。

None
port str

YugabyteDB 端口。

None
database str

YugabyteDB 数据库名称。

None
user str

YugabyteDB 用户。

None
password str

YugabyteDB 密码。

None
load_balance bool

启用统一负载均衡。默认为 False。

False
topology_keys str

启用拓扑感知负载均衡。 以 cloud.region.zone:priority 格式指定逗号分隔的地理位置。 当 load_balance 为 false 时忽略此项。默认为 None。

None
yb_servers_refresh_interval int

刷新服务器列表的时间间隔(以秒为单位);如果负载均衡为 false 则忽略此项。默认为 300。

300
fallback_to_topology_keys_only bool

如果设置为 true 且指定了 topology_keys, 驱动程序仅尝试连接到 topology_keys 中指定的节点 默认为 False。

False
failed_host_ttl_seconds int

在尝试连接到故障节点之前等待的时间,单位为秒。 默认为5。

5
connection_string Union[str, URL]

连接字符串到 yugabytedb 数据库。

None
table_name str

表名。

'chatstore'
schema_name str

模式名称。

'public'
debug bool

调试模式。默认为 False。

False
use_jsonb bool

使用 JSONB 替代 JSON。默认为 False。

False
workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
@classmethod
def from_params(
    cls,
    host: Optional[str] = None,
    port: Optional[str] = None,
    database: Optional[str] = None,
    user: Optional[str] = None,
    password: Optional[str] = None,
    load_balance: Optional[bool] = False,
    topology_keys: Optional[str] = None,
    yb_servers_refresh_interval: Optional[int] = 300,
    fallback_to_topology_keys_only: Optional[bool] = False,
    failed_host_ttl_seconds: Optional[int] = 5,
    table_name: str = "chatstore",
    schema_name: str = "public",
    connection_string: Optional[str] = None,
    debug: bool = False,
    use_jsonb: bool = False,
) -> "YugabyteDBChatStore":
    """
    Return connection string from database parameters.

    Args:
        host (str): YugabyteDB host.
        port (str): YugabyteDB port.
        database (str): YugabyteDB database name.
        user (str): YugabyteDB user.
        password (str): YugabyteDB password.
        load_balance (bool, optional): Enables uniform load balancing. Defaults to False.
        topology_keys (str, optional): Enables topology-aware load balancing.
            Specify comma-separated geo-locations in the form of cloud.region.zone:priority.
            Ignored if load_balance is false. Defaults to None.
        yb_servers_refresh_interval (int, optional): The interval in seconds to refresh the servers list;
            ignored if load_balance is false. Defaults to 300.
        fallback_to_topology_keys_only (bool, optional): If set to true and topology_keys are specified,
            the driver only tries to connect to nodes specified in topology_keys
            Defaults to False.
        failed_host_ttl_seconds (int, optional): Time, in seconds, to wait before trying to connect to failed nodes.
            Defaults to 5.
        connection_string (Union[str, sqlalchemy.engine.URL]): Connection string to yugabytedb db.
        table_name (str): Table name.
        schema_name (str): Schema name.
        debug (bool, optional): Debug mode. Defaults to False.
        use_jsonb (bool, optional): Use JSONB instead of JSON. Defaults to False.

    """
    from urllib.parse import urlencode

    query_params = {"load_balance": str(load_balance)}

    if topology_keys is not None:
        query_params["topology_keys"] = topology_keys
    if yb_servers_refresh_interval is not None:
        query_params["yb_servers_refresh_interval"] = yb_servers_refresh_interval
    if fallback_to_topology_keys_only:
        query_params["fallback_to_topology_keys_only"] = (
            fallback_to_topology_keys_only
        )
    if failed_host_ttl_seconds is not None:
        query_params["failed_host_ttl_seconds"] = failed_host_ttl_seconds

    query_str = urlencode(query_params)

    conn_str = (
        connection_string
        or f"yugabytedb+psycopg2://{user}:{password}@{host}:{port}/{database}?{query_str}"
    )

    session = cls._connect(conn_str, debug)
    return cls(
        session=session,
        table_name=table_name,
        schema_name=schema_name,
        use_jsonb=use_jsonb,
    )

from_uri classmethod #

from_uri(uri: str, table_name: str = 'chatstore', schema_name: str = 'public', debug: bool = False, use_jsonb: bool = False) -> YugabyteDBChatStore

根据数据库参数返回连接字符串。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
@classmethod
def from_uri(
    cls,
    uri: str,
    table_name: str = "chatstore",
    schema_name: str = "public",
    debug: bool = False,
    use_jsonb: bool = False,
) -> "YugabyteDBChatStore":
    """Return connection string from database parameters."""
    params = params_from_uri(uri)
    return cls.from_params(
        **params,
        table_name=table_name,
        schema_name=schema_name,
        debug=debug,
        use_jsonb=use_jsonb,
    )

set_messages #

set_messages(key: str, messages: list[ChatMessage]) -> None

为某个键设置消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def set_messages(self, key: str, messages: list[ChatMessage]) -> None:
    """Set messages for a key."""
    with self._session() as session:
        stmt = (
            insert(self._table_class)
            .values(
                key=bindparam("key"), value=cast(bindparam("value"), ARRAY(JSONB))
            )
            .on_conflict_do_update(
                index_elements=["key"],
                set_={"value": cast(bindparam("value"), ARRAY(JSONB))},
            )
        )

        params = {
            "key": key,
            "value": [message.model_dump_json() for message in messages],
        }

        # Execute the bulk upsert
        session.execute(stmt, params)
        session.commit()

get_messages #

get_messages(key: str) -> list[ChatMessage]

获取某个键的消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
255
256
257
258
259
260
261
262
263
264
265
def get_messages(self, key: str) -> list[ChatMessage]:
    """Get messages for a key."""
    with self._session() as session:
        result = session.execute(select(self._table_class).filter_by(key=key))
        result = result.scalars().first()
        if result:
            return [
                ChatMessage.model_validate(removed_message)
                for removed_message in result.value
            ]
        return []

add_message #

add_message(key: str, message: ChatMessage) -> None

为某个键添加一条消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
def add_message(self, key: str, message: ChatMessage) -> None:
    """Add a message for a key."""
    with self._session() as session:
        stmt = (
            insert(self._table_class)
            .values(
                key=bindparam("key"), value=cast(bindparam("value"), ARRAY(JSONB))
            )
            .on_conflict_do_update(
                index_elements=["key"],
                set_={"value": cast(bindparam("value"), ARRAY(JSONB))},
            )
        )
        params = {"key": key, "value": [message.model_dump_json()]}
        session.execute(stmt, params)
        session.commit()

delete_messages #

delete_messages(key: str) -> Optional[list[ChatMessage]]

删除某个键的消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
284
285
286
287
288
289
def delete_messages(self, key: str) -> Optional[list[ChatMessage]]:
    """Delete messages for a key."""
    with self._session() as session:
        session.execute(delete(self._table_class).filter_by(key=key))
        session.commit()
    return None

delete_message #

delete_message(key: str, idx: int) -> Optional[ChatMessage]

删除特定键对应的消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
def delete_message(self, key: str, idx: int) -> Optional[ChatMessage]:
    """Delete specific message for a key."""
    with self._session() as session:
        # First, retrieve the current list of messages
        stmt = select(self._table_class.value).where(self._table_class.key == key)
        result = session.execute(stmt).scalar_one_or_none()

        if result is None or idx < 0 or idx >= len(result):
            # If the key doesn't exist or the index is out of bounds
            return None

        # Remove the message at the given index
        removed_message = result[idx]

        stmt = text(
            f"""
            UPDATE {self._table_class.__tablename__}
            SET value = array_cat(
                           {self._table_class.__tablename__}.value[: :idx],
                           {self._table_class.__tablename__}.value[:idx+2:]
                       )
            WHERE key = :key;
            """
        )

        params = {"key": key, "idx": idx}
        session.execute(stmt, params)
        session.commit()

        return ChatMessage.model_validate(removed_message)

delete_last_message #

delete_last_message(key: str) -> Optional[ChatMessage]

删除某个键的最后一条消息。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
def delete_last_message(self, key: str) -> Optional[ChatMessage]:
    """Delete last message for a key."""
    with self._session() as session:
        # First, retrieve the current list of messages
        stmt = select(self._table_class.value).where(self._table_class.key == key)
        result = session.execute(stmt).scalar_one_or_none()

        if result is None or len(result) == 0:
            # If the key doesn't exist or the array is empty
            return None

        # Remove the message at the given index
        removed_message = result[-1]

        stmt = text(
            f"""
            UPDATE {self._table_class.__tablename__}
            SET value = value[1:array_length(value, 1) - 1]
            WHERE key = :key;
            """
        )
        params = {"key": key}
        session.execute(stmt, params)
        session.commit()

        return ChatMessage.model_validate(removed_message)

get_keys #

get_keys() -> list[str]

获取所有键。

workflows/handler.py 中的源代码llama_index/storage/chat_store/yugabytedb/base.py
349
350
351
352
353
354
def get_keys(self) -> list[str]:
    """Get all keys."""
    with self._session() as session:
        stmt = select(self._table_class.key)

        return session.execute(stmt).scalars().all()

选项: 成员:- YugabyteDBChatStore