Skip to content

Commit 1cfbaa1

Browse files
authored
import llama_index gracefully, solves #318 (#319)
I added informative message for import errors regarding llama_index, and changed the existing messages to match each other.
1 parent 13465f0 commit 1cfbaa1

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/ragas/llama_index/evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def evaluate(
7777
except ImportError:
7878
raise ImportError(
7979
"llama_index must be installed to use this function. "
80-
"Install it with `pip install llama_index`."
80+
"Please, install it with `pip install llama_index`."
8181
)
8282

8383
# TODO: rate limit, error handling, retries

src/ragas/llms/llamaindex.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
if t.TYPE_CHECKING:
1111
from langchain.callbacks.base import Callbacks
1212
from langchain.prompts import ChatPromptTemplate
13-
from llama_index.llms.base import LLM as LiLLM
13+
try:
14+
from llama_index.llms.base import LLM as LiLLM
15+
except ImportError:
16+
raise ImportError(
17+
"llama_index must be installed to use this function. "
18+
"Please, install it with `pip install llama_index`."
19+
)
1420

1521

1622
class LlamaIndexLLM(RagasLLM):

src/ragas/testset/testset_generator.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
from collections import defaultdict, namedtuple
66
from dataclasses import dataclass
77

8+
try:
9+
from llama_index.indices.query.embedding_utils import get_top_k_embeddings
10+
from llama_index.node_parser import SimpleNodeParser
11+
from llama_index.readers.schema import Document as LlamaindexDocument
12+
from llama_index.schema import BaseNode
13+
except ImportError:
14+
raise ImportError(
15+
"llama_index must be installed to use this function. "
16+
"Please, install it with `pip install llama_index`."
17+
)
18+
819
import numpy as np
920
import numpy.testing as npt
1021
import pandas as pd
1122
from langchain.embeddings import OpenAIEmbeddings
1223
from langchain.embeddings.base import Embeddings
1324
from langchain.prompts import ChatPromptTemplate
1425
from langchain.schema.document import Document as LangchainDocument
15-
from llama_index.indices.query.embedding_utils import get_top_k_embeddings
16-
from llama_index.node_parser import SimpleNodeParser
17-
from llama_index.readers.schema import Document as LlamaindexDocument
18-
from llama_index.schema import BaseNode
1926
from numpy.random import default_rng
2027
from tqdm import tqdm
2128

0 commit comments

Comments
 (0)