DSPy: The framework for programming—not prompting—language models
Python
feat(dspy): added PREMAI api key recognizer from env
Anindyadeep39ca9f9b1529
diff --git a/dsp/modules/sentence_vectorizer.py b/dsp/modules/sentence_vectorizer.pyindex ffc9eba9e..e1a76b5dc 100644--- a/dsp/modules/sentence_vectorizer.py+++ b/dsp/modules/sentence_vectorizer.py@@ -1,4 +1,5 @@import abc+import osfrom typing import List, Optionalimport numpy as np@@ -6,14 +7,15 @@ import openaiclass BaseSentenceVectorizer(abc.ABC):- '''+ """Base Class for Vectorizers. The main purpose is to vectorize text (doc/query)for ANN/KNN indexes. `__call__` method takes `List[Example]` as a single input, then extracts`field_to_vectorize` from every Example and convert them into embeddings.You can customize extraction logic in the `_extract_text_from_examples` method.- '''+ """+# embeddings will be computed based on the string in this attribute of Example object- field_to_vectorize = 'text_to_vectorize'+ field_to_vectorize = "text_to_vectorize"def __init__(self) -> None:pass@@ -24,20 +26,21 @@ class BaseSentenceVectorizer(abc.ABC):def _extract_text_from_examples(self, inp_examples: List) -> List[str]:if isinstance(inp_examples[0], str):- return inp_examples+ return inp_examplesreturn [" ".join([example[key] for key in example._input_keys]) for example in inp_examples]class SentenceTransformersVectorizer(BaseSentenceVectorizer):- '''+ """Vectorizer based on `SentenceTransformers` models. You can pick any model from this link:https://huggingface.co/models?library=sentence-transformersMore details about models:https://www.sbert.net/docs/pretrained_models.html- '''+ """+def __init__(self,- model_name_or_path: str = 'all-MiniLM-L6-v2',+ model_name_or_path: str = "all-MiniLM-L6-v2",vectorize_bs: int = 256,max_gpu_devices: int = 1,normalize_embeddings: bool = False,@@ -45,7 +48,7 @@ class SentenceTransformersVectorizer(BaseSentenceVectorizer):# this isn't a good practice, but with top-level import the whole DSP# module import will be slow (>5 sec), because SentenceTransformer is doing# it's directory/file-related magic under the hood :(-+try:from sentence_transformers import SentenceTransformerexcept ImportError:@@ -55,9 +58,9 @@ class SentenceTransformersVectorizer(BaseSentenceVectorizer):"or simply run `pip install sentence-transformers",)from dsp.utils.ann_utils import determine_devices-+self.num_devices, self.is_gpu = determine_devices(max_gpu_devices)- self.proxy_device = 'cuda' if self.is_gpu else 'cpu'+ self.proxy_device = "cuda" if self.is_gpu else "cpu"self.model = SentenceTransformer(model_name_or_path, device=self.proxy_device)@@ -93,42 +96,42 @@ class SentenceTransformersVectorizer(BaseSentenceVectorizer):class NaiveGetFieldVectorizer(BaseSentenceVectorizer):- '''- If embeddings were precomputed, then we could just extract them from the proper field+ """+ If embeddings were precomputed, then we could just extract them from the proper field(set by `field_with_embedding`) from each `Example`.- '''- def __init__(self, field_with_embedding: str = 'vectorized'):+ """++ 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_embedding).reshape(1, -1)- for cur_example in inp_examples- ]+ embeddings = [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 embeddingsclass CohereVectorizer(BaseSentenceVectorizer):- '''+ """This vectorizer uses the Cohere API to convert texts to embeddings.More about the available models: https://docs.cohere.com/reference/embed`api_key` should be passed as an argument and can be retrievedfrom https://dashboard.cohere.com/api-keys- '''+ """+def __init__(self,api_key: str,- model: str = 'embed-english-v3.0',+ model: str = "embed-english-v3.0",embed_batch_size: int = 96,- embedding_type: str = 'search_document', # for details check Cohere embed docs+ embedding_type: str = "search_document", # for details check Cohere embed docs):self.model = modelself.embed_batch_size = embed_batch_sizeself.embedding_type = embedding_typeimport cohere- self.client = cohere.Client(api_key, client_name='dspy')++ self.client = cohere.Client(api_key, client_name="dspy")def __call__(self, inp_examples: List["Example"]) -> np.ndarray:text_to_vectorize = self._extract_text_from_examples(inp_examples)@@ -139,7 +142,7 @@ class CohereVectorizer(BaseSentenceVectorizer):for cur_batch_idx in range(n_batches):start_idx = cur_batch_idx * self.embed_batch_sizeend_idx = (cur_batch_idx + 1) * self.embed_batch_size- cur_batch = text_to_vectorize[start_idx: end_idx]+ cur_batch = text_to_vectorize[start_idx:end_idx]response = self.client.embed(texts=cur_batch,@@ -160,14 +163,15 @@ except Exception:class OpenAIVectorizer(BaseSentenceVectorizer):- '''+ """This vectorizer uses OpenAI API to convert texts to embeddings. Changing `model` is notrecommended. More about the model: https://openai.com/blog/new-and-improved-embedding-model/`api_key` should be passed as an argument or as env variable (`OPENAI_API_KEY`).- '''+ """+def __init__(self,- model: str = 'text-embedding-ada-002',+ model: str = "text-embedding-ada-002",embed_batch_size: int = 1024,api_key: Optional[str] = None,):@@ -191,19 +195,20 @@ class OpenAIVectorizer(BaseSentenceVectorizer):for cur_batch_idx in range(n_batches): # tqdm.tqdm?start_idx = cur_batch_idx * self.embed_batch_sizeend_idx = (cur_batch_idx + 1) * self.embed_batch_size- cur_batch = text_to_vectorize[start_idx: end_idx]+ cur_batch = text_to_vectorize[start_idx:end_idx]# OpenAI API call:response = self.Embedding.create(model=self.model,input=cur_batch,)- cur_batch_embeddings = [cur_obj['embedding'] for cur_obj in response['data']]+ 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 embeddings+class FastEmbedVectorizer(BaseSentenceVectorizer):"""Sentence vectorizer implementaion using FastEmbed - https://qdrant.github.io/fastembed."""@@ -247,4 +252,15 @@ class FastEmbedVectorizer(BaseSentenceVectorizer):texts_to_vectorize = self._extract_text_from_examples(inp_examples)embeddings = self._model.embed(texts_to_vectorize, batch_size=self._batch_size, parallel=self._parallel)- return np.array([embedding.tolist() for embedding in embeddings], dtype=np.float32)\ No newline at end of file+ return np.array([embedding.tolist() for embedding in embeddings], dtype=np.float32)+++def get_premai_api_key(api_key: Optional[str] = None) -> str:+ """Retrieve the PreMAI API key from a passed argument or environment variable."""+ api_key = api_key or os.environ.get("PREMAI_API_KEY")+ if api_key is None:+ raise RuntimeError(+ "No API key found\n",+ "See the quick start guide at https://docs.premai.io/introduction to get your API key.",+ )+ return api_key