Repository: explodinggradients/ragas
Issue: embeddings
Root cause: Documentation Deficiency

I have a dataset in this format:
# Assuming data_set is a list of dictionaries
ragas_data = [
    {
        "question": entry["text_vector_1"],
        "answer": entry["text_vector_2"],
        "contexts": entry["chunks"]
    }
    for entry in data_set
]

the important thing is that the value of each instances are embeddings, and I do not have the STR of the embeddings.
and also I know that my embeddings should be a subclass of baseragasembeddings . but it needs also the str and only does it for question some thing like this code:

import ragas
from ragas.embeddings import BaseRagasEmbeddings
import numpy as np


class CustomEmbeddings(BaseRagasEmbeddings):
    def __init__(self, precomputed_embeddings, index_map):
        """
        :param precomputed_embeddings: A list of precomputed embeddings.
        :param index_map: A dictionary mapping each text (question) to the corresponding embedding index.
        """
        self.embedding_questions= list()precomputed_embeddings
        self.embedding_answers = list()
        self.embedding_context = list()
        self.index_map = index_map  # Maps text to the index of the correct embedding

    def get_embedding(self, text: str) -> np.ndarray:
        """
        Retrieve the precomputed embedding for the given text.
        """
        idx = self.index_map.get(text)
        if idx is not None:
            return np.array(self.precomputed_embeddings[idx])
        else:
            raise ValueError(f"Text '{text}' not found in index map!")

    def get_embeddings(self, texts: list) -> np.ndarray:
        """
        Retrieve embeddings for a list of texts.
        """
        embeddings = [self.get_embedding(text) for text in texts]
        return np.vstack(embeddings)  # Stack them into a 2D array
    
    def embed_documents(self):
        return # 
    def embed_query(self):
        return None

# Example usage:
precomputed_embeddings = [
    [0.1, 0.2, 0.3],  # Embedding for question 1
    [0.4, 0.5, 0.6],  # Embedding for question 2
    [0.7, 0.8, 0.9],  # Embedding for question 3
    [0.7, 0.8, 0.9],  # Embedding for question 3

]

index_map = {
    "When was the first super bowl?": 0,
    "Who won the most super bowls?": 1,
    "Which team scored the most points?": 2
}

custom_embeddings = CustomEmbeddings(precomputed_embeddings=precomputed_embeddings, index_map=index_map)
but my file is not like this. what  should I do to use evaluate function in ragas?



<!-- Answer -->
To use the `evaluate` function in Ragas with your dataset of embeddings, you'll need to implement the `embed_documents` and `embed_query` methods in your `CustomEmbeddings` class. Since you already have precomputed embeddings, you can modify your class to handle these embeddings directly.

Here's how you can adjust your class:

```python
import numpy as np
from ragas.embeddings import BaseRagasEmbeddings

class CustomEmbeddings(BaseRagasEmbeddings):
    def __init__(self, precomputed_embeddings):
        """
        :param precomputed_embeddings: A dictionary with keys 'questions', 'answers', 'contexts' mapping to lists of embeddings.
        """
        self.precomputed_embeddings = precomputed_embeddings

    def embed_query(self, text: str) -> np.ndarray:
        """
        Retrieve the precomputed embedding for the given query text.
        """
        return np.array(self.precomputed_embeddings['questions'])

    def embed_documents(self, texts: list) -> np.ndarray:
        """
        Retrieve embeddings for a list of document texts.
        """
        return np.array(self.precomputed_embeddings['contexts'])

# Example usage:
precomputed_embeddings = {
    'questions': [[0.1, 0.2, 0.3]],  # Example embedding for a question
    'answers': [[0.4, 0.5, 0.6]],    # Example embedding for an answer
    'contexts': [[0.7, 0.8, 0.9]]    # Example embedding for a context
}

custom_embeddings = CustomEmbeddings(precomputed_embeddings=precomputed_embeddings)

# Now you can use this custom embeddings class with the evaluate function
from ragas import evaluate

# Assuming your dataset is structured correctly
result = evaluate(ragas_data, embeddings=custom_embeddings)

print(result)
```

### Key Points:
- **Precomputed Embeddings**: Store your embeddings in a dictionary with keys like 'questions', 'answers', and 'contexts'.
- **Implement Methods**: Implement `embed_query` and `embed_documents` to return the appropriate embeddings.
- **Evaluate Function**: Use your `CustomEmbeddings` class with the `evaluate` function.

This setup assumes you have a way to map your dataset entries to the correct embeddings. Adjust the logic as needed to fit your specific data structure.


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*