|
1 | 1 | """Generation and evaluation of evals.""" |
2 | 2 |
|
3 | 3 | from random import randint |
4 | | -from typing import ClassVar |
| 4 | +from typing import TYPE_CHECKING, ClassVar |
5 | 5 |
|
6 | 6 | import numpy as np |
7 | | -import pandas as pd |
8 | 7 | from pydantic import BaseModel, ConfigDict, Field, field_validator |
9 | 8 | from sqlmodel import Session, func, select |
10 | 9 | from tqdm.auto import tqdm, trange |
11 | 10 |
|
| 11 | +if TYPE_CHECKING: |
| 12 | + import pandas as pd |
| 13 | + |
12 | 14 | from raglite._config import RAGLiteConfig |
13 | 15 | from raglite._database import Chunk, Document, Eval, create_database_engine |
14 | 16 | from raglite._extract import extract_with_llm |
@@ -176,8 +178,14 @@ def answer_evals( |
176 | 178 | num_evals: int = 100, |
177 | 179 | *, |
178 | 180 | config: RAGLiteConfig | None = None, |
179 | | -) -> pd.DataFrame: |
| 181 | +) -> "pd.DataFrame": |
180 | 182 | """Read evals from the database and answer them with RAG.""" |
| 183 | + try: |
| 184 | + import pandas as pd |
| 185 | + except ModuleNotFoundError as import_error: |
| 186 | + error_message = "To use the `answer_evals` function, please install the `ragas` extra." |
| 187 | + raise ModuleNotFoundError(error_message) from import_error |
| 188 | + |
181 | 189 | # Read evals from the database. |
182 | 190 | config = config or RAGLiteConfig() |
183 | 191 | engine = create_database_engine(config) |
@@ -206,10 +214,11 @@ def answer_evals( |
206 | 214 |
|
207 | 215 |
|
208 | 216 | def evaluate( |
209 | | - answered_evals: pd.DataFrame | int = 100, config: RAGLiteConfig | None = None |
210 | | -) -> pd.DataFrame: |
| 217 | + answered_evals: "pd.DataFrame | int" = 100, config: RAGLiteConfig | None = None |
| 218 | +) -> "pd.DataFrame": |
211 | 219 | """Evaluate the performance of a set of answered evals with Ragas.""" |
212 | 220 | try: |
| 221 | + import pandas as pd |
213 | 222 | from datasets import Dataset |
214 | 223 | from langchain_community.chat_models import ChatLiteLLM |
215 | 224 | from langchain_community.llms import LlamaCpp |
|
0 commit comments