LukasKratzel
LukasKratzel

Reputation: 1

How can I use a python string as input for my langchain loader?

I want to have similarity search for a bunch of knowledge bases of mine and want to achieve that with langchain. As I need to retrieve the knowledge base from a database, I have all the content inside a python string, which I want to use as input for my loader. My current code lokes like this:

from langchain.document_loaders import TextLoader
from langchain.indexes import VectorstoreIndexCreator

loader = TextLoader("./temp.txt", autodetect_encoding=True)
index = VectorstoreIndexCreator().from_loaders([loader])

How can I directly use the string as an input to avoid having to first write it to a TextFile.

I wasn't able to find anything on this in their documentation.

Upvotes: 0

Views: 1138

Answers (1)

InsertCheesyLine
InsertCheesyLine

Reputation: 1373

You can manually create a Document object

from langchain.docstore.document import Document

docs =  Document(page_content="YOUR_STRING")

Then pass it to VectorstoreIndexCreator

from langchain.indexes import VectorstoreIndexCreator
index = VectorstoreIndexCreator().from_documents([docs])

Upvotes: 1

Related Questions