Reputation: 11
I tried creating a chromadb using embedding values but its not working. I have my own embedding model that I want to deploy on the server. So here if I pass text as list I am getting its embedded values as output in list. But how to create chromadb or faiss db from this embedded values. As in the internet its showing how to create using the embedding model inference. I was able to do this one:
from langchain_huggingface import HuggingFaceEmbeddings
modelPath = 'models/model_v2'
# Create a dictionary with model configuration options, specifying to use the CPU for computations
model_kwargs = {'device':'cpu'}
# Create a dictionary with encoding options, specifically setting 'normalize_embeddings' to False
encode_kwargs = {'normalize_embeddings': False}
# Initialize an instance of HuggingFaceEmbeddings with the specified parameters
embeddings = HuggingFaceEmbeddings(
model_name=modelPath, # Provide the pre-trained model's path
model_kwargs=model_kwargs, # Pass the model configuration options
encode_kwargs=encode_kwargs # Pass the encoding options
)
db = Chroma.from_documents(documents, embeddings)
I was able to create chromadb like this, but here what to do next:
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer(modelPath)
embeddings = model.encode(sentences)
print(embeddings)
Here in the above code, I have both the text and embedding I should be able to create a chromadb from it. Whats the manual process to add both text and embedding values? Please help I am stuck here, not able to move on with it.
And please don't ask why I want this, this is a project requirement. So, that embedding model is in different server and for input text I will be getting embedding output.
Upvotes: 1
Views: 1128
Reputation: 6781
I think in your case you could use the Chroma db client library (chromadb-client
) to connect to the Chroma db server and pass the embeddings vectors (in batch mode for documents knowledge base and then in online mode for each query), each of them obtained from the remote embeddings model server.
Here's an example adapted from the Python GitHub page starter code:
# $ pip install chromadb-client
import chromadb
client = chromadb.HttpClient(host=<chromadb_hostname>,
port=<chromadb_port>)
# initialize a documents collection in remote Chroma db
collection_name="all-my-documents"
collection = client.create_collection(collection_name,
get_or_create=True)
# generate embeddings for the local documents list
# on the remote embeddings model server (using its client)
documents = ["This is document1",
"This is document2"]
docs_embeddings = <a post of `documents` to the embeddings model HTTP server>
# populating the collection: passing docs embeddings,
# documents text and their IDs
# to a remote Chroma db server
collection.add(
documents=documents,
# unique ID for each doc req. by Chroma
ids=["doc1", "doc2"],
embeddings=docs_embeddings
)
# querying the remote Chroma db docs collection
# with embeddings vector first obtained
# from the remote model server for the new text query
query = "This is a query about document2"
query_embeddings = <a post of `query` to the embeddings model HTTP server>
results = collection.query(
query_embeddings=query_embeddings,
n_results=1
)
# results["documents"][0] list will be sorted
# by the minimum distance to the query
top_results = results["documents"][0]
top_results
Upvotes: 0