跳过内容

可迭代字符串

StringIterableReader #

基类:BasePydanticReader

字符串可迭代读取器。

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

示例

.. 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."
源代码位于 llama-index-integrations/readers/llama-index-readers-string-iterable/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[Document]

加载数据。

源代码位于 llama-index-integrations/readers/llama-index-readers-string-iterable/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