utils#

Various Utility Functions

  • Tools for handling bson.ObjectId

The help IDs live as ObjectId in MongoDB and str in Langchain and JSON.

  • Tools for the Maximal Marginal Relevance (MMR) reranking

These are duplicated from langchain_community to avoid cross-dependencies.

Functions “maximal_marginal_relevance” and “cosine_similarity” are duplicated in this utility respectively from modules:

  • “libs/community/langchain_community/vectorstores/utils.py”

  • “libs/community/langchain_community/utils/math.py”

Functions

utils.cosine_similarity(X, Y)

Row-wise cosine similarity between two equal-width matrices.

utils.make_serializable(obj)

Recursively cast values in a dict to a form able to json.dump

utils.maximal_marginal_relevance(...[, ...])

Compute Maximal Marginal Relevance (MMR).

utils.oid_to_str(oid)

Convert MongoDB's internal BSON ObjectId into a simple str for compatibility.

utils.str_to_oid(str_repr)

Attempt to cast string representation of id to MongoDB's internal BSON ObjectId.