@@ -210,16 +210,34 @@ def evaluate(
210
210
try :
211
211
from datasets import Dataset
212
212
from langchain_community .chat_models import ChatLiteLLM
213
- from langchain_community .embeddings import LlamaCppEmbeddings
214
213
from langchain_community .llms import LlamaCpp
215
214
from ragas import RunConfig
216
215
from ragas import evaluate as ragas_evaluate
216
+ from ragas .embeddings import BaseRagasEmbeddings
217
217
218
+ from raglite ._config import RAGLiteConfig
219
+ from raglite ._embed import embed_sentences
218
220
from raglite ._litellm import LlamaCppPythonLLM
219
221
except ImportError as import_error :
220
222
error_message = "To use the `evaluate` function, please install the `ragas` extra."
221
223
raise ImportError (error_message ) from import_error
222
224
225
+ class RAGLiteRagasEmbeddings (BaseRagasEmbeddings ):
226
+ """A RAGLite embedder for Ragas."""
227
+
228
+ def __init__ (self , config : RAGLiteConfig | None = None ):
229
+ self .config = config or RAGLiteConfig ()
230
+
231
+ def embed_query (self , text : str ) -> list [float ]:
232
+ # Embed the input text with RAGLite's embedding function.
233
+ embeddings = embed_sentences ([text ], config = self .config )
234
+ return embeddings [0 ].tolist () # type: ignore[no-any-return]
235
+
236
+ def embed_documents (self , texts : list [str ]) -> list [list [float ]]:
237
+ # Embed a list of documents with RAGLite's embedding function.
238
+ embeddings = embed_sentences (texts , config = self .config )
239
+ return embeddings .tolist () # type: ignore[no-any-return]
240
+
223
241
# Create a set of answered evals if not provided.
224
242
config = config or RAGLiteConfig ()
225
243
answered_evals_df = (
@@ -239,23 +257,12 @@ def evaluate(
239
257
)
240
258
else :
241
259
lc_llm = ChatLiteLLM (model = config .llm ) # type: ignore[call-arg]
242
- # Load the embedder.
243
- if not config .embedder .startswith ("llama-cpp-python" ):
244
- error_message = "Currently, only `llama-cpp-python` embedders are supported."
245
- raise NotImplementedError (error_message )
246
- embedder = LlamaCppPythonLLM ().llm (model = config .embedder , embedding = True )
247
- lc_embedder = LlamaCppEmbeddings ( # type: ignore[call-arg]
248
- model_path = embedder .model_path ,
249
- n_batch = embedder .n_batch ,
250
- n_ctx = embedder .n_ctx (),
251
- n_gpu_layers = - 1 ,
252
- verbose = embedder .verbose ,
253
- )
260
+ embedder = RAGLiteRagasEmbeddings (config = config )
254
261
# Evaluate the answered evals with Ragas.
255
262
evaluation_df = ragas_evaluate (
256
263
dataset = Dataset .from_pandas (answered_evals_df ),
257
264
llm = lc_llm ,
258
- embeddings = lc_embedder ,
265
+ embeddings = embedder ,
259
266
run_config = RunConfig (max_workers = 1 ),
260
267
).to_pandas ()
261
268
return evaluation_df
0 commit comments