Here is a simple code to use Redis and embeddings but It’s not clear how can I build and load own embeddings and then pull it from Redis and use in search
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores.redis import Redis
embeddings = OpenAIEmbeddings
metadata = [
{
"user": "john",
"age": 18,
"job": "engineer",
"credit_score": "high"
}
]
texts = ["foo", "foo", "foo", "bar", "bar"]
rds = Redis.from_texts(
texts,
embeddings,
metadata,
redis_url="redis://localhost:6379",
index_name="users",
)
results = rds.similarity_search("foo")
print(results[0].page_content)
But I want to load a text from e.g. text file, create embedings and load into Redis for later use. Something like this:
from openai import OpenAI
client = OpenAI()
def get_embedding(text, model="text-embedding-ada-002"):
text = text.replace("n", " ")
return client.embeddings.create(input = [text], model=model).data[0].embedding
Does anyone have good example to implement this approach? Also wondering about TTL for embedings in Redis
2
Answers
Seems that the package requires the provided embeddings object to conform to
langchain_core.embeddings.Embeddings
. Looking at the source code, you could go about creating a custom embedder object by implementing the two required methodsembed_query
andembed_document
:Helloļ¼ You can use the TextLoader to load txt and split it into documents!
Just like below: