explodinggradients / ragas

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

AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'. Did you mean: 'agenerate_text'? using llm and embeddings from Huggingface #1020

Open Manel-Hik opened 3 weeks ago

Manel-Hik commented 3 weeks ago

[ ] I have checked the documentation and related resources and couldn't resolve my bug.

Describe the bug I'm trying to generate a test set using my model llama3 from HF (already hosted throught tgi) and embedding also from HF, from a local pdf document

Code to Reproduce

embedding_url = "my_emebdding_url"
embeddings = HuggingFaceEndpointEmbeddings(model=embedding_url, huggingfacehub_api_token="my_token")
hf_model_url= "mylmodelurl"
llm = OpenAI(
    base_url=hf_model_url,
    api_key="nokey",
    top_p=0.9

)
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper

llm = LangchainLLMWrapper(llm)
llm = LangchainLLMWrapper(llm)
embeddings = LangchainEmbeddingsWrapper(embeddings)

from ragas.metrics import (
    context_precision,
    faithfulness,
    context_recall,
)
from ragas.metrics.critique import harmfulness

faithfulness.llm = llm
context_precision.llm = llm
context_recall.llm = llm
harmfulness.llm = llm

from ragas.testset.generator import TestsetGenerator
for document in docs:
    document.metadata['filename'] = document.metadata['source']

from ragas.testset.generator import TestsetGenerator
from ragas.testset.evolutions import simple, reasoning, multi_context
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

generator_llm = llm
critic_llm = llm
embeddings = embeddings

generator = TestsetGenerator.from_langchain(
    generator_llm,
    critic_llm,
    embeddings
)

testset = generator.generate_with_langchain_docs(docs[:9], 
                                                 test_size=10,
                                                 distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25},
                                                 raise_exceptions=False)

Error trace

result = await self.langchain_llm.agenerate_prompt(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'. Did you mean: 'agenerate_text'?
---------------------------------------------------------------------------
ExceptionInRunner                         Traceback (most recent call last)
Cell In[91], line 17
     10 generator = TestsetGenerator.from_langchain(
     11     generator_llm,
     12     critic_llm,
     13     embeddings
     14 )
     16 # generate testset
---> 17 testset = generator.generate_with_langchain_docs(docs[:9], 
     18                                                  test_size=10,
     19                                                  distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25},
     20                                                  raise_exceptions=False)

File ~/miniconda3/lib/python3.12/site-packages/ragas/testset/generator.py:206, in TestsetGenerator.generate_with_langchain_docs(self, documents, test_size, distributions, with_debugging_logs, is_async, raise_exceptions, run_config)
    204 distributions = distributions or {}
    205 # chunk documents and add to docstore
--> 206 self.docstore.add_documents(
    207     [Document.from_langchain_document(doc) for doc in documents]
    208 )
    210 return self.generate(
    211     test_size=test_size,
    212     distributions=distributions,
   (...)
    216     run_config=run_config,
    217 )

File ~/miniconda3/lib/python3.12/site-packages/ragas/testset/docstore.py:215, in InMemoryDocumentStore.add_documents(self, docs, show_progress)
    210 # split documents with self.splitter into smaller nodes
    211 nodes = [
    212     Node.from_langchain_document(d)
    213     for d in self.splitter.transform_documents(docs)
    214 ]
--> 215 self.add_nodes(nodes, show_progress=show_progress)

File ~/miniconda3/lib/python3.12/site-packages/ragas/testset/docstore.py:254, in InMemoryDocumentStore.add_nodes(self, nodes, show_progress)
    252 results = executor.results()
    253 if not results:
--> 254     raise ExceptionInRunner()
    256 for i, n in enumerate(nodes):
    257     if i in nodes_to_embed.keys():

ExceptionInRunner: The runner thread which was running the jobs raised an exeception. Read the traceback above to debug it. You can also pass `raise_exceptions=False` incase you want to show only a warning message instead.

Ragas version: 0.1.9 Python version: 3.12.2 langchain-core : 0.2.5

Could you help me in this please

Thanks in advance

jjmachan commented 6 days ago

hey @Manel-Hik, the issue is that you are wrapping the LLMs twice

embedding_url = "my_emebdding_url"
embeddings = HuggingFaceEndpointEmbeddings(model=embedding_url, huggingfacehub_api_token="my_token")
hf_model_url= "mylmodelurl"
llm = OpenAI(
    base_url=hf_model_url,
    api_key="nokey",
    top_p=0.9

)

from ragas.metrics import (
    context_precision,
    faithfulness,
    context_recall,
)
from ragas.metrics.critique import harmfulness

from ragas.testset.generator import TestsetGenerator
for document in docs:
    document.metadata['filename'] = document.metadata['source']

from ragas.testset.generator import TestsetGenerator
from ragas.testset.evolutions import simple, reasoning, multi_context
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

generator = TestsetGenerator.from_langchain(
    generator_llm=llm
    critic_llm=llm
    embeddings=embeddings
)

testset = generator.generate_with_langchain_docs(docs[:9], 
                                                 test_size=10,
                                                 distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25},
                                                 raise_exceptions=False)

can you try this?