From 1cfbaa18b92ab744d414cf7ab769ce6f66af7352 Mon Sep 17 00:00:00 2001 From: Jafar Isbarov <60838378+ceferisbarov@users.noreply.github.com> Date: Tue, 21 Nov 2023 15:26:37 +0400 Subject: [PATCH] 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. --- src/ragas/llama_index/evaluation.py | 2 +- src/ragas/llms/llamaindex.py | 8 +++++++- src/ragas/testset/testset_generator.py | 15 +++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/ragas/llama_index/evaluation.py b/src/ragas/llama_index/evaluation.py index eec95cae0..dab200688 100644 --- a/src/ragas/llama_index/evaluation.py +++ b/src/ragas/llama_index/evaluation.py @@ -77,7 +77,7 @@ def evaluate( except ImportError: raise ImportError( "llama_index must be installed to use this function. " - "Install it with `pip install llama_index`." + "Please, install it with `pip install llama_index`." ) # TODO: rate limit, error handling, retries diff --git a/src/ragas/llms/llamaindex.py b/src/ragas/llms/llamaindex.py index 5554754ca..63b01987a 100644 --- a/src/ragas/llms/llamaindex.py +++ b/src/ragas/llms/llamaindex.py @@ -10,7 +10,13 @@ if t.TYPE_CHECKING: from langchain.callbacks.base import Callbacks from langchain.prompts import ChatPromptTemplate - from llama_index.llms.base import LLM as LiLLM + try: + from llama_index.llms.base import LLM as LiLLM + except ImportError: + raise ImportError( + "llama_index must be installed to use this function. " + "Please, install it with `pip install llama_index`." + ) class LlamaIndexLLM(RagasLLM): diff --git a/src/ragas/testset/testset_generator.py b/src/ragas/testset/testset_generator.py index 67cbcc3c9..51ba8ab50 100644 --- a/src/ragas/testset/testset_generator.py +++ b/src/ragas/testset/testset_generator.py @@ -5,6 +5,17 @@ from collections import defaultdict, namedtuple from dataclasses import dataclass +try: + from llama_index.indices.query.embedding_utils import get_top_k_embeddings + from llama_index.node_parser import SimpleNodeParser + from llama_index.readers.schema import Document as LlamaindexDocument + from llama_index.schema import BaseNode +except ImportError: + raise ImportError( + "llama_index must be installed to use this function. " + "Please, install it with `pip install llama_index`." + ) + import numpy as np import numpy.testing as npt import pandas as pd @@ -12,10 +23,6 @@ from langchain.embeddings.base import Embeddings from langchain.prompts import ChatPromptTemplate from langchain.schema.document import Document as LangchainDocument -from llama_index.indices.query.embedding_utils import get_top_k_embeddings -from llama_index.node_parser import SimpleNodeParser -from llama_index.readers.schema import Document as LlamaindexDocument -from llama_index.schema import BaseNode from numpy.random import default_rng from tqdm import tqdm