Python版本:
import chromadb
from chromadb.utils.embedding_functions import HuggingFaceEmbeddingServer
client = chromadb.HttpClient(host="xx.xx.xx.xx", port=xx)
emb_fn = HuggingFaceEmbeddingServer(url="http://xx.xx.xx.xx")
collection = client.get_collection(name="xx", embedding_function=emb_fn)
results = collection.query(
query_texts=["how to do xxx"],
n_results=2,
include=["metadatas"]
)["metadatas"]
for query_result in results:
for item in query_result:
print(item["text"])
同样功能的typescript版本:
const client = new ChromaClient({path: "http://xx.xx.xx.xx:xx"});
const embFn= new HuggingFaceEmbeddingServerFunction({url:"http://xx.xx.xx.xx"});
const collection = await client.getCollection({name:"xx",embeddingFunction: embFn});
const results = await collection.query({queryTexts:"how to do xxx",nResults:2});
console.log(results);