DSPy: The framework for programming—not prompting—language models
Python
OpenAI Embeddings vectorization for KNN added
Igor Kotenkovdb6e4bcf0d77
diff --git a/dsp/modules/sentence_vectorizer.py b/dsp/modules/sentence_vectorizer.pyindex 4ccc7aefc..691340843 100644--- a/dsp/modules/sentence_vectorizer.py+++ b/dsp/modules/sentence_vectorizer.py@@ -1,12 +1,16 @@import abc-from typing import List+from typing import List, Optionalimport numpy as np+import openaifrom dsp.utils import determine_devicesclass BaseSentenceVectorizer(abc.ABC):+ # embeddings will be computed based on the string in this attribute of Example object+ field_to_vectorize = 'text_to_vectorize'+def __init__(self) -> None:pass@@ -14,6 +18,13 @@ class BaseSentenceVectorizer(abc.ABC):def __call__(self, inp_examples: List["Example"]) -> np.ndarray:pass+ def _extract_text_from_examples(self, inp_examples: List["Example"]) -> List[str]:+ text_to_vectorize = [+ getattr(example, self.field_to_vectorize)+ for example in inp_examples+ ]+ return text_to_vectorize+class SentenceTransformersVectorizer(BaseSentenceVectorizer):def __init__(@@ -37,7 +48,8 @@ class SentenceTransformersVectorizer(BaseSentenceVectorizer):self.normalize_embeddings = normalize_embeddingsdef __call__(self, inp_examples: List["Example"]) -> np.ndarray:- text_to_vectorize = [example.text_to_vectorize for example in inp_examples]+ text_to_vectorize = self._extract_text_from_examples(inp_examples)+if self.is_gpu and self.num_devices > 1:target_devices = list(range(self.num_devices))pool = self.model.start_multi_process_pool(target_devices=target_devices)@@ -63,13 +75,48 @@ class SentenceTransformersVectorizer(BaseSentenceVectorizer):class NaiveGetFieldVectorizer(BaseSentenceVectorizer):- def __init__(self, field_with_embeding: str = 'vectorized'):- self.field_with_embeding = field_with_embeding+ def __init__(self, field_with_embedding: str = 'vectorized'):+ self.field_with_embedding = field_with_embeddingdef __call__(self, inp_examples: List["Example"]) -> np.ndarray:embeddings = [- getattr(cur_example, self.field_with_embeding).reshape(1, -1)+ getattr(cur_example, self.field_with_embedding).reshape(1, -1)for cur_example in inp_examples]embeddings = np.concatenate(embeddings, axis=0).astype(np.float32)return embeddings+++class OpenAIVectorizer(BaseSentenceVectorizer):+ def __init__(+ self, model: str = 'text-embedding-ada-002',+ embed_batch_size: int = 1024,+ api_key: Optional[str] = None+ ):+ self.model = model+ self.embed_batch_size = embed_batch_size++ if api_key:+ openai.api_key = api_key++ def __call__(self, inp_examples: List["Example"]) -> np.ndarray:+ text_to_vectorize = self._extract_text_from_examples(inp_examples)+ # maybe it's better to preallocate numpy matrix, but we don't know emb_dim+ embeddings_list = []++ n_batches = (len(text_to_vectorize) - 1) // self.embed_batch_size + 1+ for cur_batch_idx in range(n_batches): # tqdm.tqdm?+ start_idx = cur_batch_idx * self.embed_batch_size+ end_idx = (cur_batch_idx + 1) * self.embed_batch_size+ cur_batch = text_to_vectorize[start_idx: end_idx]+ # cached API call:+ response = openai.Embedding.create(+ model=self.model,+ input=cur_batch+ )++ cur_batch_embeddings = [cur_obj['embedding'] for cur_obj in response['data']]+ embeddings_list.extend(cur_batch_embeddings)++ embeddings = np.array(embeddings_list, dtype=np.float32)+ return embeddingsdiff --git a/knn_demonstrations_test.ipynb b/knn_demonstrations_test.ipynbindex 0920ce1a1..69e49db6a 100644--- a/knn_demonstrations_test.ipynb+++ b/knn_demonstrations_test.ipynb@@ -8,6 +8,9 @@"source": ["import dsp\n","import os\n",+ "import openai\n",+ "# api_key = '<replace with your API key>'\n",+ "# openai.api_key = api_key\n","\n","os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'"]@@ -179,7 +182,6 @@"# context manager is working properly\n","with dsp.settings.context(vectorizer=dsp.SentenceTransformersVectorizer()):\n"," knn_func = dsp.knn(train)\n",- " \n","\n"," knn_res_train_vec = knn_func(train[QUERY_IDX], 3)\n"," print(train[QUERY_IDX].question) # the query itself\n",@@ -197,7 +199,7 @@{"data": {"text/plain": [- "<dsp.modules.sentence_vectorizer.NaiveGetFieldVectorizer at 0x7fc21559c130>"+ "<dsp.modules.sentence_vectorizer.NaiveGetFieldVectorizer at 0x7faae3d71fd0>"]},"execution_count": 11,@@ -249,7 +251,43 @@},{"cell_type": "code",- "execution_count": null,+ "execution_count": 14,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "<dsp.modules.sentence_vectorizer.OpenAIVectorizer object at 0x7faac15c6190>\n",+ "Who produced the album that included a re-recording of \"Lithium\"?\n",+ "--------------------------------------------------------------------------------\n",+ "Who produced the album that included a re-recording of \"Lithium\"?\n",+ "There were two founders of recording studio called \"Smart Studios\". The first one was Butch Vig. Who was the second founder?\n",+ "Who founded Smart Studios in Madison, while still performing drums in Spooner at night and driving a taxi cab during the day?\n"+ ]+ }+ ],+ "source": [+ "QUERY_IDX = 0\n",+ "\n",+ "# let's try OpenAI embeddings\n",+ "with dsp.settings.context(vectorizer=dsp.OpenAIVectorizer()):\n",+ " print(dsp.settings.vectorizer)\n",+ " knn_func = dsp.knn(train)\n",+ "\n",+ " knn_res_train_vec = knn_func(train[QUERY_IDX], 3)\n",+ " print(train[QUERY_IDX].question) # the query itself\n",+ " # similar questions\n",+ " print('-'*80)\n",+ " for i in [train[train.index(i)].question for i in knn_res_train_vec]:\n",+ " print(i)\n",+ "\n",+ "# the order is different from embeddings from SentenceTransformers"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 2,"metadata": {},"outputs": [],"source": []