Spaces:
Sleeping
Sleeping
Update ragas_code.py
Browse files- ragas_code.py +53 -0
ragas_code.py
CHANGED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import ragas
|
3 |
+
import pandas as pd
|
4 |
+
from datasets import Dataset, load_dataset
|
5 |
+
from langchain.chat_models import AzureChatOpenAI, ChatOpenAI
|
6 |
+
from langchain.embeddings import AzureOpenAIEmbeddings, OpenAIEmbeddings
|
7 |
+
from ragas.llms import LangchainLLMWrapper
|
8 |
+
from ragas import evaluate
|
9 |
+
from ragas.metrics.critique import harmfulness
|
10 |
+
from ragas.metrics import (
|
11 |
+
context_precision,
|
12 |
+
answer_relevancy,
|
13 |
+
faithfulness,
|
14 |
+
context_recall,
|
15 |
+
context_relevancy)
|
16 |
+
from ragas.metrics._answer_correctness import answer_correctness
|
17 |
+
from ragas.metrics._answer_similarity import answer_similarity
|
18 |
+
|
19 |
+
|
20 |
+
# Ragas Evaluation
|
21 |
+
def ragas_eval(metrics, openai_api_key, df):
|
22 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
23 |
+
llm = ChatOpenAI()
|
24 |
+
embeddings = OpenAIEmbeddings()
|
25 |
+
|
26 |
+
df.rename(columns={"context": "contexts", "ground_truths": "ground_truth"}, inplace=True)
|
27 |
+
df["contexts"] = df["contexts"].apply(lambda x: [x])
|
28 |
+
eval_data = Dataset.from_pandas(df)
|
29 |
+
|
30 |
+
metric_mappings = {
|
31 |
+
"answer_correctness": answer_correctness,
|
32 |
+
"answer_relevancy": answer_relevancy,
|
33 |
+
"faithfulness": faithfulness,
|
34 |
+
"context_precision": context_precision,
|
35 |
+
"context_recall": context_recall,
|
36 |
+
"context_relevancy": context_relevancy,
|
37 |
+
"answer_similarity": answer_similarity,
|
38 |
+
}
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
for metric in metrics:
|
43 |
+
if metric in metric_mappings:
|
44 |
+
result = evaluate(eval_data, metrics=[metric_mappings[metric]], llm=llm, embeddings=embeddings, raise_exceptions=False)
|
45 |
+
df2 = result.to_pandas()
|
46 |
+
|
47 |
+
new_columns = [col for col in df2.columns if col not in df.columns]
|
48 |
+
|
49 |
+
# Append only the new columns to final_df
|
50 |
+
for col in new_columns:
|
51 |
+
df[col] = df2[col]
|
52 |
+
|
53 |
+
return df
|