Skip to content

API KEY #1358

@amin-kh96

Description

@amin-kh96

[ ] I checked the documentation and related resources and couldn't find an answer to my question.

Your Question
I wrote this code and I get the error:
The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
is it mandatory to set an API key to evaluate?
if i have to use Api-key is there any free way to use it?

Code Examples

import json
import pandas as pd
from datasets import Dataset
from langchain.embeddings import HuggingFaceEmbeddings
from ragas.embeddings import LangchainEmbeddingsWrapper
from ragas.metrics import faithfulness,context_utilization
from ragas import evaluate

# Load the ground_truthpip install --upgrade langchain ragas
file_path = 'assets\\GT.json'
with open(file_path) as f:
    ground_truth_data = json.load(f)

#load the question and the answer and the chunks
file_path = 'assets\\user_llm_interaction_embeddings_c1521dd5_b819_4241_b3a4_3e5c1388037c.json'
with open(file_path) as f:
     llm=json.load(f)
#Initialize an empty list to hold the new dataset
data_set = []

#Iterate through the list and combine every two dictionaries
for i in range(0, len(llm), 2):
    combined_dict = {
        "text_vector_1": llm[i].get("text_vector", []),
        "text_vector_2": llm[i + 1].get("text_vector", []),
        'chunks': llm[i + 1].get('chunks', [])
    }
    data_set.append(combined_dict)



def map_chunks(data_set, ground_truth_data):
    for item in data_set:  # Iterate over each dictionary in data_set
        c = []  # Reset c for each item
        for chunk_id in item['chunks']:  # Loop through 'chunks' in the current dictionary
            for element in ground_truth_data:  # Loop through ground_truth_data
                if element['id'] == chunk_id:  # Match chunk_id with element's id
                    c.append(element['text_vector'])  # Append the matching text_vector to c
        item['chunks'] = c  # Replace the original 'chunks' (ids) with the mapped text_vector values

    return data_set  # Return the updated data_set

data_set = map_chunks(data_set, ground_truth_data)

# 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
]
# Convert list of dictionaries to dictionary of lists
ragas_data_dict = {key: [d[key] for d in ragas_data] for key in ragas_data[0]}


# Initialize Hugging Face embeddings
emb = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# Wrap the embeddings
wrapped_embeddings = LangchainEmbeddingsWrapper(embeddings=emb)

# Assuming you have your dataset ready
dataset = ragas_data_dict  # Your dataset here


# Step 4: Define the list of evaluation metrics
metrics = [faithfulness,context_utilization]

# Step 5: Evaluate the data using the RAGAS `evaluate` function
evaluation_report = evaluate(dataset=dataset, metrics=metrics, embeddings=emb)
# Step 6: Display the evaluation results
print("RAGAS Evaluation Report:")
print(evaluation_report)

# Optional: Convert the evaluation report to a DataFrame for better readability
evaluation_df = pd.DataFrame(evaluation_report.items(), columns=['Metric', 'Score'])
print("\nEvaluation Report as DataFrame:")
print(evaluation_df)

Additional context
I have the embeddings my dataset is not str

Metadata

Metadata

Assignees

No one assigned

    Labels

    answered🤖 The question has been answered. Will be closed automatically if no new commentsquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions