Skip to content

Commit b582e5b

Browse files
authored
feat: make ragas pip more lighter (explodinggradients#283)
*merge explodinggradients#261 before merging this* - removed all the optional dependencies that is making ragas bloated - add option dependencies for sentence-transformer in `ragas[all]`
1 parent 2802019 commit b582e5b

File tree

7 files changed

+20
-24
lines changed

7 files changed

+20
-24
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dockerfile

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ run-ci: format lint type ## Running all CI checks
2828
run-benchmarks: ## Run benchmarks
2929
@echo "Running benchmarks..."
3030
@cd $(GIT_ROOT)/tests/benchmarks && python benchmark_eval.py
31+
run-benchmarks-in-docker: ## Run benchmarks in docker
32+
@echo "Running benchmarks in docker..."
33+
@cd $(GIT_ROOT)
34+
docker buildx build --build-arg OPENAI_API_KEY=$(OPENAI_API_KEY) -t ragas-benchmark -f $(GIT_ROOT)/tests/benchmarks/Dockerfile .
35+
docker inspect ragas-benchmark:latest | jq ".[0].Size" | numfmt --to=si
3136
test: ## Run tests
3237
@echo "Running tests..."
3338
@pytest tests/unit $(shell if [ -n "$(k)" ]; then echo "-k $(k)"; fi)

pyproject.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
name = "ragas"
33
dependencies = [
44
"numpy",
5-
"transformers",
6-
"sentence-transformers",
75
"datasets",
86
"tiktoken",
97
"langchain",
@@ -13,6 +11,11 @@ dependencies = [
1311
]
1412
dynamic = ["version", "readme"]
1513

14+
[project.optional-dependencies]
15+
all = [
16+
"sentence-transformers",
17+
]
18+
1619
[tool.setuptools]
1720
package-dir = {"" = "src"}
1821

src/ragas/metrics/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
from langchain.callbacks.manager import CallbackManager, trace_as_chain_group
1717
from tqdm import tqdm
1818

19+
from ragas.embeddings.base import RagasEmbeddings
1920
from ragas.llms import llm_factory
2021

2122
if t.TYPE_CHECKING:
2223
from langchain.callbacks.base import Callbacks
2324

24-
from ragas.embeddings.base import RagasEmbeddings
2525
from ragas.llms import RagasLLM
2626

2727

src/ragas/utils.py

-18
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,13 @@
22

33
import logging
44
import os
5-
import typing as t
65
from functools import lru_cache
7-
from warnings import warn
86

9-
import torch
10-
from torch import device as Device
11-
12-
DEVICES = ["cpu", "cuda"]
137
DEBUG_ENV_VAR = "RAGAS_DEBUG"
148
# constant to tell us that there is no key passed to the llm/embeddings
159
NO_KEY = "no-key"
1610

1711

18-
def device_check(device: t.Literal["cpu", "cuda"] | Device) -> torch.device:
19-
if isinstance(device, Device):
20-
return device
21-
if device not in DEVICES:
22-
raise ValueError(f"Invalid device {device}")
23-
if device == "cuda" and not torch.cuda.is_available():
24-
warn("cuda not available, using cpu")
25-
device = "cpu"
26-
27-
return torch.device(device)
28-
29-
3012
@lru_cache(maxsize=1)
3113
def get_debug_mode() -> bool:
3214
if os.environ.get(DEBUG_ENV_VAR, str(False)).lower() == "true":

tests/benchmarks/Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.9-slim
2+
RUN apt-get update && apt-get install -y git make
3+
COPY . /app
4+
WORKDIR /app
5+
RUN pip install -e /app/
6+
ARG OPENAI_API_KEY
7+
ENV OPENAI_API_KEY=$OPENAI_API_KEY
8+
RUN make run-benchmarks

tests/benchmarks/benchmark_eval.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import time
22

33
from datasets import DatasetDict, load_dataset
4-
from torch.cuda import is_available
54

65
from ragas import evaluate
76
from ragas.metrics import (
@@ -12,8 +11,6 @@
1211
)
1312
from ragas.metrics.critique import harmfulness
1413

15-
DEVICE = "cuda" if is_available() else "cpu"
16-
1714
# data
1815
ds = load_dataset("explodinggradients/fiqa", "ragas_eval")
1916
assert isinstance(ds, DatasetDict)

0 commit comments

Comments
 (0)