跳转到内容

字符串可迭代对象

字符串可迭代读取器 #

基类:EventBasePydanticReader

字符串可迭代读取器。

给定一个可迭代的字符串序列(例如列表),获取文档列表。

示例

.. 代码块:: python

from llama_index import TreeIndex
from llama_index.readers import StringIterableReader

documents = StringIterableReader().load_data(
    texts=["I went to the store", "I bought an apple"]
)
index = TreeIndex.from_documents(documents)
query_engine = index.as_query_engine()
query_engine.query("what did I buy?")

# response should be something like "You bought an apple."
workflows/handler.py 中的源代码llama_index/readers/string_iterable/base.py
 9
10
11
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
class StringIterableReader(BasePydanticReader):
    """
    String Iterable Reader.

    Gets a list of documents, given an iterable (e.g. list) of strings.

    Example:
        .. code-block:: python

            from llama_index import TreeIndex
            from llama_index.readers import StringIterableReader

            documents = StringIterableReader().load_data(
                texts=["I went to the store", "I bought an apple"]
            )
            index = TreeIndex.from_documents(documents)
            query_engine = index.as_query_engine()
            query_engine.query("what did I buy?")

            # response should be something like "You bought an apple."

    """

    is_remote: bool = False

    @classmethod
    def class_name(cls) -> str:
        return "StringIterableReader"

    def load_data(self, texts: List[str]) -> List[Document]:
        """Load the data."""
        results = []
        for text in texts:
            results.append(Document(text=text))

        return results

load_data #

load_data(texts: List[str]) -> List[文档]

加载数据。

workflows/handler.py 中的源代码llama_index/readers/string_iterable/base.py
38
39
40
41
42
43
44
def load_data(self, texts: List[str]) -> List[Document]:
    """Load the data."""
    results = []
    for text in texts:
        results.append(Document(text=text))

    return results

选项: 成员:- StringIterableReader