Repository: explodinggradients/ragas
Issue: AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'. Did you mean: 'agenerate_text'?
Root cause: Interface Contract Mismatch

[ ] I have checked the [documentation](https://docs.ragas.io/) and related resources and couldn't resolve my bug.

**Describe the bug**
```
AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'. Did you mean: 'agenerate_text'?
```
Ragas version:0.1.10
Python version:3.10.12

**Code to Reproduce**
```py
bedrock = boto3.client(service_name="bedrock-runtime", region_name = 'us-east-1', aws_access_key_id=AWS_ACCESS_KEY_ID, 
                        aws_secret_access_key=AWS_SECRET_ACCESS_KEY, 
                        aws_session_token= AWS_SESSION_TOKEN)

llm_model = BedrockChat(model_id='mistral.mistral-large-2402-v1:0', client=bedrock)

llm_model = LangchainLLMWrapper(llm_model)
embeddings = LangchainEmbeddingsWrapper(embeddings)

splitter = TokenTextSplitter(chunk_size=200, chunk_overlap=10)
keyphrase_extractor = KeyphraseExtractor(llm=llm_model)

docstore = InMemoryDocumentStore(
    splitter=splitter,
    embeddings=embeddings,
    extractor=keyphrase_extractor,
)


generator = TestsetGenerator.from_langchain(
    generator_llm=llm_model,
    critic_llm=llm_model,
    embeddings=embeddings,
    docstore=docstore
)

testset = generator.generate_with_langchain_docs(documents, test_size=1, is_async=False, raise_exceptions=False, distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25})


test_df = testset.to_pandas()
test_df.head()
test_df.to_csv('awsbedrocktest.csv')
```

**Error trace**
```
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/executor.py", line 109, in wrapped_callable_async
    result = await result_or_coroutine
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/testset/evolutions.py", line 142, in evolve
    ) = await self._aevolve(current_tries, current_nodes)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/testset/evolutions.py", line 465, in _aevolve
    simple_question, current_nodes, _ = await self.se._aevolve(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/testset/evolutions.py", line 296, in _aevolve
    passed = await self.node_filter.filter(merged_node)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/testset/filters.py", line 56, in filter
    results = await self.llm.generate(prompt=prompt)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/llms/base.py", line 93, in generate
    return await agenerate_text_with_retry(
  File "/usr/local/lib/python3.10/dist-packages/tenacity/_asyncio.py", line 88, in async_wrapped
    return await fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/tenacity/_asyncio.py", line 47, in __call__
    do = self.iter(retry_state=retry_state)
  File "/usr/local/lib/python3.10/dist-packages/tenacity/__init__.py", line 325, in iter
    raise retry_exc.reraise()
  File "/usr/local/lib/python3.10/dist-packages/tenacity/__init__.py", line 158, in reraise
    raise self.last_attempt.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 451, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.10/dist-packages/tenacity/_asyncio.py", line 50, in __call__
    result = await fn(*args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/ragas/llms/base.py", line 178, in agenerate_text
    result = await self.langchain_llm.agenerate_prompt(
AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'. Did you mean: 'agenerate_text'?
```

**Expected behavior**
A clear and concise description of what you expected to happen.


**Additional context**
Add any other context about the problem here.

<!-- PS: bugs suck but is also part of the process. We sincerely apologies for breaking your flow because of it, but don't worry, we got your back ❤️. We will get this fixed as fast as we can and thanks for helping us out by reporting it 🙏. -->


<!-- Answer -->
To resolve the `AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate_prompt'`, you need to replace the calls to `agenerate_prompt` with `agenerate_text` in the `LangchainLLMWrapper` class. The `LangchainLLMWrapper` class does not have a method named `agenerate_prompt`, but it does have an `agenerate_text` method.

Here is the corrected code for the `LangchainLLMWrapper` class:

```python
class LangchainLLMWrapper(BaseRagasLLM):
    """
    A simple base class for RagasLLMs that is based on Langchain's BaseLanguageModel
    interface. it implements 2 functions:
    - generate_text: for generating text from a given PromptValue
    - agenerate_text: for generating text from a given PromptValue asynchronously
    """

    def __init__(
        self, langchain_llm: BaseLanguageModel, run_config: t.Optional[RunConfig] = None
    ):
        self.langchain_llm = langchain_llm
        if run_config is None:
            run_config = RunConfig()
        self.set_run_config(run_config)

    def generate_text(
        self,
        prompt: PromptValue,
        n: int = 1,
        temperature: float = 1e-8,
        stop: t.Optional[t.List[str]] = None,
        callbacks: Callbacks = None,
    ) -> LLMResult:
        temperature = self.get_temperature(n=n)
        if is_multiple_completion_supported(self.langchain_llm):
            return self.langchain_llm.generate(
                prompts=[prompt],
                n=n,
                temperature=temperature,
                stop=stop,
                callbacks=callbacks,
            )
        else:
            result = self.langchain_llm.generate(
                prompts=[prompt] * n,
                temperature=temperature,
                stop=stop,
                callbacks=callbacks,
            )
            # make LLMResult.generation appear as if it was n_completions
            # note that LLMResult.runs is still a list that represents each run
            generations = [[g[0] for g in result.generations]]
            result.generations = generations
            return result

    async def agenerate_text(
        self,
        prompt: PromptValue,
        n: int = 1,
        temperature: float = 1e-8,
        stop: t.Optional[t.List[str]] = None,
        callbacks: Callbacks = None,
    ) -> LLMResult:
        temperature = self.get_temperature(n=n)
        if is_multiple_completion_supported(self.langchain_llm):
            return await self.langchain_llm.agenerate(
                prompts=[prompt],
                n=n,
                temperature=temperature,
                stop=stop,
                callbacks=callbacks,
            )
        else:
            result = await self.langchain_llm.agenerate(
                prompts=[prompt] * n,
                temperature=temperature,
                stop=stop,
                callbacks=callbacks,
            )
            # make LLMResult.generation appear as if it was n_completions
            # note that LLMResult.runs is still a list that represents each run
            generations = [[g[0] for g in result.generations]]
            result.generations = generations
            return result

    def set_run_config(self, run_config: RunConfig):
        self.run_config = run_config

        # configure if using OpenAI API
        if isinstance(self.langchain_llm, BaseOpenAI) or isinstance(
            self.langchain_llm, ChatOpenAI
        ):
            try:
                from openai import RateLimitError
            except ImportError:
                raise ImportError(
                    "openai.error.RateLimitError not found. Please install openai package as `pip install openai`"
                )
            self.langchain_llm.request_timeout = run_config.timeout
            self.run_config.exception_types = RateLimitError
```

This should resolve the `AttributeError` you are encountering <sup>[[1]](https://github.com/explodinggradients/ragas/blob/main/src/ragas/llms/base.py)</sup>.


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


@Rugved2204 there is a type error in you code. The expected input to `from_langchain` is the langchain LLM objects, no need to wrap it

It seems the issue was answered, closing this now.

Hi @dosu, after I replaced the class LangchainLLMWrapper() with your above code, I got a new error as follows:
File "/Users/.../venv/lib/python3.11/site-packages/ragas/llms/base.py", line 182, in agenerate_text
    result = await self.langchain_llm.agenerate(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate'. Did you mean: 'generate'?

Ragas version:0.1.19
Python version:3.11.0

Could you please provide more information on how to resolve it? Thanks.

> Hi @dosu, after I replaced the class LangchainLLMWrapper() with your above code, I got a new error as follows: File "/Users/.../venv/lib/python3.11/site-packages/ragas/llms/base.py", line 182, in agenerate_text result = await self.langchain_llm.agenerate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'LangchainLLMWrapper' object has no attribute 'agenerate'. Did you mean: 'generate'?
> 
> Ragas version:0.1.19 Python version:3.11.0
> 
> Could you please provide more information on how to resolve it? Thanks.

I'm facing the same


@Josh-Chen-CanaDREAM @btebbutt 

can you share you're code? if you using `from_langchain()` don't wrap it, just pass the langchain class

do check the type hints too - that will sort these issues easily