explodinggradients / ragas

Evaluation framework for your Retrieval Augmented Generation (RAG) pipelines
https://docs.ragas.io
Apache License 2.0
6.84k stars 681 forks source link

AttributeError #1447

Open amin-kh96 opened 2 days ago

amin-kh96 commented 2 days ago

i deifned this function to not use the LLM calls but it still give me this error:

Exception has occurred: AttributeError 'list' object has no attribute 'get_sample_type' File "C:\Users\Amin\OneDrive - unige.it\Documenti\projectss\ragas-prototype\src\rag_eva.py", line 222, in evaluation_report = evaluate(ragas_data, metrics=metrics, embeddings=ragas_embeddings,llm=custom_llm) # Pass your custom LLM here ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'get_sample_type'

my code is this:

Define the custom LLM class

class CustomRagasLLM(BaseRagasLLM): def init(self, api_key: str = None): """ Initialize the custom LLM, optionally using an API key if necessary. """ self.api_key = api_key

def _call(self, prompt: str) -> str:
    """
    Process the prompt and return a result. This can be customized to
    use a local model or perform any required logic.
    """
    if not self.api_key:
        return f"Processed: {prompt} (without API key)"
    else:
        # Handle LLM response if using an API
        return f"Processed: {prompt} (with API key: {self.api_key})"

def generate_text(
    self,
    prompt: PromptValue,
    n: int = 1,
    temperature: float = 1e-8,
    stop: t.Optional[t.List[str]] = None,
    callbacks: t.List = []
) -> LLMResult:
    # Synchronous generation logic
    text = self._call(prompt)
    return LLMResult(generations=[Generation(text=text)])

async def agenerate_text(
    self,
    prompt: PromptValue,
    n: int = 1,
    temperature: float = 1e-8,
    stop: t.Optional[t.List[str]] = None,
    callbacks: t.List = []
) -> LLMResult:
    """
    Asynchronous method to generate text. This should allow for async processing.
    """
    # Simulate an asynchronous call, here we directly call the sync method for now
    text =await self._call(prompt)
    return LLMResult(generations=[Generation(text=text)])
dosubot[bot] commented 2 days ago

To fix the AttributeError: 'list' object has no attribute 'get_sample_type', ensure that you are using an EvaluationDataset instance instead of a list when calling the evaluate function. Here's how you can create an EvaluationDataset correctly:

  1. Convert your list to an EvaluationDataset:

    If you have a list of samples, you need to convert it to an EvaluationDataset:

    from ragas import EvaluationDataset
    
    # Assuming ragas_data is your list of samples
    evaluation_dataset = EvaluationDataset.from_list(ragas_data)
    
    # Use evaluation_dataset in the evaluate function
    evaluation_report = evaluate(evaluation_dataset, metrics=metrics, embeddings=ragas_embeddings, llm=custom_llm)
  2. Ensure your samples are structured correctly:

    Make sure each sample in your list is an instance of SingleTurnSample or MultiTurnSample, as required by the EvaluationDataset [1][2].

By following these steps, you should be able to resolve the error and proceed with your evaluation.

To continue talking to Dosu, mention @dosu.

amin-kh96 commented 2 days ago

@dosu. how to fix this error: Exception has occurred: AttributeError 'list' object has no attribute 'rename_columns' File "C:\Users\Amin\git\ragas-prototype\src\rag_eva.py", line 196, in evaluation_report = evaluate(ragas_data, metrics=metrics, embeddings=ragas_embeddings,llm=custom_llm) # Pass your custom LLM here ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'rename_columns'

dosubot[bot] commented 2 days ago

To fix the AttributeError: 'list' object has no attribute 'rename_columns', you need to convert your list to a compatible data structure that supports the rename_columns method. Here's how you can do it:

  1. Convert your list to an EvaluationDataset:

    If you have a list of samples, convert it to an EvaluationDataset:

    from ragas import EvaluationDataset
    
    # Assuming ragas_data is your list of samples
    evaluation_dataset = EvaluationDataset.from_list(ragas_data)
    
    # Use evaluation_dataset in the evaluate function
    evaluation_report = evaluate(evaluation_dataset, metrics=metrics, embeddings=ragas_embeddings, llm=custom_llm)

Ensure that each sample in your list is structured correctly as required by the EvaluationDataset [1][2].

To continue talking to Dosu, mention @dosu.

amin-kh96 commented 2 days ago

@dosu. but this does not exist in the Ragas library: EvaluationDataset. which version of ragas are you using?

dosubot[bot] commented 2 days ago

The EvaluationDataset class might not exist in the current version of the Ragas library. Instead, you can use the available methods to handle datasets or lists of samples. Here are some relevant methods:

Ensure your samples are instances of SingleTurnSample or MultiTurnSample for compatibility [1][2].

To continue talking to Dosu, mention @dosu.

amin-kh96 commented 2 days ago

@dosu. i face this error : AttributeError: 'list' object has no attribute 'rename_columns' and my code is this:

Assuming data_set is a list of dictionaries

ragas_data = [ { "question": entry["text_vector_1"], # Assuming this is a list of strings "answer": entry["text_vector_2"], # Assuming this is a list of strings "contexts": entry["chunks"] # Assuming this is a list of lists of strings } for entry in data_set ]

Create the required structure for Dataset

formatted_data = { "question": [entry["question"] for entry in ragas_data], "contexts": [entry["contexts"] for entry in ragas_data], "answer": [entry["answer"] for entry in ragas_data] }

the important thing is that I have a list of floats(computed embeddings).

dosubot[bot] commented 2 days ago

To resolve the AttributeError: 'list' object has no attribute 'rename_columns', you need to convert your list of dictionaries into a compatible dataset format that supports the rename_columns method. Here's how you can do it using the from_list method of the EvaluationDataset class:

  1. Convert your list to an EvaluationDataset:

    First, ensure your data is structured correctly and then convert it:

    from ragas import EvaluationDataset
    
    # Assuming ragas_data is your list of dictionaries
    evaluation_dataset = EvaluationDataset.from_list(ragas_data)
    
    # Now you can use evaluation_dataset in the evaluate function
    evaluation_report = evaluate(evaluation_dataset, metrics=metrics, embeddings=ragas_embeddings, llm=custom_llm)
  2. Ensure your data structure is correct:

    Make sure each dictionary in your list has the correct keys and values as required by the EvaluationDataset:

    ragas_data = [
       {
           "question": entry["text_vector_1"],
           "answer": entry["text_vector_2"],
           "contexts": entry["chunks"]
       }
       for entry in data_set
    ]

This should help you avoid the AttributeError by using a compatible dataset structure [1].

To continue talking to Dosu, mention @dosu.