diff --git a/docs/howtos/integrations/_llamaindex.md b/docs/howtos/integrations/_llamaindex.md
index 9092fe7f7..8419b259d 100644
--- a/docs/howtos/integrations/_llamaindex.md
+++ b/docs/howtos/integrations/_llamaindex.md
@@ -10,32 +10,27 @@ You will need an testset to evaluate your `QueryEngine` against. You can either
Let's see how that works with Llamaindex
-
-```python
# load the documents
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader("./nyc_wikipedia").load_data()
-```
Now lets init the `TestsetGenerator` object with the corresponding generator and critic llms
```python
-from ragas.testset.generator import TestsetGenerator
-from ragas.testset.evolutions import simple, reasoning, multi_context
+from ragas.testset import TestsetGenerator
+
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
# generator with openai models
-generator_llm = OpenAI(model="gpt-3.5-turbo-16k")
-critic_llm = OpenAI(model="gpt-4")
-embeddings = OpenAIEmbedding()
+generator_llm = OpenAI(model="gpt-4o")
+embeddings = OpenAIEmbedding(model="text-embedding-3-large")
generator = TestsetGenerator.from_llama_index(
- generator_llm=generator_llm,
- critic_llm=critic_llm,
- embeddings=embeddings,
+ llm=generator_llm,
+ embedding_model=embeddings,
)
```
@@ -46,23 +41,11 @@ Now you are all set to generate the dataset
# generate testset
testset = generator.generate_with_llamaindex_docs(
documents,
- test_size=5,
- distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25},
+ testset_size=5,
)
```
- embedding nodes: 0%| | 0/54 [00:00, ?it/s]
-
-
- Filename and doc_id are the same for all nodes.
-
-
-
- Generating: 0%| | 0/5 [00:00, ?it/s]
-
-
-
```python
df = testset.to_pandas()
df.head()
@@ -89,59 +72,47 @@ df.head()
|
- question |
- contexts |
- ground_truth |
- evolution_type |
- metadata |
- episode_done |
+ user_input |
+ reference_contexts |
+ reference |
+ synthesizer_name |
| 0 |
- What cultural movement began in New York City ... |
- [ Others cite the end of the crack epidemic an... |
- The Harlem Renaissance |
- simple |
- [{'file_path': '/home/jjmachan/jjmachan/explod... |
- True |
+ Why was New York named after the Duke of York? |
+ [Etymology ==\n\nIn 1664, New York was named i... |
+ New York was named after the Duke of York in 1... |
+ AbstractQuerySynthesizer |
| 1 |
- What is the significance of New York City's tr... |
- [ consisting of 51 council members whose distr... |
- New York City's transportation system is both ... |
- simple |
- [{'file_path': '/home/jjmachan/jjmachan/explod... |
- True |
+ How did the early Europan exploraton and setle... |
+ [History ==\n\n\n=== Early history ===\nIn the... |
+ The early European exploration and settlement ... |
+ AbstractQuerySynthesizer |
| 2 |
- What factors led to the creation of Central Pa... |
- [ next ten years with British troops stationed... |
- Public-minded members of the contemporaneous b... |
- reasoning |
- [{'file_path': '/home/jjmachan/jjmachan/explod... |
- True |
+ New York City population culture finance diver... |
+ [New York City, the most populous city in the ... |
+ New York City is a global cultural, financial,... |
+ ComparativeAbstractQuerySynthesizer |
| 3 |
- What was the impact of the Treaty of Breda on ... |
- [ British raids. In 1626, the Dutch colonial D... |
- The Treaty of Breda confirmed the transfer of ... |
- multi_context |
- [{'file_path': '/home/jjmachan/jjmachan/explod... |
- True |
+ How do the economic aspects of New York City, ... |
+ [New York City, the most populous city in the ... |
+ New York City's economic aspects, such as its ... |
+ ComparativeAbstractQuerySynthesizer |
| 4 |
- What role did New York play in the American Re... |
- [ British raids. In 1626, the Dutch colonial D... |
- New York played a significant role in the Amer... |
- simple |
- [{'file_path': '/home/jjmachan/jjmachan/explod... |
- True |
+ What role do biomedical research institutions ... |
+ [Education ==\n\n \n\nNew York City has the la... |
+ Biomedical research institutions in New York C... |
+ SpecificQuerySynthesizer |
@@ -160,8 +131,7 @@ Since we already loaded the dataset into `documents` lets use that.
```python
# build query engine
-from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
-from llama_index.core.settings import Settings
+from llama_index.core import VectorStoreIndex
vector_index = VectorStoreIndex.from_documents(documents)
@@ -174,24 +144,24 @@ Lets try an sample question from the generated testset to see if it is working
```python
# convert it to pandas dataset
df = testset.to_pandas()
-df["question"][0]
+df["user_input"][0]
```
- 'What cultural movement began in New York City and established the African-American literary canon in the United States?'
+ 'Why was New York named after the Duke of York?'
```python
-response_vector = query_engine.query(df["question"][0])
+response_vector = query_engine.query(df["user_input"][0])
print(response_vector)
```
- The Harlem Renaissance was the cultural movement that began in New York City and established the African-American literary canon in the United States.
+ New York was named after the Duke of York because in 1664, the city was named in honor of the Duke of York, who later became King James II of England.
## Evaluating the `QueryEngine`
@@ -210,54 +180,38 @@ Now lets import the metrics we will be using to evaluate
```python
+# import metrics
from ragas.metrics import (
- faithfulness,
- answer_relevancy,
- context_precision,
- context_recall,
+ Faithfulness,
+ AnswerRelevancy,
+ ContextPrecision,
+ ContextRecall,
)
-from ragas.metrics.critique import harmfulness
+# init metrics with evaluator LLM
+from ragas.llms import LlamaIndexLLMWrapper
+evaluator_llm = LlamaIndexLLMWrapper(OpenAI(model="gpt-4o"))
metrics = [
- faithfulness,
- answer_relevancy,
- context_precision,
- context_recall,
- harmfulness,
+ Faithfulness(llm=evaluator_llm),
+ AnswerRelevancy(llm=evaluator_llm),
+ ContextPrecision(llm=evaluator_llm),
+ ContextRecall(llm=evaluator_llm),
]
```
-now lets init the evaluator model
-
-
-```python
-from llama_index.llms.openai import OpenAI
-from llama_index.embeddings.openai import OpenAIEmbedding
-
-# using GPT 3.5, use GPT 4 / 4-turbo for better accuracy
-evaluator_llm = OpenAI(model="gpt-3.5-turbo")
-```
-
the `evaluate()` function expects a dict of "question" and "ground_truth" for metrics. You can easily convert the `testset` to that format
```python
-# convert to HF dataset
-ds = testset.to_dataset()
-
-ds_dict = ds.to_dict()
-ds_dict["question"]
-ds_dict["ground_truth"]
+# convert to Ragas Evaluation Dataset
+ragas_dataset = testset.to_evaluation_dataset()
+ragas_dataset
```
- ['The Harlem Renaissance',
- "New York City's transportation system is both complex and extensive, with a comprehensive mass transit system that accounts for one in every three users of mass transit in the United States. The New York City Subway system is the largest rapid transit system in the world, and the city has a high usage of public transport, with a majority of households not owning a car. Due to their reliance on mass transit, New Yorkers spend less of their household income on transportation compared to the national average.",
- 'Public-minded members of the contemporaneous business elite lobbied for the establishment of Central Park',
- 'The Treaty of Breda confirmed the transfer of New Amsterdam to English control and the renaming of the settlement as New York. The Duke of York, who would later become King James II and VII, played a significant role in the naming of New York City.',
- 'New York played a significant role in the American Revolution. The Stamp Act Congress met in New York in October 1765, and the city became a center for the Sons of Liberty organization. Skirmishes and battles took place in and around New York, including the Battle of Long Island and the Battle of Saratoga. The city was occupied by British forces for much of the war, but it was eventually liberated by American troops in 1783.']
+ EvaluationDataset(features=['user_input', 'reference_contexts', 'reference'], len=7)
@@ -270,34 +224,17 @@ from ragas.integrations.llama_index import evaluate
result = evaluate(
query_engine=query_engine,
metrics=metrics,
- dataset=ds_dict,
- llm=evaluator_llm,
- embeddings=OpenAIEmbedding(),
+ dataset=ragas_dataset,
)
```
- Running Query Engine: 0%| | 0/5 [00:00, ?it/s]
-
-
-
- Evaluating: 0%| | 0/25 [00:00, ?it/s]
-
-
- n values greater than 1 not support for LlamaIndex LLMs
- n values greater than 1 not support for LlamaIndex LLMs
- n values greater than 1 not support for LlamaIndex LLMs
- n values greater than 1 not support for LlamaIndex LLMs
- n values greater than 1 not support for LlamaIndex LLMs
-
-
-
```python
# final scores
print(result)
```
- {'faithfulness': 0.9000, 'answer_relevancy': 0.8993, 'context_precision': 0.9000, 'context_recall': 1.0000, 'harmfulness': 0.0000}
+ {'faithfulness': 0.9746, 'answer_relevancy': 0.9421, 'context_precision': 0.9286, 'context_recall': 0.6857}
You can convert into a pandas dataframe to run more analysis on it.
@@ -328,77 +265,101 @@ result.to_pandas()
| 0 |
- What cultural movement began in New York City ... |
- [=== 19th century ===\n\nOver the course of th... |
- The Harlem Renaissance of literary and cultura... |
- The Harlem Renaissance |
- 0.5 |
- 0.907646 |
- 0.5 |
+ What events led to New York being named after ... |
+ [New York City is the headquarters of the glob... |
+ [Etymology ==\n\nIn 1664, New York was named i... |
+ New York was named in honor of the Duke of Yor... |
+ New York was named after the Duke of York in 1... |
+ 1.000000 |
+ 0.950377 |
+ 1.0 |
1.0 |
- 0 |
| 1 |
- What is the significance of New York City's tr... |
- [== Transportation ==\n\nNew York City's compr... |
- New York City's transportation system is signi... |
- New York City's transportation system is both ... |
- 1.0 |
- 0.986921 |
- 1.0 |
+ How early European explorers and Native Americ... |
+ [=== Dutch rule ===\n\nA permanent European pr... |
+ [History ==\n\n\n=== Early history ===\nIn the... |
+ Early European explorers established a permane... |
+ Early European explorers and Native Americans ... |
+ 1.000000 |
+ 0.896300 |
1.0 |
- 0 |
+ 0.8 |
| 2 |
- What factors led to the creation of Central Pa... |
- [=== 19th century ===\n\nOver the course of th... |
- Prominent American literary figures lived in N... |
- Public-minded members of the contemporaneous b... |
- 1.0 |
- 0.805014 |
+ New York City population economy challenges |
+ [=== Wealth and income disparity ===\nNew York... |
+ [New York City, the most populous city in the ... |
+ New York City has faced challenges related to ... |
+ New York City, as the most populous city in th... |
+ 1.000000 |
+ 0.915717 |
1.0 |
- 1.0 |
- 0 |
+ 0.0 |
| 3 |
- What was the impact of the Treaty of Breda on ... |
- [=== Dutch rule ===\n\nA permanent European pr... |
- The Treaty of Breda resulted in the transfer o... |
- The Treaty of Breda confirmed the transfer of ... |
+ How do the economic aspects of New York City, ... |
+ [=== Wealth and income disparity ===\nNew York... |
+ [New York City, the most populous city in the ... |
+ The economic aspects of New York City, as a gl... |
+ New York City's economic aspects as a global c... |
+ 0.913043 |
+ 0.929317 |
1.0 |
- 0.860931 |
+ 0.0 |
+
+
+ | 4 |
+ What are some of the cultural and architectura... |
+ [==== Staten Island ====\nStaten Island (Richm... |
+ [Geography ==\n\nDuring the Wisconsin glaciati... |
+ Brooklyn is known for its cultural diversity, ... |
+ Brooklyn is distinct within New York City due ... |
+ 1.000000 |
+ 0.902664 |
+ 0.5 |
+ 1.0 |
+
+
+ | 5 |
+ What measures has New York City implemented to... |
+ [==== International events ====\nIn terms of h... |
+ [Environment ==\n\n \nEnvironmental issues in ... |
+ New York City has implemented various measures... |
+ New York City has implemented several measures... |
+ 0.909091 |
+ 1.000000 |
1.0 |
1.0 |
- 0 |
- | 4 |
- What role did New York play in the American Re... |
+ 6 |
+ What role did New York City play during the Am... |
[=== Province of New York and slavery ===\n\nI... |
- New York served as a significant location duri... |
- New York played a significant role in the Amer... |
- 1.0 |
- 0.935846 |
+ [History ==\n\n\n=== Early history ===\nIn the... |
+ New York City served as a significant military... |
+ During the American Revolution, New York City ... |
+ 1.000000 |
+ 1.000000 |
1.0 |
1.0 |
- 0 |
diff --git a/docs/howtos/integrations/llamaindex.ipynb b/docs/howtos/integrations/llamaindex.ipynb
index c20371443..ddf677957 100644
--- a/docs/howtos/integrations/llamaindex.ipynb
+++ b/docs/howtos/integrations/llamaindex.ipynb
@@ -25,11 +25,9 @@
]
},
{
- "cell_type": "code",
- "execution_count": 3,
+ "cell_type": "markdown",
"id": "096e5af0",
"metadata": {},
- "outputs": [],
"source": [
"# load the documents\n",
"from llama_index.core import SimpleDirectoryReader\n",
@@ -47,25 +45,23 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 2,
"id": "e2107b62",
"metadata": {},
"outputs": [],
"source": [
- "from ragas.testset.generator import TestsetGenerator\n",
- "from ragas.testset.evolutions import simple, reasoning, multi_context\n",
+ "from ragas.testset import TestsetGenerator\n",
+ "\n",
"from llama_index.llms.openai import OpenAI\n",
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"\n",
"# generator with openai models\n",
- "generator_llm = OpenAI(model=\"gpt-3.5-turbo-16k\")\n",
- "critic_llm = OpenAI(model=\"gpt-4\")\n",
- "embeddings = OpenAIEmbedding()\n",
+ "generator_llm = OpenAI(model=\"gpt-4o\")\n",
+ "embeddings = OpenAIEmbedding(model=\"text-embedding-3-large\")\n",
"\n",
"generator = TestsetGenerator.from_llama_index(\n",
- " generator_llm=generator_llm,\n",
- " critic_llm=critic_llm,\n",
- " embeddings=embeddings,\n",
+ " llm=generator_llm,\n",
+ " embedding_model=embeddings,\n",
")"
]
},
@@ -79,58 +75,21 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": null,
"id": "fe03839d",
"metadata": {},
- "outputs": [
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "e555d31a1f8f494a9533605c03ec4140",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "embedding nodes: 0%| | 0/54 [00:00, ?it/s]"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Filename and doc_id are the same for all nodes.\n"
- ]
- },
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "33bc385c8b2040679eb3028de741e63c",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "Generating: 0%| | 0/5 [00:00, ?it/s]"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- }
- ],
+ "outputs": [],
"source": [
"# generate testset\n",
"testset = generator.generate_with_llamaindex_docs(\n",
" documents,\n",
- " test_size=5,\n",
- " distributions={simple: 0.5, reasoning: 0.25, multi_context: 0.25},\n",
+ " testset_size=5,\n",
")"
]
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 4,
"id": "0b75a723",
"metadata": {},
"outputs": [
@@ -155,95 +114,83 @@
" \n",
" \n",
" | 0 | \n",
- " What cultural movement began in New York City ... | \n",
- " [ Others cite the end of the crack epidemic an... | \n",
- " The Harlem Renaissance | \n",
- " simple | \n",
- " [{'file_path': '/home/jjmachan/jjmachan/explod... | \n",
- " True | \n",
+ " Why was New York named after the Duke of York? | \n",
+ " [Etymology ==\\n\\nIn 1664, New York was named i... | \n",
+ " New York was named after the Duke of York in 1... | \n",
+ " AbstractQuerySynthesizer | \n",
"
\n",
" \n",
" | 1 | \n",
- " What is the significance of New York City's tr... | \n",
- " [ consisting of 51 council members whose distr... | \n",
- " New York City's transportation system is both ... | \n",
- " simple | \n",
- " [{'file_path': '/home/jjmachan/jjmachan/explod... | \n",
- " True | \n",
+ " How did the early Europan exploraton and setle... | \n",
+ " [History ==\\n\\n\\n=== Early history ===\\nIn the... | \n",
+ " The early European exploration and settlement ... | \n",
+ " AbstractQuerySynthesizer | \n",
"
\n",
" \n",
" | 2 | \n",
- " What factors led to the creation of Central Pa... | \n",
- " [ next ten years with British troops stationed... | \n",
- " Public-minded members of the contemporaneous b... | \n",
- " reasoning | \n",
- " [{'file_path': '/home/jjmachan/jjmachan/explod... | \n",
- " True | \n",
+ " New York City population culture finance diver... | \n",
+ " [New York City, the most populous city in the ... | \n",
+ " New York City is a global cultural, financial,... | \n",
+ " ComparativeAbstractQuerySynthesizer | \n",
"
\n",
" \n",
" | 3 | \n",
- " What was the impact of the Treaty of Breda on ... | \n",
- " [ British raids. In 1626, the Dutch colonial D... | \n",
- " The Treaty of Breda confirmed the transfer of ... | \n",
- " multi_context | \n",
- " [{'file_path': '/home/jjmachan/jjmachan/explod... | \n",
- " True | \n",
+ " How do the economic aspects of New York City, ... | \n",
+ " [New York City, the most populous city in the ... | \n",
+ " New York City's economic aspects, such as its ... | \n",
+ " ComparativeAbstractQuerySynthesizer | \n",
"
\n",
" \n",
" | 4 | \n",
- " What role did New York play in the American Re... | \n",
- " [ British raids. In 1626, the Dutch colonial D... | \n",
- " New York played a significant role in the Amer... | \n",
- " simple | \n",
- " [{'file_path': '/home/jjmachan/jjmachan/explod... | \n",
- " True | \n",
+ " What role do biomedical research institutions ... | \n",
+ " [Education ==\\n\\n \\n\\nNew York City has the la... | \n",
+ " Biomedical research institutions in New York C... | \n",
+ " SpecificQuerySynthesizer | \n",
"
\n",
" \n",
"\n",
""
],
"text/plain": [
- " question \\\n",
- "0 What cultural movement began in New York City ... \n",
- "1 What is the significance of New York City's tr... \n",
- "2 What factors led to the creation of Central Pa... \n",
- "3 What was the impact of the Treaty of Breda on ... \n",
- "4 What role did New York play in the American Re... \n",
+ " user_input \\\n",
+ "0 Why was New York named after the Duke of York? \n",
+ "1 How did the early Europan exploraton and setle... \n",
+ "2 New York City population culture finance diver... \n",
+ "3 How do the economic aspects of New York City, ... \n",
+ "4 What role do biomedical research institutions ... \n",
"\n",
- " contexts \\\n",
- "0 [ Others cite the end of the crack epidemic an... \n",
- "1 [ consisting of 51 council members whose distr... \n",
- "2 [ next ten years with British troops stationed... \n",
- "3 [ British raids. In 1626, the Dutch colonial D... \n",
- "4 [ British raids. In 1626, the Dutch colonial D... \n",
+ " reference_contexts \\\n",
+ "0 [Etymology ==\\n\\nIn 1664, New York was named i... \n",
+ "1 [History ==\\n\\n\\n=== Early history ===\\nIn the... \n",
+ "2 [New York City, the most populous city in the ... \n",
+ "3 [New York City, the most populous city in the ... \n",
+ "4 [Education ==\\n\\n \\n\\nNew York City has the la... \n",
"\n",
- " ground_truth evolution_type \\\n",
- "0 The Harlem Renaissance simple \n",
- "1 New York City's transportation system is both ... simple \n",
- "2 Public-minded members of the contemporaneous b... reasoning \n",
- "3 The Treaty of Breda confirmed the transfer of ... multi_context \n",
- "4 New York played a significant role in the Amer... simple \n",
+ " reference \\\n",
+ "0 New York was named after the Duke of York in 1... \n",
+ "1 The early European exploration and settlement ... \n",
+ "2 New York City is a global cultural, financial,... \n",
+ "3 New York City's economic aspects, such as its ... \n",
+ "4 Biomedical research institutions in New York C... \n",
"\n",
- " metadata episode_done \n",
- "0 [{'file_path': '/home/jjmachan/jjmachan/explod... True \n",
- "1 [{'file_path': '/home/jjmachan/jjmachan/explod... True \n",
- "2 [{'file_path': '/home/jjmachan/jjmachan/explod... True \n",
- "3 [{'file_path': '/home/jjmachan/jjmachan/explod... True \n",
- "4 [{'file_path': '/home/jjmachan/jjmachan/explod... True "
+ " synthesizer_name \n",
+ "0 AbstractQuerySynthesizer \n",
+ "1 AbstractQuerySynthesizer \n",
+ "2 ComparativeAbstractQuerySynthesizer \n",
+ "3 ComparativeAbstractQuerySynthesizer \n",
+ "4 SpecificQuerySynthesizer "
]
},
- "execution_count": 6,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -275,14 +222,13 @@
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 5,
"id": "37c4a1cb",
"metadata": {},
"outputs": [],
"source": [
"# build query engine\n",
- "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
- "from llama_index.core.settings import Settings\n",
+ "from llama_index.core import VectorStoreIndex\n",
"\n",
"vector_index = VectorStoreIndex.from_documents(documents)\n",
"\n",
@@ -299,17 +245,17 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 6,
"id": "895d95b2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "'What cultural movement began in New York City and established the African-American literary canon in the United States?'"
+ "'Why was New York named after the Duke of York?'"
]
},
- "execution_count": 9,
+ "execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -317,12 +263,12 @@
"source": [
"# convert it to pandas dataset\n",
"df = testset.to_pandas()\n",
- "df[\"question\"][0]"
+ "df[\"user_input\"][0]"
]
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 7,
"id": "a25026c2",
"metadata": {},
"outputs": [
@@ -330,12 +276,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "The Harlem Renaissance was the cultural movement that began in New York City and established the African-American literary canon in the United States.\n"
+ "New York was named after the Duke of York because in 1664, the city was named in honor of the Duke of York, who later became King James II of England.\n"
]
}
],
"source": [
- "response_vector = query_engine.query(df[\"question\"][0])\n",
+ "response_vector = query_engine.query(df[\"user_input\"][0])\n",
"\n",
"print(response_vector)"
]
@@ -374,50 +320,30 @@
},
{
"cell_type": "code",
- "execution_count": 11,
+ "execution_count": 8,
"id": "9875132a",
"metadata": {},
"outputs": [],
"source": [
+ "# import metrics\n",
"from ragas.metrics import (\n",
- " faithfulness,\n",
- " answer_relevancy,\n",
- " context_precision,\n",
- " context_recall,\n",
+ " Faithfulness,\n",
+ " AnswerRelevancy,\n",
+ " ContextPrecision,\n",
+ " ContextRecall,\n",
")\n",
- "from ragas.metrics.critique import harmfulness\n",
"\n",
+ "# init metrics with evaluator LLM\n",
+ "from ragas.llms import LlamaIndexLLMWrapper\n",
+ "evaluator_llm = LlamaIndexLLMWrapper(OpenAI(model=\"gpt-4o\"))\n",
"metrics = [\n",
- " faithfulness,\n",
- " answer_relevancy,\n",
- " context_precision,\n",
- " context_recall,\n",
- " harmfulness,\n",
+ " Faithfulness(llm=evaluator_llm),\n",
+ " AnswerRelevancy(llm=evaluator_llm),\n",
+ " ContextPrecision(llm=evaluator_llm),\n",
+ " ContextRecall(llm=evaluator_llm),\n",
"]"
]
},
- {
- "cell_type": "markdown",
- "id": "8230a307",
- "metadata": {},
- "source": [
- "now lets init the evaluator model"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "id": "f8049166",
- "metadata": {},
- "outputs": [],
- "source": [
- "from llama_index.llms.openai import OpenAI\n",
- "from llama_index.embeddings.openai import OpenAIEmbedding\n",
- "\n",
- "# using GPT 3.5, use GPT 4 / 4-turbo for better accuracy\n",
- "evaluator_llm = OpenAI(model=\"gpt-3.5-turbo\")"
- ]
- },
{
"cell_type": "markdown",
"id": "605e5d96",
@@ -428,32 +354,25 @@
},
{
"cell_type": "code",
- "execution_count": 23,
+ "execution_count": 9,
"id": "4b2a81ed",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "['The Harlem Renaissance',\n",
- " \"New York City's transportation system is both complex and extensive, with a comprehensive mass transit system that accounts for one in every three users of mass transit in the United States. The New York City Subway system is the largest rapid transit system in the world, and the city has a high usage of public transport, with a majority of households not owning a car. Due to their reliance on mass transit, New Yorkers spend less of their household income on transportation compared to the national average.\",\n",
- " 'Public-minded members of the contemporaneous business elite lobbied for the establishment of Central Park',\n",
- " 'The Treaty of Breda confirmed the transfer of New Amsterdam to English control and the renaming of the settlement as New York. The Duke of York, who would later become King James II and VII, played a significant role in the naming of New York City.',\n",
- " 'New York played a significant role in the American Revolution. The Stamp Act Congress met in New York in October 1765, and the city became a center for the Sons of Liberty organization. Skirmishes and battles took place in and around New York, including the Battle of Long Island and the Battle of Saratoga. The city was occupied by British forces for much of the war, but it was eventually liberated by American troops in 1783.']"
+ "EvaluationDataset(features=['user_input', 'reference_contexts', 'reference'], len=7)"
]
},
- "execution_count": 23,
+ "execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "# convert to HF dataset\n",
- "ds = testset.to_dataset()\n",
- "\n",
- "ds_dict = ds.to_dict()\n",
- "ds_dict[\"question\"]\n",
- "ds_dict[\"ground_truth\"]"
+ "# convert to Ragas Evaluation Dataset\n",
+ "ragas_dataset = testset.to_evaluation_dataset()\n",
+ "ragas_dataset"
]
},
{
@@ -466,65 +385,23 @@
},
{
"cell_type": "code",
- "execution_count": 24,
+ "execution_count": null,
"id": "05633cc2",
"metadata": {},
- "outputs": [
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "261183e30af84047a6d8c18fdbef1d72",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "Running Query Engine: 0%| | 0/5 [00:00, ?it/s]"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "fd465e91cf81472ea57683bb67ee9860",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "Evaluating: 0%| | 0/25 [00:00, ?it/s]"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "n values greater than 1 not support for LlamaIndex LLMs\n",
- "n values greater than 1 not support for LlamaIndex LLMs\n",
- "n values greater than 1 not support for LlamaIndex LLMs\n",
- "n values greater than 1 not support for LlamaIndex LLMs\n",
- "n values greater than 1 not support for LlamaIndex LLMs\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"from ragas.integrations.llama_index import evaluate\n",
"\n",
"result = evaluate(\n",
" query_engine=query_engine,\n",
" metrics=metrics,\n",
- " dataset=ds_dict,\n",
- " llm=evaluator_llm,\n",
- " embeddings=OpenAIEmbedding(),\n",
+ " dataset=ragas_dataset,\n",
")"
]
},
{
"cell_type": "code",
- "execution_count": 25,
+ "execution_count": 19,
"id": "f927a943",
"metadata": {},
"outputs": [
@@ -532,7 +409,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "{'faithfulness': 0.9000, 'answer_relevancy': 0.8993, 'context_precision': 0.9000, 'context_recall': 1.0000, 'harmfulness': 0.0000}\n"
+ "{'faithfulness': 0.9746, 'answer_relevancy': 0.9421, 'context_precision': 0.9286, 'context_recall': 0.6857}\n"
]
}
],
@@ -551,7 +428,7 @@
},
{
"cell_type": "code",
- "execution_count": 26,
+ "execution_count": 20,
"id": "b96311e2",
"metadata": {},
"outputs": [
@@ -576,120 +453,163 @@
" \n",
" \n",
" | 0 | \n",
- " What cultural movement began in New York City ... | \n",
- " [=== 19th century ===\\n\\nOver the course of th... | \n",
- " The Harlem Renaissance of literary and cultura... | \n",
- " The Harlem Renaissance | \n",
- " 0.5 | \n",
- " 0.907646 | \n",
- " 0.5 | \n",
+ " What events led to New York being named after ... | \n",
+ " [New York City is the headquarters of the glob... | \n",
+ " [Etymology ==\\n\\nIn 1664, New York was named i... | \n",
+ " New York was named in honor of the Duke of Yor... | \n",
+ " New York was named after the Duke of York in 1... | \n",
+ " 1.000000 | \n",
+ " 0.950377 | \n",
+ " 1.0 | \n",
" 1.0 | \n",
- " 0 | \n",
"
\n",
" \n",
" | 1 | \n",
- " What is the significance of New York City's tr... | \n",
- " [== Transportation ==\\n\\nNew York City's compr... | \n",
- " New York City's transportation system is signi... | \n",
- " New York City's transportation system is both ... | \n",
- " 1.0 | \n",
- " 0.986921 | \n",
- " 1.0 | \n",
+ " How early European explorers and Native Americ... | \n",
+ " [=== Dutch rule ===\\n\\nA permanent European pr... | \n",
+ " [History ==\\n\\n\\n=== Early history ===\\nIn the... | \n",
+ " Early European explorers established a permane... | \n",
+ " Early European explorers and Native Americans ... | \n",
+ " 1.000000 | \n",
+ " 0.896300 | \n",
" 1.0 | \n",
- " 0 | \n",
+ " 0.8 | \n",
"
\n",
" \n",
" | 2 | \n",
- " What factors led to the creation of Central Pa... | \n",
- " [=== 19th century ===\\n\\nOver the course of th... | \n",
- " Prominent American literary figures lived in N... | \n",
- " Public-minded members of the contemporaneous b... | \n",
- " 1.0 | \n",
- " 0.805014 | \n",
- " 1.0 | \n",
+ " New York City population economy challenges | \n",
+ " [=== Wealth and income disparity ===\\nNew York... | \n",
+ " [New York City, the most populous city in the ... | \n",
+ " New York City has faced challenges related to ... | \n",
+ " New York City, as the most populous city in th... | \n",
+ " 1.000000 | \n",
+ " 0.915717 | \n",
" 1.0 | \n",
- " 0 | \n",
+ " 0.0 | \n",
"
\n",
" \n",
" | 3 | \n",
- " What was the impact of the Treaty of Breda on ... | \n",
- " [=== Dutch rule ===\\n\\nA permanent European pr... | \n",
- " The Treaty of Breda resulted in the transfer o... | \n",
- " The Treaty of Breda confirmed the transfer of ... | \n",
+ " How do the economic aspects of New York City, ... | \n",
+ " [=== Wealth and income disparity ===\\nNew York... | \n",
+ " [New York City, the most populous city in the ... | \n",
+ " The economic aspects of New York City, as a gl... | \n",
+ " New York City's economic aspects as a global c... | \n",
+ " 0.913043 | \n",
+ " 0.929317 | \n",
" 1.0 | \n",
- " 0.860931 | \n",
+ " 0.0 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " What are some of the cultural and architectura... | \n",
+ " [==== Staten Island ====\\nStaten Island (Richm... | \n",
+ " [Geography ==\\n\\nDuring the Wisconsin glaciati... | \n",
+ " Brooklyn is known for its cultural diversity, ... | \n",
+ " Brooklyn is distinct within New York City due ... | \n",
+ " 1.000000 | \n",
+ " 0.902664 | \n",
+ " 0.5 | \n",
+ " 1.0 | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " What measures has New York City implemented to... | \n",
+ " [==== International events ====\\nIn terms of h... | \n",
+ " [Environment ==\\n\\n \\nEnvironmental issues in ... | \n",
+ " New York City has implemented various measures... | \n",
+ " New York City has implemented several measures... | \n",
+ " 0.909091 | \n",
+ " 1.000000 | \n",
" 1.0 | \n",
" 1.0 | \n",
- " 0 | \n",
"
\n",
" \n",
- " | 4 | \n",
- " What role did New York play in the American Re... | \n",
+ " 6 | \n",
+ " What role did New York City play during the Am... | \n",
" [=== Province of New York and slavery ===\\n\\nI... | \n",
- " New York served as a significant location duri... | \n",
- " New York played a significant role in the Amer... | \n",
+ " [History ==\\n\\n\\n=== Early history ===\\nIn the... | \n",
+ " New York City served as a significant military... | \n",
+ " During the American Revolution, New York City ... | \n",
+ " 1.000000 | \n",
+ " 1.000000 | \n",
" 1.0 | \n",
- " 0.935846 | \n",
" 1.0 | \n",
- " 1.0 | \n",
- " 0 | \n",
"
\n",
" \n",
"\n",
""
],
"text/plain": [
- " question \\\n",
- "0 What cultural movement began in New York City ... \n",
- "1 What is the significance of New York City's tr... \n",
- "2 What factors led to the creation of Central Pa... \n",
- "3 What was the impact of the Treaty of Breda on ... \n",
- "4 What role did New York play in the American Re... \n",
+ " user_input \\\n",
+ "0 What events led to New York being named after ... \n",
+ "1 How early European explorers and Native Americ... \n",
+ "2 New York City population economy challenges \n",
+ "3 How do the economic aspects of New York City, ... \n",
+ "4 What are some of the cultural and architectura... \n",
+ "5 What measures has New York City implemented to... \n",
+ "6 What role did New York City play during the Am... \n",
+ "\n",
+ " retrieved_contexts \\\n",
+ "0 [New York City is the headquarters of the glob... \n",
+ "1 [=== Dutch rule ===\\n\\nA permanent European pr... \n",
+ "2 [=== Wealth and income disparity ===\\nNew York... \n",
+ "3 [=== Wealth and income disparity ===\\nNew York... \n",
+ "4 [==== Staten Island ====\\nStaten Island (Richm... \n",
+ "5 [==== International events ====\\nIn terms of h... \n",
+ "6 [=== Province of New York and slavery ===\\n\\nI... \n",
"\n",
- " contexts \\\n",
- "0 [=== 19th century ===\\n\\nOver the course of th... \n",
- "1 [== Transportation ==\\n\\nNew York City's compr... \n",
- "2 [=== 19th century ===\\n\\nOver the course of th... \n",
- "3 [=== Dutch rule ===\\n\\nA permanent European pr... \n",
- "4 [=== Province of New York and slavery ===\\n\\nI... \n",
+ " reference_contexts \\\n",
+ "0 [Etymology ==\\n\\nIn 1664, New York was named i... \n",
+ "1 [History ==\\n\\n\\n=== Early history ===\\nIn the... \n",
+ "2 [New York City, the most populous city in the ... \n",
+ "3 [New York City, the most populous city in the ... \n",
+ "4 [Geography ==\\n\\nDuring the Wisconsin glaciati... \n",
+ "5 [Environment ==\\n\\n \\nEnvironmental issues in ... \n",
+ "6 [History ==\\n\\n\\n=== Early history ===\\nIn the... \n",
"\n",
- " answer \\\n",
- "0 The Harlem Renaissance of literary and cultura... \n",
- "1 New York City's transportation system is signi... \n",
- "2 Prominent American literary figures lived in N... \n",
- "3 The Treaty of Breda resulted in the transfer o... \n",
- "4 New York served as a significant location duri... \n",
+ " response \\\n",
+ "0 New York was named in honor of the Duke of Yor... \n",
+ "1 Early European explorers established a permane... \n",
+ "2 New York City has faced challenges related to ... \n",
+ "3 The economic aspects of New York City, as a gl... \n",
+ "4 Brooklyn is known for its cultural diversity, ... \n",
+ "5 New York City has implemented various measures... \n",
+ "6 New York City served as a significant military... \n",
"\n",
- " ground_truth faithfulness \\\n",
- "0 The Harlem Renaissance 0.5 \n",
- "1 New York City's transportation system is both ... 1.0 \n",
- "2 Public-minded members of the contemporaneous b... 1.0 \n",
- "3 The Treaty of Breda confirmed the transfer of ... 1.0 \n",
- "4 New York played a significant role in the Amer... 1.0 \n",
+ " reference faithfulness \\\n",
+ "0 New York was named after the Duke of York in 1... 1.000000 \n",
+ "1 Early European explorers and Native Americans ... 1.000000 \n",
+ "2 New York City, as the most populous city in th... 1.000000 \n",
+ "3 New York City's economic aspects as a global c... 0.913043 \n",
+ "4 Brooklyn is distinct within New York City due ... 1.000000 \n",
+ "5 New York City has implemented several measures... 0.909091 \n",
+ "6 During the American Revolution, New York City ... 1.000000 \n",
"\n",
- " answer_relevancy context_precision context_recall harmfulness \n",
- "0 0.907646 0.5 1.0 0 \n",
- "1 0.986921 1.0 1.0 0 \n",
- "2 0.805014 1.0 1.0 0 \n",
- "3 0.860931 1.0 1.0 0 \n",
- "4 0.935846 1.0 1.0 0 "
+ " answer_relevancy context_precision context_recall \n",
+ "0 0.950377 1.0 1.0 \n",
+ "1 0.896300 1.0 0.8 \n",
+ "2 0.915717 1.0 0.0 \n",
+ "3 0.929317 1.0 0.0 \n",
+ "4 0.902664 0.5 1.0 \n",
+ "5 1.000000 1.0 1.0 \n",
+ "6 1.000000 1.0 1.0 "
]
},
- "execution_count": 26,
+ "execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
diff --git a/mkdocs.yml b/mkdocs.yml
index e5ec8ba23..8060d6b54 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -87,6 +87,7 @@ nav:
- Cost Analysis: howtos/applications/_cost.md
- Integrations:
- howtos/integrations/index.md
+ - LlamaIndex: howtos/integrations/_llamaindex.md
- Migrations:
- From v0.1 to v0.2: howtos/migrations/migrate_from_v01_to_v02.md
- 📖 References:
diff --git a/src/ragas/dataset_schema.py b/src/ragas/dataset_schema.py
index c08ddd5dd..b79f800a2 100644
--- a/src/ragas/dataset_schema.py
+++ b/src/ragas/dataset_schema.py
@@ -316,6 +316,9 @@ def __getitem__(
else:
raise TypeError("Index must be int or slice")
+ def is_multi_turn(self) -> bool:
+ return self.get_sample_type() == MultiTurnSample
+
def to_list(self) -> t.List[t.Dict]:
rows = [sample.to_dict() for sample in self.samples]
@@ -341,6 +344,9 @@ def from_list(cls, data: t.List[t.Dict]) -> EvaluationDataset:
samples.extend(SingleTurnSample(**sample) for sample in data)
return cls(samples=samples)
+ def __repr__(self) -> str:
+ return f"EvaluationDataset(features={self.features()}, len={len(self.samples)})"
+
@dataclass
class EvaluationResult:
diff --git a/src/ragas/evaluation.py b/src/ragas/evaluation.py
index 5a874762f..d681e7132 100644
--- a/src/ragas/evaluation.py
+++ b/src/ragas/evaluation.py
@@ -114,7 +114,7 @@ def evaluate(
Returns
-------
EvaluationResult
- EvaluationResult object containing the scores of each metric.
+ EvaluationResult object containing the scores of each metric.
You can use this do analysis later.
Raises
diff --git a/src/ragas/integrations/llama_index.py b/src/ragas/integrations/llama_index.py
index 3a5b6e8b5..d833670f4 100644
--- a/src/ragas/integrations/llama_index.py
+++ b/src/ragas/integrations/llama_index.py
@@ -2,23 +2,22 @@
import logging
import typing as t
-from uuid import uuid4
-
-from datasets import Dataset
+from ragas.dataset_schema import EvaluationDataset, SingleTurnSample
from ragas.embeddings import LlamaIndexEmbeddingsWrapper
from ragas.evaluation import evaluate as ragas_evaluate
-from ragas.exceptions import ExceptionInRunner
from ragas.executor import Executor
from ragas.llms import LlamaIndexLLMWrapper
from ragas.run_config import RunConfig
if t.TYPE_CHECKING:
+ from langchain_core.callbacks import Callbacks
from llama_index.core.base.embeddings.base import (
BaseEmbedding as LlamaIndexEmbeddings,
)
from llama_index.core.base.llms.base import BaseLLM as LlamaindexLLM
+ from ragas.cost import TokenUsageParser
from ragas.evaluation import EvaluationResult
from ragas.metrics.base import Metric
@@ -28,74 +27,76 @@
def evaluate(
query_engine,
- dataset: Dataset,
+ dataset: EvaluationDataset,
metrics: list[Metric],
llm: t.Optional[LlamaindexLLM] = None,
embeddings: t.Optional[LlamaIndexEmbeddings] = None,
+ callbacks: t.Optional[Callbacks] = None,
+ in_ci: bool = False,
+ run_config: t.Optional[RunConfig] = None,
+ token_usage_parser: t.Optional[TokenUsageParser] = None,
raise_exceptions: bool = False,
column_map: t.Optional[t.Dict[str, str]] = None,
- run_config: t.Optional[RunConfig] = None,
+ show_progress: bool = True,
) -> EvaluationResult:
column_map = column_map or {}
# wrap llms and embeddings
li_llm = None
if llm is not None:
- li_llm = LlamaIndexLLMWrapper(llm)
+ li_llm = LlamaIndexLLMWrapper(llm, run_config=run_config)
li_embeddings = None
if embeddings is not None:
- li_embeddings = LlamaIndexEmbeddingsWrapper(embeddings)
+ li_embeddings = LlamaIndexEmbeddingsWrapper(embeddings, run_config=run_config)
# validate and transform dataset
- if dataset is None:
- raise ValueError("Provide dataset!")
+ if dataset is None or not isinstance(dataset, EvaluationDataset):
+ raise ValueError("Please provide a dataset that is of type EvaluationDataset")
exec = Executor(
desc="Running Query Engine",
keep_progress_bar=True,
+ show_progress=show_progress,
raise_exceptions=raise_exceptions,
run_config=run_config,
)
- # get query
- queries = dataset["question"]
+ # check if multi-turn
+ if dataset.is_multi_turn():
+ raise NotImplementedError(
+ "Multi-turn evaluation is not implemented yet. Please do raise an issue on GitHub if you need this feature and we will prioritize it"
+ )
+ samples = t.cast(t.List[SingleTurnSample], dataset.samples)
+
+ # get query and make jobs
+ queries = [sample.user_input for sample in samples]
for i, q in enumerate(queries):
exec.submit(query_engine.aquery, q, name=f"query-{i}")
- answers: t.List[str] = []
- contexts: t.List[t.List[str]] = []
- try:
- results = exec.results()
- if results == []:
- raise ExceptionInRunner()
- except Exception as e:
- raise e
- else:
- for r in results:
- answers.append(r.response)
- contexts.append([n.node.text for n in r.source_nodes])
-
- # create HF dataset
- hf_dataset = Dataset.from_dict(
- {
- "question": queries,
- "contexts": contexts,
- "answer": answers,
- }
- )
- if "ground_truth" in dataset.column_names:
- hf_dataset = hf_dataset.add_column(
- name="ground_truth",
- column=dataset["ground_truth"],
- new_fingerprint=str(uuid4()),
- )
+ # get responses and retrieved contexts
+ responses: t.List[str] = []
+ retrieved_contexts: t.List[t.List[str]] = []
+ results = exec.results()
+ for r in results:
+ responses.append(r.response)
+ retrieved_contexts.append([n.node.text for n in r.source_nodes])
+
+ # append the extra information to the dataset
+ for i, sample in enumerate(samples):
+ sample.response = responses[i]
+ sample.retrieved_contexts = retrieved_contexts[i]
results = ragas_evaluate(
- dataset=hf_dataset,
+ dataset=dataset,
metrics=metrics,
llm=li_llm,
embeddings=li_embeddings,
raise_exceptions=raise_exceptions,
+ callbacks=callbacks,
+ show_progress=show_progress,
+ run_config=run_config or RunConfig(),
+ in_ci=in_ci,
+ token_usage_parser=token_usage_parser,
)
return results
diff --git a/src/ragas/llms/base.py b/src/ragas/llms/base.py
index ecbf6279e..20856deef 100644
--- a/src/ragas/llms/base.py
+++ b/src/ragas/llms/base.py
@@ -299,6 +299,9 @@ def check_args(
"stop": stop,
}
+ def is_finished(self, response: LLMResult) -> bool:
+ return True
+
def generate_text(
self,
prompt: PromptValue,
diff --git a/src/ragas/metrics/_factual_correctness.py b/src/ragas/metrics/_factual_correctness.py
index 1d887cfab..3f027a4a4 100644
--- a/src/ragas/metrics/_factual_correctness.py
+++ b/src/ragas/metrics/_factual_correctness.py
@@ -271,7 +271,6 @@ async def _single_turn_ascore(
reference_response = await self.verify_claims(
premise=reference, hypothesis_list=response_claims, callbacks=callbacks
)
-
if self.mode != "precision":
response_reference = await self.verify_claims(
@@ -286,7 +285,6 @@ async def _single_turn_ascore(
fn = sum(~response_reference)
else:
fn = 0
-
if self.mode == "precision":
score = tp / (tp + fp + 1e-8)
diff --git a/src/ragas/metrics/_tool_call_accuracy.py b/src/ragas/metrics/_tool_call_accuracy.py
index 8fe416d6a..b5a0e0dc5 100644
--- a/src/ragas/metrics/_tool_call_accuracy.py
+++ b/src/ragas/metrics/_tool_call_accuracy.py
@@ -61,7 +61,9 @@ def is_sequence_aligned(
async def _multi_turn_ascore(
self, sample: MultiTurnSample, callbacks: Callbacks
) -> float:
- assert sample.reference_tool_calls is not None, "Reference tool calls is not set"
+ assert (
+ sample.reference_tool_calls is not None
+ ), "Reference tool calls is not set"
pred_tool_calls = []
for item in sample.user_input:
diff --git a/src/ragas/testset/synthesizers/generate.py b/src/ragas/testset/synthesizers/generate.py
index d36de35a3..f8fc1363b 100644
--- a/src/ragas/testset/synthesizers/generate.py
+++ b/src/ragas/testset/synthesizers/generate.py
@@ -9,9 +9,13 @@
from ragas._analytics import TestsetGenerationEvent, track
from ragas.callbacks import new_group
from ragas.cost import TokenUsageParser
-from ragas.embeddings.base import BaseRagasEmbeddings, LangchainEmbeddingsWrapper
+from ragas.embeddings.base import (
+ BaseRagasEmbeddings,
+ LangchainEmbeddingsWrapper,
+ LlamaIndexEmbeddingsWrapper,
+)
from ragas.executor import Executor
-from ragas.llms import BaseRagasLLM, LangchainLLMWrapper
+from ragas.llms import BaseRagasLLM, LangchainLLMWrapper, LlamaIndexLLMWrapper
from ragas.run_config import RunConfig
from ragas.testset.graph import KnowledgeGraph, Node, NodeType
from ragas.testset.synthesizers import default_query_distribution
@@ -24,6 +28,11 @@
from langchain_core.documents import Document as LCDocument
from langchain_core.embeddings.embeddings import Embeddings as LangchainEmbeddings
from langchain_core.language_models import BaseLanguageModel as LangchainLLM
+ from llama_index.core.base.embeddings.base import (
+ BaseEmbedding as LlamaIndexEmbedding,
+ )
+ from llama_index.core.base.llms.base import BaseLLM as LlamaIndexLLM
+ from llama_index.core.schema import Document as LlamaIndexDocument
from ragas.embeddings.base import BaseRagasEmbeddings
from ragas.llms.base import BaseRagasLLM
@@ -71,6 +80,23 @@ def from_langchain(
knowledge_graph,
)
+ @classmethod
+ def from_llama_index(
+ cls,
+ llm: LlamaIndexLLM,
+ embedding_model: LlamaIndexEmbedding,
+ knowledge_graph: t.Optional[KnowledgeGraph] = None,
+ ) -> TestsetGenerator:
+ """
+ Creates a `TestsetGenerator` from a LlamaIndex LLM and embedding model.
+ """
+ knowledge_graph = knowledge_graph or KnowledgeGraph()
+ return cls(
+ LlamaIndexLLMWrapper(llm),
+ LlamaIndexEmbeddingsWrapper(embedding_model),
+ knowledge_graph,
+ )
+
def generate_with_langchain_docs(
self,
documents: t.Sequence[LCDocument],
@@ -85,7 +111,40 @@ def generate_with_langchain_docs(
raise_exceptions: bool = True,
) -> Testset:
"""
- Generates an evaluation dataset based on given scenarios and parameters.
+ Generates an evaluation dataset based on given Langchain documents and parameters.
+
+ Parameters
+ ----------
+ documents : Sequence[LCDocument]
+ A sequence of Langchain documents to use as source material
+ testset_size : int
+ The number of test samples to generate
+ transforms : Optional[Transforms], optional
+ Custom transforms to apply to the documents, by default None
+ transforms_llm : Optional[BaseRagasLLM], optional
+ LLM to use for transforms if different from instance LLM, by default None
+ transforms_embedding_model : Optional[BaseRagasEmbeddings], optional
+ Embedding model to use for transforms if different from instance model, by default None
+ query_distribution : Optional[QueryDistribution], optional
+ Distribution of query types to generate, by default None
+ run_config : Optional[RunConfig], optional
+ Configuration for the generation run, by default None
+ callbacks : Optional[Callbacks], optional
+ Callbacks to use during generation, by default None
+ with_debugging_logs : bool, optional
+ Whether to include debug logs, by default False
+ raise_exceptions : bool, optional
+ Whether to raise exceptions during generation, by default True
+
+ Returns
+ -------
+ Testset
+ The generated evaluation dataset
+
+ Raises
+ ------
+ ValueError
+ If no LLM or embedding model is provided either during initialization or as arguments
"""
# force the user to provide an llm and embedding client to prevent use of default LLMs
@@ -135,6 +194,79 @@ def generate_with_langchain_docs(
raise_exceptions=raise_exceptions,
)
+ def generate_with_llamaindex_docs(
+ self,
+ documents: t.Sequence[LlamaIndexDocument],
+ testset_size: int,
+ transforms: t.Optional[Transforms] = None,
+ transforms_llm: t.Optional[LlamaIndexLLM] = None,
+ transforms_embedding_model: t.Optional[LlamaIndexEmbedding] = None,
+ query_distribution: t.Optional[QueryDistribution] = None,
+ run_config: t.Optional[RunConfig] = None,
+ callbacks: t.Optional[Callbacks] = None,
+ with_debugging_logs=False,
+ raise_exceptions: bool = True,
+ ):
+ """
+ Generates an evaluation dataset based on given scenarios and parameters.
+ """
+
+ run_config = run_config or RunConfig()
+
+ # force the user to provide an llm and embedding client to prevent use of default LLMs
+ if not self.llm and not transforms_llm:
+ raise ValueError(
+ "An llm client was not provided. Provide an LLM on TestsetGenerator instantiation or as an argument for transforms_llm parameter. Alternatively you can provide your own transforms through the `transforms` parameter."
+ )
+ if not self.embedding_model and not transforms_embedding_model:
+ raise ValueError(
+ "An embedding client was not provided. Provide an embedding model on TestsetGenerator instantiation or as an argument for transforms_llm parameter. Alternatively you can provide your own transforms through the `transforms` parameter."
+ )
+
+ if not transforms:
+ if transforms_llm is None:
+ llm_for_transforms = self.llm
+ else:
+ llm_for_transforms = LlamaIndexLLMWrapper(transforms_llm)
+ if transforms_embedding_model is None:
+ embedding_model_for_transforms = self.embedding_model
+ else:
+ embedding_model_for_transforms = LlamaIndexEmbeddingsWrapper(
+ transforms_embedding_model
+ )
+ transforms = default_transforms(
+ llm=llm_for_transforms,
+ embedding_model=embedding_model_for_transforms,
+ )
+
+ # convert the documents to Ragas nodes
+ nodes = []
+ for doc in documents:
+ if doc.text is not None and doc.text.strip() != "":
+ node = Node(
+ type=NodeType.DOCUMENT,
+ properties={
+ "page_content": doc.text,
+ "document_metadata": doc.metadata,
+ },
+ )
+ nodes.append(node)
+
+ kg = KnowledgeGraph(nodes=nodes)
+
+ # apply transforms and update the knowledge graph
+ apply_transforms(kg, transforms, run_config)
+ self.knowledge_graph = kg
+
+ return self.generate(
+ testset_size=testset_size,
+ query_distribution=query_distribution,
+ run_config=run_config,
+ callbacks=callbacks,
+ with_debugging_logs=with_debugging_logs,
+ raise_exceptions=raise_exceptions,
+ )
+
def generate(
self,
testset_size: int,
@@ -182,6 +314,9 @@ def generate(
4. Generate samples for each scenario.
5. Compile the results into an EvaluationDataset.
"""
+ if run_config is not None:
+ self.llm.set_run_config(run_config)
+
query_distribution = query_distribution or default_query_distribution(self.llm)
callbacks = callbacks or []