From e55db5841aaa86cd96494bf3428a8b0e1591e4b4 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 19 Apr 2023 20:14:01 -0700 Subject: [PATCH] Harrison/svm speedup (#3195) Co-authored-by: Lance Martin <122662504+PineappleExpress808@users.noreply.github.com> --- langchain/retrievers/svm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/langchain/retrievers/svm.py b/langchain/retrievers/svm.py index 2060d3b0fd..694ac97893 100644 --- a/langchain/retrievers/svm.py +++ b/langchain/retrievers/svm.py @@ -4,6 +4,7 @@ https://github.com/karpathy/randomfun/blob/master/knn_vs_svm.ipynb""" from __future__ import annotations +import concurrent.futures from typing import Any, List, Optional import numpy as np @@ -14,7 +15,8 @@ from langchain.schema import BaseRetriever, Document def create_index(contexts: List[str], embeddings: Embeddings) -> np.ndarray: - return np.array([embeddings.embed_query(split) for split in contexts]) + with concurrent.futures.ThreadPoolExecutor() as executor: + return np.array(list(executor.map(embeddings.embed_query, contexts))) class SVMRetriever(BaseRetriever, BaseModel):