2023-02-03 00:05:28 -08:00
|
|
|
"""Simple reader that turns an iterable of strings into a list of Documents."""
|
|
|
|
from typing import List
|
|
|
|
|
2023-02-20 21:46:58 -08:00
|
|
|
from llama_index.readers.base import BaseReader
|
|
|
|
from llama_index.readers.schema.base import Document
|
2023-02-03 00:05:28 -08:00
|
|
|
|
|
|
|
|
|
|
|
class StringIterableReader(BaseReader):
|
|
|
|
"""String Iterable Reader.
|
|
|
|
|
|
|
|
Gets a list of documents, given an iterable (e.g. list) of strings.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
.. code-block:: python
|
|
|
|
|
2023-02-20 21:46:58 -08:00
|
|
|
from llama_index import StringIterableReader, GPTTreeIndex
|
2023-02-03 00:05:28 -08:00
|
|
|
|
|
|
|
documents = StringIterableReader().load_data(
|
|
|
|
texts=["I went to the store", "I bought an apple"])
|
|
|
|
index = GPTTreeIndex(documents)
|
|
|
|
index.query("what did I buy?")
|
|
|
|
|
|
|
|
# response should be something like "You bought an apple."
|
|
|
|
"""
|
|
|
|
|
|
|
|
def load_data(self, texts: List[str]) -> List[Document]:
|
|
|
|
"""Load the data."""
|
|
|
|
results = []
|
|
|
|
for text in texts:
|
|
|
|
results.append(Document(text))
|
|
|
|
|
|
|
|
return results
|