Skip to content

trulens.feedback.embeddings

trulens.feedback.embeddings

Classes

Embeddings

Bases: WithClassInfo, SerialModel

Embedding related feedback function implementations.

Attributes
tru_class_info instance-attribute
tru_class_info: Class

Class information of this pydantic object for use in deserialization.

Using this odd key to not pollute attribute names in whatever class we mix this into. Should be the same as CLASS_INFO.

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

load staticmethod
load(obj, *args, **kwargs)

Deserialize/load this object using the class information in tru_class_info to lookup the actual class that will do the deserialization.

model_validate classmethod
model_validate(*args, **kwargs) -> Any

Deserialized a jsonized version of the app into the instance of the class it was serialized from.

Note

This process uses extra information stored in the jsonized object and handled by WithClassInfo.

__init__
__init__(embed_model: BaseEmbedding)

Instantiates embeddings for feedback functions.

Example

Below is just one example. Embedders from llama-index are supported: https://docs.llamaindex.ai/en/latest/module_guides/models/embeddings/

from llama_index.embeddings.openai import OpenAIEmbedding
from trulens.feedback.embeddings import Embeddings

embed_model = OpenAIEmbedding()

f_embed = Embedding(embed_model=embed_model)
PARAMETER DESCRIPTION
embed_model

TYPE: BaseEmbedding

cosine_distance
cosine_distance(
    query: str, document: str
) -> Union[float, Tuple[float, Dict[str, str]]]

Runs cosine distance on the query and document embeddings

Example

Below is just one example. Embedders from llama-index are supported: https://docs.llamaindex.ai/en/latest/module_guides/models/embeddings/

from llama_index.embeddings.openai import OpenAIEmbedding
from trulens.feedback.embeddings import Embeddings

embed_model = OpenAIEmbedding()

# Create the feedback function
f_embed = feedback.Embeddings(embed_model=embed_model)
f_embed_dist = feedback.Feedback(f_embed.cosine_distance)                .on_input_output()
PARAMETER DESCRIPTION
query

A text prompt to a vector DB.

TYPE: str

document

The document returned from the vector DB.

TYPE: str

RETURNS DESCRIPTION
float

the embedding vector distance

TYPE: Union[float, Tuple[float, Dict[str, str]]]

manhattan_distance
manhattan_distance(
    query: str, document: str
) -> Union[float, Tuple[float, Dict[str, str]]]

Runs L1 distance on the query and document embeddings

Example

Below is just one example. Embedders from llama-index are supported: https://docs.llamaindex.ai/en/latest/module_guides/models/embeddings/

from llama_index.embeddings.openai import OpenAIEmbedding
from trulens.feedback.embeddings import Embeddings

embed_model = OpenAIEmbedding()

# Create the feedback function
f_embed = feedback.Embeddings(embed_model=embed_model)
f_embed_dist = feedback.Feedback(f_embed.manhattan_distance)                .on_input_output()
PARAMETER DESCRIPTION
query

A text prompt to a vector DB.

TYPE: str

document

The document returned from the vector DB.

TYPE: str

RETURNS DESCRIPTION
float

the embedding vector distance

TYPE: Union[float, Tuple[float, Dict[str, str]]]

euclidean_distance
euclidean_distance(
    query: str, document: str
) -> Union[float, Tuple[float, Dict[str, str]]]

Runs L2 distance on the query and document embeddings

Example

Below is just one example. Embedders from llama-index are supported: https://docs.llamaindex.ai/en/latest/module_guides/models/embeddings/

from llama_index.embeddings.openai import OpenAIEmbedding
from trulens.feedback.embeddings import Embeddings

embed_model = OpenAIEmbedding()

# Create the feedback function
f_embed = feedback.Embeddings(embed_model=embed_model)
f_embed_dist = feedback.Feedback(f_embed.euclidean_distance)                .on_input_output()
PARAMETER DESCRIPTION
query

A text prompt to a vector DB.

TYPE: str

document

The document returned from the vector DB.

TYPE: str

RETURNS DESCRIPTION
float

the embedding vector distance

TYPE: Union[float, Tuple[float, Dict[str, str]]]