Repository: explodinggradients/ragas
Issue: Parallelization conflict in evaluate function
Root cause: Resource Mishandling

When I run `evaluate` with any model of VertexAI, I get several warnings that say 

> Gapic client context issue detected.This can occur due to parallelization.

And sometimes the execution of evaluate gets stuck without completing all evaluations. I think this is caused by reusing the same llm client in the evaluate function.

Ragas version:
Python version: 0.1.9}

**Code to Reproduce**
```python
from datasets import load_dataset

# loading the V2 dataset
amnesty_qa = load_dataset("explodinggradients/amnesty_qa", "english_v2", trust_remote_code=True)
amnesty_qa

import os
import sys
import pathlib
from dotenv import load_dotenv
from google.auth import default

from langchain_google_vertexai import VertexAIEmbeddings # GCP embeddings

load_dotenv()

os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")

# Project and Storage Constants
credentials, PROJECT_ID = default()

from langchain_google_vertexai import ChatVertexAI, VertexAIEmbeddings
from ragas.llms import LangchainLLMWrapper

# create Langchain LLM and Embeddings
vertextai_llm = ChatVertexAI(
    model_name="gemini-1.5-flash-001",
    temperature=0
)

vertextai_embeddings = VertexAIEmbeddings(model_name="textembedding-gecko@003")

from ragas import evaluate
from ragas.metrics import (
    faithfulness,
    answer_relevancy,
    context_recall,
    context_precision,
    context_relevancy,
    answer_similarity,
    answer_correctness
)

result = evaluate(
    amnesty_qa["eval"],
    metrics=[
        faithfulness,
        answer_relevancy,
        context_recall,
        context_precision,
        context_relevancy,
        answer_similarity,
        answer_correctness
    ],
    llm=vertextai_llm,
    embeddings=vertextai_embeddings
)

result = result.to_pandas()
```

**Error trace**

**Expected behavior**
The evaluation should use separate LLM clients to prevent parallel conflicts.


<!-- Greeting -->
Hello, @aeronesto! I'm here to assist you with any bugs, questions, or contributions you may have. Let's work together to resolve the issue you've encountered.

<!-- Answer -->
The issue you're encountering is likely due to the reuse of the same LLM client in a parallelized context, which can cause conflicts. Here are some best practices and adjustments you can make to handle this more effectively:

1. **Create Separate Instances**: Ensure that each parallel task has its own instance of the LLM client. This avoids conflicts and ensures smooth execution.

2. **Adjust Configuration Parameters**: Use the `RunConfig` class to set appropriate timeouts and retry logic, which can help manage parallel execution more effectively.

Here's how you can modify your code to create separate instances of the LLM client for each evaluation task:

```python
from datasets import load_dataset
from dotenv import load_dotenv
from google.auth import default
from langchain_google_vertexai import ChatVertexAI, VertexAIEmbeddings
from ragas.llms import LangchainLLMWrapper
from ragas import evaluate
from ragas.metrics import (
    faithfulness,
    answer_relevancy,
    context_recall,
    context_precision,
    context_relevancy,
    answer_similarity,
    answer_correctness
)

# Load environment variables
load_dotenv()

# Set Google API key
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")

# Project and Storage Constants
credentials, PROJECT_ID = default()

# Load the dataset
amnesty_qa = load_dataset("explodinggradients/amnesty_qa", "english_v2", trust_remote_code=True)

# Function to create a new instance of the LLM client
def create_llm_client():
    return ChatVertexAI(
        model_name="gemini-1.5-flash-001",
        temperature=0
    )

# Create embeddings instance
vertextai_embeddings = VertexAIEmbeddings(model_name="textembedding-gecko@003")

# Evaluate with separate LLM client instances
result = evaluate(
    amnesty_qa["eval"],
    metrics=[
        faithfulness,
        answer_relevancy,
        context_recall,
        context_precision,
        context_relevancy,
        answer_similarity,
        answer_correctness
    ],
    llm=create_llm_client(),  # Use a new instance for each evaluation
    embeddings=vertextai_embeddings,
    is_async=True  # Enable asynchronous execution
)

result = result.to_pandas()
```

By creating a new instance of the LLM client for each evaluation, you can avoid the parallelization conflicts you're experiencing. Additionally, setting `is_async=True` enables asynchronous execution, which can further improve performance <sup>[[1]](https://github.com/explodinggradients/ragas/blob/main/src/ragas/llms/base.py)</sup><sup>[[2]](https://github.com/explodinggradients/ragas/blob/main/src/ragas/evaluation.py)</sup>.


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


Setting `llm=create_llm_client()` still uses the same client and setting `is_async=True` didn't resolve the issue.

@aeronesto with #1093 we were able to solve the issues with vertexAI. if you do get a chance can you check it out? would love you feedback 🙂 

It seems the issue was answered, closing this now.