跳到主内容

Cassandra

CassandraDatabaseToolSpec #

基础:BaseToolSpec

用于与 Apache Cassandra 数据库交互的基本工具。

源代码位于 llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class CassandraDatabaseToolSpec(BaseToolSpec):
    """Base tool for interacting with an Apache Cassandra database."""

    db: CassandraDatabase = Field(exclude=True)

    spec_functions = [
        "cassandra_db_query",
        "cassandra_db_schema",
        "cassandra_db_select_table_data",
    ]

    def __init__(self, db: CassandraDatabase) -> None:
        """DB session in context."""
        self.db = db

    def cassandra_db_query(self, query: str) -> List[Document]:
        """
        Execute a CQL query and return the results as a list of Documents.

        Args:
            query (str): A CQL query to execute.

        Returns:
            List[Document]: A list of Document objects, each containing data from a row.

        """
        documents = []
        result = self.db.run_no_throw(query, fetch="Cursor")
        for row in result:
            doc_str = ", ".join([str(value) for value in row])
            documents.append(Document(text=doc_str))
        return documents

    def cassandra_db_schema(self, keyspace: str) -> List[Document]:
        """
        Input to this tool is a keyspace name, output is a table description
            of Apache Cassandra tables.
            If the query is not correct, an error message will be returned.
            If an error is returned, report back to the user that the keyspace
            doesn't exist and stop.

        Args:
            keyspace (str): The name of the keyspace for which to return the schema.

        Returns:
            List[Document]: A list of Document objects, each containing a table description.

        """
        return [Document(text=self.db.get_keyspace_tables_str(keyspace))]

    def cassandra_db_select_table_data(
        self, keyspace: str, table: str, predicate: str, limit: int
    ) -> List[Document]:
        """
        Tool for getting data from a table in an Apache Cassandra database.
            Use the WHERE clause to specify the predicate for the query that uses the
            primary key. A blank predicate will return all rows. Avoid this if possible.
            Use the limit to specify the number of rows to return. A blank limit will
            return all rows.

        Args:
            keyspace (str): The name of the keyspace containing the table.
            table (str): The name of the table for which to return data.
            predicate (str): The predicate for the query that uses the primary key.
            limit (int): The maximum number of rows to return.

        Returns:
            List[Document]: A list of Document objects, each containing a row of data.

        """
        return [
            Document(text=self.db.get_table_data(keyspace, table, predicate, limit))
        ]

cassandra_db_query #

cassandra_db_query(query: str) -> List[Document]

执行 CQL 查询并将结果作为 Document 列表返回。

参数

名称 类型 描述 默认值
query str

要执行的 CQL 查询。

必需

返回值

类型 描述
列表[Document]

List[Document]:Document 对象列表,每个对象包含一行数据。

源代码位于 llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def cassandra_db_query(self, query: str) -> List[Document]:
    """
    Execute a CQL query and return the results as a list of Documents.

    Args:
        query (str): A CQL query to execute.

    Returns:
        List[Document]: A list of Document objects, each containing data from a row.

    """
    documents = []
    result = self.db.run_no_throw(query, fetch="Cursor")
    for row in result:
        doc_str = ", ".join([str(value) for value in row])
        documents.append(Document(text=doc_str))
    return documents

cassandra_db_schema #

cassandra_db_schema(keyspace: str) -> List[Document]

此工具的输入是键空间名称,输出是 Apache Cassandra 表的表描述。如果查询不正确,将返回错误消息。如果返回错误,则向用户报告键空间不存在并停止。

参数

名称 类型 描述 默认值
keyspace str

要返回模式的键空间名称。

必需

返回值

类型 描述
列表[Document]

List[Document]:Document 对象列表,每个对象包含一个表描述。

源代码位于 llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def cassandra_db_schema(self, keyspace: str) -> List[Document]:
    """
    Input to this tool is a keyspace name, output is a table description
        of Apache Cassandra tables.
        If the query is not correct, an error message will be returned.
        If an error is returned, report back to the user that the keyspace
        doesn't exist and stop.

    Args:
        keyspace (str): The name of the keyspace for which to return the schema.

    Returns:
        List[Document]: A list of Document objects, each containing a table description.

    """
    return [Document(text=self.db.get_keyspace_tables_str(keyspace))]

cassandra_db_select_table_data #

cassandra_db_select_table_data(keyspace: str, table: str, predicate: str, limit: int) -> List[Document]

用于从 Apache Cassandra 数据库表中获取数据的工具。使用 WHERE 子句指定使用主键的查询的谓词。空白谓词将返回所有行。如果可能,请避免这样做。使用 limit 指定要返回的行数。空白 limit 将返回所有行。

参数

名称 类型 描述 默认值
keyspace str

包含表的键空间名称。

必需
table str

要返回数据的表名称。

必需
predicate str

使用主键的查询的谓词。

必需
limit int

要返回的最大行数。

必需

返回值

类型 描述
列表[Document]

List[Document]:Document 对象列表,每个对象包含一行数据。

源代码位于 llama-index-integrations/tools/llama-index-tools-cassandra/llama_index/tools/cassandra/base.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def cassandra_db_select_table_data(
    self, keyspace: str, table: str, predicate: str, limit: int
) -> List[Document]:
    """
    Tool for getting data from a table in an Apache Cassandra database.
        Use the WHERE clause to specify the predicate for the query that uses the
        primary key. A blank predicate will return all rows. Avoid this if possible.
        Use the limit to specify the number of rows to return. A blank limit will
        return all rows.

    Args:
        keyspace (str): The name of the keyspace containing the table.
        table (str): The name of the table for which to return data.
        predicate (str): The predicate for the query that uses the primary key.
        limit (int): The maximum number of rows to return.

    Returns:
        List[Document]: A list of Document objects, each containing a row of data.

    """
    return [
        Document(text=self.db.get_table_data(keyspace, table, predicate, limit))
    ]