跳转到内容

CouchDB

SimpleCouchDB读取器 #

基类:EventBaseReader

简单的CouchDB读取器。

将每个CouchDB文档连接成LlamaIndex使用的Document。

参数:

名称 类型 描述 默认
couchdb_url str

CouchDB 完整 URL。

None
max_docs int

最大加载文档数量。

1000
workflows/handler.py 中的源代码llama_index/readers/couchdb/base.py
12
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
86
87
88
class SimpleCouchDBReader(BaseReader):
    """
    Simple CouchDB reader.

    Concatenates each CouchDB doc into Document used by LlamaIndex.

    Args:
        couchdb_url (str): CouchDB Full URL.
        max_docs (int): Maximum number of documents to load.

    """

    def __init__(
        self,
        user: str,
        pwd: str,
        host: str,
        port: int,
        couchdb_url: Optional[Dict] = None,
        max_docs: int = 1000,
    ) -> None:
        """Initialize with parameters."""
        if couchdb_url is not None:
            self.client = couchdb3.Server(couchdb_url)
        else:
            self.client = couchdb3.Server(f"http://{user}:{pwd}@{host}:{port}")
        self.max_docs = max_docs

    def load_data(self, db_name: str, query: Optional[str] = None) -> List[Document]:
        """
        Load data from the input directory.

        Args:
            db_name (str): name of the database.
            query (Optional[str]): query to filter documents.
                Defaults to None

        Returns:
            List[Document]: A list of documents.

        """
        documents = []
        db = self.client.get(db_name)
        if query is None:
            # if no query is specified, return all docs in database
            logging.debug("showing all docs")
            results = db.view("_all_docs", include_docs=True)
        else:
            logging.debug("executing query")
            results = db.find(query)

        if not isinstance(results, dict):
            logging.debug(results.rows)
        else:
            logging.debug(results)

        # check if more than one result
        if (
            not isinstance(results, dict)
            and hasattr(results, "rows")
            and results.rows is not None
        ):
            for row in results.rows:
                # check that the id field exists
                if "id" not in row:
                    raise ValueError("`id` field not found in CouchDB document.")
                documents.append(Document(text=json.dumps(row.doc)))
        else:
            # only one result
            if results.get("docs") is not None:
                for item in results.get("docs"):
                    # check that the _id field exists
                    if "_id" not in item:
                        raise ValueError("`_id` field not found in CouchDB document.")
                    documents.append(Document(text=json.dumps(item)))

        return documents

load_data #

load_data(db_name: str, query: Optional[str] = None) -> List[文档]

从输入目录加载数据。

参数:

名称 类型 描述 默认
db_name str

数据库的名称。

required
query Optional[str]

用于筛选文档的查询条件。 默认为 None

None

返回:

类型 描述
List[文档]

List[Document]: 文档列表。

workflows/handler.py 中的源代码llama_index/readers/couchdb/base.py
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
86
87
88
def load_data(self, db_name: str, query: Optional[str] = None) -> List[Document]:
    """
    Load data from the input directory.

    Args:
        db_name (str): name of the database.
        query (Optional[str]): query to filter documents.
            Defaults to None

    Returns:
        List[Document]: A list of documents.

    """
    documents = []
    db = self.client.get(db_name)
    if query is None:
        # if no query is specified, return all docs in database
        logging.debug("showing all docs")
        results = db.view("_all_docs", include_docs=True)
    else:
        logging.debug("executing query")
        results = db.find(query)

    if not isinstance(results, dict):
        logging.debug(results.rows)
    else:
        logging.debug(results)

    # check if more than one result
    if (
        not isinstance(results, dict)
        and hasattr(results, "rows")
        and results.rows is not None
    ):
        for row in results.rows:
            # check that the id field exists
            if "id" not in row:
                raise ValueError("`id` field not found in CouchDB document.")
            documents.append(Document(text=json.dumps(row.doc)))
    else:
        # only one result
        if results.get("docs") is not None:
            for item in results.get("docs"):
                # check that the _id field exists
                if "_id" not in item:
                    raise ValueError("`_id` field not found in CouchDB document.")
                documents.append(Document(text=json.dumps(item)))

    return documents

选项: 成员:- SimpleCouchDBReader