DSPy: The framework for programming—not prompting—language models
Python
run ruff linter
Connor Shorten829e573f7f5c
diff --git a/dsp/modules/__init__.py b/dsp/modules/__init__.pyindex 06e1d6f07..fdf59eabc 100644--- a/dsp/modules/__init__.py+++ b/dsp/modules/__init__.py@@ -1,3 +1,4 @@+from .anthropic import Claudefrom .azure_openai import AzureOpenAIfrom .bedrock import *from .cache_utils import *@@ -13,4 +14,3 @@ from .ollama import *from .pyserini import *from .sbert import *from .sentence_vectorizer import *-from .anthropic import Claudediff --git a/dsp/modules/anthropic.py b/dsp/modules/anthropic.pyindex 99c9f225a..1e2617551 100644--- a/dsp/modules/anthropic.py+++ b/dsp/modules/anthropic.py@@ -1,12 +1,11 @@+import loggingimport os+from typing import Any, Optional+import backoff-import json-from typing import Optional, Anyfrom anthropic import Anthropic, RateLimitErrorfrom dsp.modules.lm import LM-import logging-logger = logging.getLogger(__name__)@@ -23,7 +22,7 @@ def backoff_hdlr(details):def giveup_hdlr(details):- """wrapper function that decides when to give up on retry"""+ """Wrapper function that decides when to give up on retry"""if "rate limits" in details.message:return Falsereturn True@@ -36,7 +35,7 @@ class Claude(LM):model: str = "claude-instant-1.2",api_key: Optional[str] = None,api_base: Optional[str] = None,- **kwargs+ **kwargs,):super().__init__(model)self.provider = "anthropic"@@ -105,7 +104,6 @@ class Claude(LM):Returns:list[str]: list of completion choices"""-assert only_completed, "for now"assert return_sorted is False, "for now"diff --git a/dsp/modules/aws_lm.py b/dsp/modules/aws_lm.pyindex a44e30a49..ee20e2c9a 100644--- a/dsp/modules/aws_lm.py+++ b/dsp/modules/aws_lm.py@@ -1,5 +1,4 @@-"""-A generalized AWS LLM.+"""A generalized AWS LLM."""from __future__ import annotations@@ -17,8 +16,7 @@ CHARS2TOKENS: int = 4class AWSLM(LM):- """- This class adds support for an AWS model+ """This class adds support for an AWS model"""def __init__(@@ -34,7 +32,6 @@ class AWSLM(LM):"""_summary_Args:-service_name (str): Used in context of invoking the boto3 API.region_name (str, optional): The AWS region where this LM is hosted.model (str, optional): An LM name, e.g., a bedrock name or an AWS endpoint.@@ -101,7 +98,6 @@ class AWSLM(LM):def basic_request(self, prompt, **kwargs) -> str | list[str]:"""Query the endpoint."""-# Remove any texts that are too longformatted_prompt: strif self._truncate_long_prompt_prompts:@@ -166,8 +162,7 @@ class AWSLM(LM):return_sorted: bool = False,**kwargs,) -> list[str]:- """- Query the AWS LLM.+ """Query the AWS LLM.There is only support for only_completed=True and return_sorted=Falseright now.diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.pyindex c90f634e4..b9c984395 100644--- a/dsp/modules/azure_openai.py+++ b/dsp/modules/azure_openai.py@@ -193,7 +193,6 @@ class AzureOpenAI(LM):Returns:list[dict[str, Any]]: list of completion choices"""-assert only_completed, "for now"assert return_sorted is False, "for now"diff --git a/dsp/modules/azurecognitivesearch.py b/dsp/modules/azurecognitivesearch.pyindex fedc59b01..232cd24b5 100644--- a/dsp/modules/azurecognitivesearch.py+++ b/dsp/modules/azurecognitivesearch.py@@ -44,18 +44,16 @@ class AzureCognitiveSearch:return [dotdict(psg) for psg in topk]def azure_search_request(key_content: str, key_score: str, client: SearchClient, query: str, top: int =1):- '''- Search in Azure Cognitive Search Index- '''+ """Search in Azure Cognitive Search Index+ """results = client.search(search_text=query,top=top)results = process_azure_result(results, key_content, key_content)return resultsdef process_azure_result(results:SearchItemPaged, content_key:str, content_score: str):- '''- process received result from Azure Cognitive Search as dictionary array and map content and score to correct format- '''+ """Process received result from Azure Cognitive Search as dictionary array and map content and score to correct format+ """res = []for result in results:tmp = {}diff --git a/dsp/modules/clarifai.py b/dsp/modules/clarifai.pyindex 2d5839c62..3afc19779 100644--- a/dsp/modules/clarifai.py+++ b/dsp/modules/clarifai.py@@ -11,6 +11,7 @@ class ClarifaiLLM(LM):model (str, optional): Clarifai URL of the model. Defaults to "Mistral-7B-Instruct".api_key (Optional[str], optional): CLARIFAI_PAT token. Defaults to None.**kwargs: Additional arguments to pass to the API provider.+Example:import dspydspy.configure(lm=dspy.Clarifai(model=MODEL_URL,diff --git a/dsp/modules/cohere.py b/dsp/modules/cohere.pyindex 7308d5935..827ece89e 100644--- a/dsp/modules/cohere.py+++ b/dsp/modules/cohere.py@@ -23,7 +23,7 @@ def backoff_hdlr(details):def giveup_hdlr(details):- """wrapper function that decides when to give up on retry"""+ """Wrapper function that decides when to give up on retry"""if "rate limits" in details.message:return Falsereturn True@@ -42,8 +42,7 @@ class Cohere(LM):stop_sequences: list[str] = [],**kwargs,):- """- Parameters+ """Parameters----------model : strWhich pre-trained model from Cohere to use?diff --git a/dsp/modules/finetuning/finetune_hf.py b/dsp/modules/finetuning/finetune_hf.pyindex a22ab10fc..3ee2d0c50 100644--- a/dsp/modules/finetuning/finetune_hf.py+++ b/dsp/modules/finetuning/finetune_hf.py@@ -229,8 +229,7 @@ def _train_seq2seq(model, tokenizer, tokenized_dataset, metric, config):def smart_tokenizer_and_embedding_resize(special_tokens_dict, tokenizer, model):- """- Resize tokenizer and embedding.+ """Resize tokenizer and embedding.Note: This is the unoptimized version that may make your embedding size not be divisible by 64."""num_new_tokens = tokenizer.add_special_tokens(special_tokens_dict)@@ -249,8 +248,7 @@ def smart_tokenizer_and_embedding_resize(special_tokens_dict, tokenizer, model):@dataclassclass DataCollatorForSupervisedDataset:- """- Collate examples for supervised fine-tuning.+ """Collate examples for supervised fine-tuning."""tokenizer: PreTrainedTokenizerdiff --git a/dsp/modules/google.py b/dsp/modules/google.pyindex 5d97534ea..902f46d27 100644--- a/dsp/modules/google.py+++ b/dsp/modules/google.py@@ -25,7 +25,7 @@ def backoff_hdlr(details):def giveup_hdlr(details):- """wrapper function that decides when to give up on retry"""+ """Wrapper function that decides when to give up on retry"""if "rate limits" in details.message:return Falsereturn True@@ -64,8 +64,7 @@ class Google(LM):safety_settings: Optional[Iterable] = BLOCK_ONLY_HIGH,**kwargs,):- """- Parameters+ """Parameters----------model : strWhich pre-trained model from Google to use?diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.pyindex d0b343af3..2055b0756 100644--- a/dsp/modules/gpt3.py+++ b/dsp/modules/gpt3.py@@ -173,7 +173,6 @@ class GPT3(LM):Returns:list[dict[str, Any]]: list of completion choices"""-assert only_completed, "for now"assert return_sorted is False, "for now"diff --git a/dsp/modules/hf.py b/dsp/modules/hf.pyindex aad0c0e36..54a0ad75a 100644--- a/dsp/modules/hf.py+++ b/dsp/modules/hf.py@@ -28,7 +28,7 @@ def openai_to_hf(**kwargs):class HFModel(LM):def __init__(self, model: str, checkpoint: Optional[str] = None, is_client: bool = False,hf_device_map: Literal["auto", "balanced", "balanced_low_0", "sequential"] = "auto"):- """wrapper for Hugging Face models+ """Wrapper for Hugging Face modelsArgs:model (str): HF model identifier to load and use@@ -37,7 +37,6 @@ class HFModel(LM):hf_device_map (str, optional): HF config strategy to load the model.Recommeded to use "auto", which will help loading large models using accelerate. Defaults to "auto"."""-super().__init__(model)self.provider = "hf"self.is_client = is_clientdiff --git a/dsp/modules/ollama.py b/dsp/modules/ollama.pyindex 27304d271..a28c4a646 100644--- a/dsp/modules/ollama.py+++ b/dsp/modules/ollama.py@@ -164,7 +164,6 @@ class OllamaLocal(LM):Returns:list[dict[str, Any]]: list of completion choices"""-assert only_completed, "for now"assert return_sorted is False, "for now"diff --git a/dsp/modules/pyserini.py b/dsp/modules/pyserini.pyindex 5523a76e7..fcea328f1 100644--- a/dsp/modules/pyserini.py+++ b/dsp/modules/pyserini.py@@ -15,21 +15,18 @@ class PyseriniRetriever:dataset: Dataset = None,id_field: str = '_id',text_fields: list[str] = ['text']) -> None:+ """Args:+ query_encoder (`str`):+ Huggingface model to encode queries+ index (`str`):+ Either a prebuilt index from pyserini or a local path to a faiss index+ dataset (`Dataset`):+ Only required when using a local faiss index. The dataset should be the one that has been put into the faiss index.+ id_field (`str`):+ The name of the id field of the dataset used for retrieval.+ text_fields (`list[str]`):+ A list of the names of the text fields for the dataset used for retrieval."""- Args:-- query_encoder (`str`):- Huggingface model to encode queries- index (`str`):- Either a prebuilt index from pyserini or a local path to a faiss index- dataset (`Dataset`):- Only required when using a local faiss index. The dataset should be the one that has been put into the faiss index.- id_field (`str`):- The name of the id field of the dataset used for retrieval.- text_fields (`list[str]`):- A list of the names of the text fields for the dataset used for retrieval.- """-# Keep pyserini as an optional dependencyfrom pyserini.prebuilt_index_info import FAISS_INDEX_INFO, IMPACT_INDEX_INFO, TF_INDEX_INFOfrom pyserini.search import FaissSearcherdiff --git a/dsp/modules/sentence_vectorizer.py b/dsp/modules/sentence_vectorizer.pyindex b878420f7..25587e4a0 100644--- a/dsp/modules/sentence_vectorizer.py+++ b/dsp/modules/sentence_vectorizer.py@@ -6,12 +6,11 @@ import openaiclass BaseSentenceVectorizer(abc.ABC):- '''- Base Class for Vectorizers. The main purpose is to vectorize text (doc/query)+ """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 objectfield_to_vectorize = 'text_to_vectorize'@@ -29,12 +28,11 @@ class BaseSentenceVectorizer(abc.ABC):class SentenceTransformersVectorizer(BaseSentenceVectorizer):- '''- Vectorizer based on `SentenceTransformers` models. You can pick any model from this link:+ """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',@@ -93,10 +91,9 @@ 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'):self.field_with_embedding = field_with_embedding@@ -110,12 +107,11 @@ class NaiveGetFieldVectorizer(BaseSentenceVectorizer):class CohereVectorizer(BaseSentenceVectorizer):- '''- This vectorizer uses the Cohere API to convert texts to embeddings.+ """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,@@ -160,11 +156,10 @@ except Exception:class OpenAIVectorizer(BaseSentenceVectorizer):- '''- This vectorizer uses OpenAI API to convert texts to embeddings. Changing `model` is not+ """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',diff --git a/dsp/primitives/demonstrate.py b/dsp/primitives/demonstrate.pyindex 7dfe126b5..99a25ee35 100644--- a/dsp/primitives/demonstrate.py+++ b/dsp/primitives/demonstrate.py@@ -90,7 +90,6 @@ def sample(train: list[Example], k: int):def all_but(train: list[Example], x: Example) -> list[Example]:"""Removes the example x from the train set by comparing the question and history."""-output = [yfor y in train@@ -127,15 +126,13 @@ def passage_has_answers(passage: str, answers: list[str]) -> bool:def cast_naive_get_only_question_text(inp_example: Example) -> Example:- """- Extracts question as a field to vectorize with Vectorizer object. `question` field is used.+ """Extracts question as a field to vectorize with Vectorizer object. `question` field is used."""return inp_example.copy(text_to_vectorize=inp_example.question)def cast_naive_get_question_and_answer(inp_example: Example) -> Example:- """- Extracts question and answer as fields to vectorize with Vectorizer object.+ """Extracts question and answer as fields to vectorize with Vectorizer object.`question` and `answer` fields are used. They will be concatenated with the word "Answer"between."""@@ -150,8 +147,7 @@ def knn(cast: Callable[[Example], Example] = cast_naive_get_only_question_text,**knn_args,) -> Callable[[Example, int], list[Example]]:- """- A function that vectorizes train data using `dsm.settings.vectorizer`, then build an ANN/KNN+ """A function that vectorizes train data using `dsm.settings.vectorizer`, then build an ANN/KNNindex to search similar questions among `train` samples.Args:diff --git a/dsp/primitives/predict.py b/dsp/primitives/predict.pyindex 41edf6c51..5aa3420f4 100644--- a/dsp/primitives/predict.py+++ b/dsp/primitives/predict.py@@ -199,7 +199,6 @@ def majority(def majority_vote_(completions: Completions, normalize: bool, prediction_field: str):"""Core logic for majority vote."""-if not dsp.settings.lm:raise AssertionError("No LM is loaded.")diff --git a/dsp/utils/ann_utils.py b/dsp/utils/ann_utils.pyindex dcd3f09ce..ea0eff94c 100644--- a/dsp/utils/ann_utils.py+++ b/dsp/utils/ann_utils.py@@ -11,8 +11,7 @@ except ImportError:def determine_devices(max_gpu_devices: int = 0) -> Tuple[int, bool]:- """- Determine which device we should use+ """Determine which device we should useArgs:max_gpu_devices: an integer value, define how many GPUs we'll use.-1 means all devices. 0 means there are no GPUs. Default is 0.@@ -87,8 +86,7 @@ def create_faiss_index(in_list_dist_type: str = 'L2',centroid_dist_type: str = 'L2',) -> Index:- """- Create IVF index (with IP or L2 dist), without adding data and training+ """Create IVF index (with IP or L2 dist), without adding data and trainingArgs:emb_dim: size of each embeddingn_objects: size of a trainset for index. Used to determine optimal typediff --git a/dsp/utils/dpr.py b/dsp/utils/dpr.pyindex d4d18f84f..8eaa231f4 100644--- a/dsp/utils/dpr.py+++ b/dsp/utils/dpr.py@@ -1,7 +1,6 @@-"""- Source: DPR Implementation from Facebook Research- https://github.com/facebookresearch/DPR/tree/master/dpr- Original license: https://github.com/facebookresearch/DPR/blob/main/LICENSE+"""Source: DPR Implementation from Facebook Research+https://github.com/facebookresearch/DPR/tree/master/dpr+Original license: https://github.com/facebookresearch/DPR/blob/main/LICENSE"""import unicodedata@@ -146,9 +145,8 @@ class SimpleTokenizer(Tokenizer):NON_WS = r'[^\p{Z}\p{C}]'def __init__(self, **kwargs):- """- Args:- annotators: None or empty set (only tokenizes).+ """Args:+ annotators: None or empty set (only tokenizes)."""self._regexp = regex.compile('(%s)|(%s)' % (self.ALPHA_NUM, self.NON_WS),@@ -195,8 +193,7 @@ def has_answer(tokenized_answers, text):def locate_answers(tokenized_answers, text):- """- Returns each occurrence of an answer as (offset, endpos) in terms of *characters*.+ """Returns each occurrence of an answer as (offset, endpos) in terms of *characters*."""tokenized_text = DPR_tokenize(text)occurrences = []diff --git a/dsp/utils/settings.py b/dsp/utils/settings.pyindex 1de0ada7e..a3e1e91c7 100644--- a/dsp/utils/settings.py+++ b/dsp/utils/settings.py@@ -10,10 +10,8 @@ class Settings:_instance = Nonedef __new__(cls):+ """Singleton Pattern. See https://python-patterns.guide/gang-of-four/singleton/"""- Singleton Pattern. See https://python-patterns.guide/gang-of-four/singleton/- """-if cls._instance is None:cls._instance = super().__new__(cls)cls._instance.lock = threading.Lock()diff --git a/dsp/utils/utils.py b/dsp/utils/utils.pyindex d5a30b0e3..69e8d812a 100644--- a/dsp/utils/utils.py+++ b/dsp/utils/utils.py@@ -47,10 +47,8 @@ def create_directory(path):def deduplicate(seq: list[str]) -> list[str]:+ """Source: https://stackoverflow.com/a/480227/1493011"""- Source: https://stackoverflow.com/a/480227/1493011- """-seen = set()return [x for x in seq if not (x in seen or seen.add(x))]@@ -121,11 +119,9 @@ def flatten(L):def zipstar(L, lazy=False):- """- A much faster A, B, C = zip(*[(a, b, c), (a, b, c), ...])+ """A much faster A, B, C = zip(*[(a, b, c), (a, b, c), ...])May return lists or tuples."""-if len(L) == 0:return L@@ -167,10 +163,8 @@ def groupby_first_item(lst):def process_grouped_by_first_item(lst):+ """Requires items in list to already be grouped by first item."""- Requires items in list to already be grouped by first item.- """-groups = defaultdict(list)started = False@@ -194,12 +188,10 @@ def process_grouped_by_first_item(lst):def grouper(iterable, n, fillvalue=None):+ """Collect data into fixed-length chunks or blocks+ Example: grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"+ Source: https://docs.python.org/3/library/itertools.html#itertools-recipes"""- Collect data into fixed-length chunks or blocks- Example: grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"- Source: https://docs.python.org/3/library/itertools.html#itertools-recipes- """-args = [iter(iterable)] * nreturn itertools.zip_longest(*args, fillvalue=fillvalue)diff --git a/dspy/datasets/dataset.py b/dspy/datasets/dataset.pyindex c66c5edc2..69225d3b4 100644--- a/dspy/datasets/dataset.py+++ b/dspy/datasets/dataset.py@@ -56,11 +56,9 @@ class Dataset:return self._test_def _shuffle_and_sample(self, split, data, size, seed=0):- '''- The setting (seed=s, size=N) is always a subset- of the setting (seed=s, size=M) for N < M.- '''-+ """The setting (seed=s, size=N) is always a subset+ of the setting (seed=s, size=M) for N < M.+ """data = list(data)# Shuffle the data irrespective of the requested size.diff --git a/dspy/predict/aggregation.py b/dspy/predict/aggregation.pyindex 4cb6df3f9..3f73ed020 100644--- a/dspy/predict/aggregation.py+++ b/dspy/predict/aggregation.py@@ -5,12 +5,10 @@ default_normalize = lambda s: normalize_text(s) or Nonedef majority(prediction_or_completions, normalize=default_normalize, field=None):+ """Returns the most common completion for the target field (or the last field) in the signature.+ When normalize returns None, that completion is ignored.+ In case of a tie, earlier completion are prioritized."""- Returns the most common completion for the target field (or the last field) in the signature.- When normalize returns None, that completion is ignored.- In case of a tie, earlier completion are prioritized.- """-assert any(isinstance(prediction_or_completions, t) for t in [Prediction, Completions, list])input_type = type(prediction_or_completions)diff --git a/dspy/predict/langchain.py b/dspy/predict/langchain.pyindex 86d439f50..9cc00547c 100644--- a/dspy/predict/langchain.py+++ b/dspy/predict/langchain.py@@ -17,7 +17,8 @@ from dspy.signatures.signature import infer_prefixclass Template2Signature(dspy.Signature):"""You are a processor for prompts. I will give you a prompt template (Python f-string) for an arbitrary task for other LMs.-Your job is to prepare three modular pieces: (i) any essential task instructions or guidelines, (ii) a list of variable names for inputs, (iv) the variable name for output."""+ Your job is to prepare three modular pieces: (i) any essential task instructions or guidelines, (ii) a list of variable names for inputs, (iv) the variable name for output.+ """template = dspy.InputField(format=lambda x: f"```\n\n{x.strip()}\n\n```\n\nLet's now prepare three modular pieces.")essential_instructions = dspy.OutputField()diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.pyindex 19f1aea3a..aba9c2492 100644--- a/dspy/primitives/assertions.py+++ b/dspy/primitives/assertions.py@@ -326,8 +326,7 @@ default_assertion_handler = backtrack_handlerdef assert_transform_module(module, assertion_handler=default_assertion_handler, **handler_args,):- """- Transform a module to handle assertions.+ """Transform a module to handle assertions."""if not getattr(module, "forward", False):raise ValueError(diff --git a/dspy/primitives/module.py b/dspy/primitives/module.pyindex bee80338f..9c22c799c 100644--- a/dspy/primitives/module.py+++ b/dspy/primitives/module.py@@ -8,10 +8,8 @@ class BaseModule:passdef named_parameters(self):+ """Unlike PyTorch, handles (non-recursive) lists of parameters too."""- Unlike PyTorch, handles (non-recursive) lists of parameters too.- """-from dspy.predict.parameter import Parametervisited = set()diff --git a/dspy/primitives/program.py b/dspy/primitives/program.pyindex aa499d908..de286127f 100644--- a/dspy/primitives/program.py+++ b/dspy/primitives/program.py@@ -55,8 +55,7 @@ class Module(BaseModule, metaclass=ProgramMeta):return selfdef activate_assertions(self, handler=backtrack_handler, **handler_args):- """- Activates assertions for the module.+ """Activates assertions for the module.The default handler is the backtrack_handler."""assert_transform_module(self, handler, **handler_args)diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.pyindex 1b7456c7b..8264267f1 100644--- a/dspy/primitives/python_interpreter.py+++ b/dspy/primitives/python_interpreter.py@@ -107,7 +107,7 @@ class PythonInterpreter:def execute(self, code: str, state: Optional[Dict[str, Any]] = None,fuzz_state: Optional[Dict[str, Any]] = None,keep_state: bool = True) -> Any:- r""" Execute the input python codes in a security environment.+ r"""Execute the input python codes in a security environment.Args:code (str): Generated python code to be executed.@@ -585,7 +585,7 @@ class CodePrompt(TextPrompt):represents the value of the last statement (excluding "import")in the code. This value could potentially be the desired resultof the LLM-generated code.- """+ """# NOTE: Only supports Python code for now.if not interpreter:interpreter = PythonInterpreter(action_space=globals())diff --git a/dspy/retrieve/chromadb_rm.py b/dspy/retrieve/chromadb_rm.pyindex 07ef407d1..f2f5aba20 100644--- a/dspy/retrieve/chromadb_rm.py+++ b/dspy/retrieve/chromadb_rm.py@@ -1,5 +1,4 @@-"""-Retriever model for chromadb+"""Retriever model for chromadb"""from typing import List, Optional, Union@@ -35,8 +34,7 @@ if chromadb is None:class ChromadbRM(dspy.Retrieve):- """- A retrieval module that uses chromadb to return the top passages for a given query.+ """A retrieval module that uses chromadb to return the top passages for a given query.Assumes that the chromadb index has been created and populated with the following metadata:- documents: The text of the passage@@ -94,7 +92,6 @@ class ChromadbRM(dspy.Retrieve):Returns:"""-self._chromadb_client = chromadb.Client(Settings(persist_directory=persist_directory,diff --git a/dspy/retrieve/clarifai_rm.py b/dspy/retrieve/clarifai_rm.pyindex 654234d2c..3fc132502 100644--- a/dspy/retrieve/clarifai_rm.py+++ b/dspy/retrieve/clarifai_rm.py@@ -17,8 +17,7 @@ except ImportError as err:class ClarifaiRM(dspy.Retrieve):- """- Retrieval module uses clarifai to return the Top K relevant pasages for the given query.+ """Retrieval module uses clarifai to return the Top K relevant pasages for the given query.Assuming that you have ingested the source documents into clarifai App, where it is indexed and stored.Args:@@ -60,6 +59,7 @@ class ClarifaiRM(dspy.Retrieve):self, query_or_queries: Union[str, List[str]], k: Optional[int] = None,) -> dspy.Prediction:"""Uses clarifai-python SDK search function and retrieves top_k similar passages for given query,+Args:query_or_queries : single query or list of queriesk : Top K relevant documents to returndiff --git a/dspy/retrieve/databricks_rm.py b/dspy/retrieve/databricks_rm.pyindex c275bdddc..5fea160c7 100644--- a/dspy/retrieve/databricks_rm.py+++ b/dspy/retrieve/databricks_rm.py@@ -9,8 +9,7 @@ from dspy.primitives.prediction import Predictionclass DatabricksRM(dspy.Retrieve):- """- A retrieval module that uses Databricks Vector Search Endpoint to return the top-k embeddings for a given query.+ """A retrieval module that uses Databricks Vector Search Endpoint to return the top-k embeddings for a given query.Args:databricks_index_name (str): Databricks vector search index to querydiff --git a/dspy/retrieve/deeplake_rm.py b/dspy/retrieve/deeplake_rm.pyindex 235108a91..176cda1fc 100644--- a/dspy/retrieve/deeplake_rm.py+++ b/dspy/retrieve/deeplake_rm.py@@ -1,5 +1,4 @@-"""-Retriever model for deeplake+"""Retriever model for deeplake"""from collections import defaultdict@@ -23,9 +22,7 @@ except Exception:class DeeplakeRM(dspy.Retrieve):-- """- A retriever module that uses deeplake to return the top passages for a given query.+ """A retriever module that uses deeplake to return the top passages for a given query.Assumes that a Deep Lake Vector Store has been created and populated with the following payload:- text: The text of the passage@@ -81,7 +78,6 @@ class DeeplakeRM(dspy.Retrieve):def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int],) -> dspy.Prediction:-"""Search with DeepLake for self.k top passages for queryArgs:diff --git a/dspy/retrieve/marqo_rm.py b/dspy/retrieve/marqo_rm.pyindex 29c52fdb4..5ec932e6c 100644--- a/dspy/retrieve/marqo_rm.py+++ b/dspy/retrieve/marqo_rm.py@@ -12,8 +12,7 @@ except ImportError:)class MarqoRM(dspy.Retrieve):- """- A retrieval module that uses Marqo to return the top passages for a given query.+ """A retrieval module that uses Marqo to return the top passages for a given query.Assumes that a Marqo index has been created and populated with the following payload:- document: The text of the passagediff --git a/dspy/retrieve/pgvector_rm.py b/dspy/retrieve/pgvector_rm.pyindex cf1773f17..295461b5a 100644--- a/dspy/retrieve/pgvector_rm.py+++ b/dspy/retrieve/pgvector_rm.py@@ -15,8 +15,7 @@ except ImportError:class PgVectorRM(dspy.Retrieve):- """- Implements a retriever that (as the name suggests) uses pgvector to retrieve passages,+ """Implements a retriever that (as the name suggests) uses pgvector to retrieve passages,using a raw SQL query and a postgresql connection managed by psycopg2.It needs to register the pgvector extension with the psycopg2 connection@@ -65,8 +64,7 @@ class PgVectorRM(dspy.Retrieve):embedding_field: str = "embedding",fields: List[str] = ['text'],):- """- k = 20 is the number of paragraphs to retrieve+ """K = 20 is the number of paragraphs to retrieve"""self.openai_client = openai_clientdiff --git a/dspy/retrieve/pinecone_rm.py b/dspy/retrieve/pinecone_rm.pyindex 0328bc6ba..70589a0f6 100644--- a/dspy/retrieve/pinecone_rm.py+++ b/dspy/retrieve/pinecone_rm.py@@ -1,5 +1,4 @@-"""-Retriever model for Pinecone+"""Retriever model for PineconeAuthor: Dhar Rawal (@drawal1)"""@@ -34,8 +33,7 @@ except Exception:ERRORS = (openai.RateLimitError, openai.APIError)class PineconeRM(dspy.Retrieve):- """- A retrieval module that uses Pinecone to return the top passages for a given query.+ """A retrieval module that uses Pinecone to return the top passages for a given query.Assumes that the Pinecone index has been created and populated with the following metadata:- text: The text of the passage@@ -135,7 +133,6 @@ class PineconeRM(dspy.Retrieve):Returns:pinecone.Index: The loaded index."""-# Pinecone init overrides default if kwargs are present, so we need to exclude if Nonekwargs = {}if api_key:diff --git a/dspy/retrieve/qdrant_rm.py b/dspy/retrieve/qdrant_rm.pyindex 5c2af050b..7ddaa47b7 100644--- a/dspy/retrieve/qdrant_rm.py+++ b/dspy/retrieve/qdrant_rm.py@@ -14,8 +14,7 @@ except ImportError:class QdrantRM(dspy.Retrieve):- """- A retrieval module that uses Qdrant to return the top passages for a given query.+ """A retrieval module that uses Qdrant to return the top passages for a given query.Assumes that a Qdrant collection has been created and populated with the following payload:- document: The text of the passage@@ -59,6 +58,7 @@ class QdrantRM(dspy.Retrieve):Args:query_or_queries (Union[str, List[str]]): The query or queries to search for.k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.+Returns:dspy.Prediction: An object containing the retrieved passages."""diff --git a/dspy/retrieve/vectara_rm.py b/dspy/retrieve/vectara_rm.pyindex 047c70d6c..889993520 100644--- a/dspy/retrieve/vectara_rm.py+++ b/dspy/retrieve/vectara_rm.py@@ -15,8 +15,7 @@ def remove_snippet(s: str) -> str:return s.replace(START_SNIPPET, "").replace(END_SNIPPET, "")class VectaraRM(dspy.Retrieve):- """- A retrieval module that uses Vectara to return the top passages for a given query.+ """A retrieval module that uses Vectara to return the top passages for a given query.Assumes that a Vectara corpus has been created and populated with the following payload:- document: The text of the passage@@ -70,6 +69,7 @@ class VectaraRM(dspy.Retrieve):limit: int = 3,) -> List[str]:"""Query Vectara index to get for top k matching passages.+Args:query: query string"""@@ -135,6 +135,7 @@ class VectaraRM(dspy.Retrieve):Args:query_or_queries (Union[str, List[str]]): The query or queries to search for.k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.+Returns:dspy.Prediction: An object containing the retrieved passages."""diff --git a/dspy/retrieve/weaviate_rm.py b/dspy/retrieve/weaviate_rm.pyindex 61e8d1ef0..4951032aa 100644--- a/dspy/retrieve/weaviate_rm.py+++ b/dspy/retrieve/weaviate_rm.py@@ -12,8 +12,7 @@ except ImportError:class WeaviateRM(dspy.Retrieve):- """- A retrieval module that uses Weaviate to return the top passages for a given query.+ """A retrieval module that uses Weaviate to return the top passages for a given query.Assumes that a Weaviate collection has been created and populated with the following payload:- content: The text of the passage@@ -59,10 +58,10 @@ class WeaviateRM(dspy.Retrieve):Args:query_or_queries (Union[str, List[str]]): The query or queries to search for.k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.+Returns:dspy.Prediction: An object containing the retrieved passages."""-k = k if k is not None else self.kqueries = ([query_or_queries]diff --git a/dspy/retrieve/weaviate_rm_test.py b/dspy/retrieve/weaviate_rm_test.pyindex fa2c97237..d638639b2 100644--- a/dspy/retrieve/weaviate_rm_test.py+++ b/dspy/retrieve/weaviate_rm_test.py@@ -7,8 +7,7 @@# Connect DSPy# Test this API-"""-from dspy.retrieve.weaviate_rm import WeaviateRM+"""from dspy.retrieve.weaviate_rm import WeaviateRMretriever_model = WeaviateRM("WeaviateBlogChunk", weaviate_client=weaviate_client)dspy.settings.configure(rm=retriever_model)diff --git a/dspy/retrieve/you_rm.py b/dspy/retrieve/you_rm.pyindex 25a62ccea..8534a3645 100644--- a/dspy/retrieve/you_rm.py+++ b/dspy/retrieve/you_rm.py@@ -27,7 +27,6 @@ class YouRM(dspy.Retrieve):Returns:dspy.Prediction: An object containing the retrieved passages."""-k = k if k is not None else self.kqueries = (diff --git a/dspy/teleprompt/ensemble.py b/dspy/teleprompt/ensemble.pyindex 5e0db9bca..7ed13deb4 100644--- a/dspy/teleprompt/ensemble.py+++ b/dspy/teleprompt/ensemble.py@@ -9,7 +9,6 @@ TODO: The EnsembledProgram should actually imitate the structure of the individuclass Ensemble(Teleprompter):def __init__(self, *, reduce_fn=None, size=None, deterministic=False):"""A common reduce_fn is dspy.majority."""-assert deterministic is False, "TODO: Implement example hashing for deterministic ensemble."self.reduce_fn = reduce_fndiff --git a/dspy/teleprompt/signature_opt.py b/dspy/teleprompt/signature_opt.pyindex d71aa69a7..47789ae9f 100644--- a/dspy/teleprompt/signature_opt.py+++ b/dspy/teleprompt/signature_opt.py@@ -41,7 +41,8 @@ class BasicGenerateInstruction(Signature):class GenerateInstructionGivenAttempts(dspy.Signature):"""You are an instruction optimizer for large language models. I will give some task instructions I've tried, along with their corresponding validation scores. The instructions are arranged in increasing order based on their scores, where higher scores indicate better quality.-Your task is to propose a new instruction that will lead a good language model to perform the task even better. Don't be afraid to be creative."""+ Your task is to propose a new instruction that will lead a good language model to perform the task even better. Don't be afraid to be creative.+ """attempted_instructions = dspy.InputField(format=dsp.passages2text)proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")@@ -101,7 +102,7 @@ class SignatureOptimizer(Teleprompter):def compile(self, student, *, devset, eval_kwargs):- """student is a program that needs to be optimized, note that it may be zero-shot or already pre-optimized for demos != []"""+ """Student is a program that needs to be optimized, note that it may be zero-shot or already pre-optimized for demos != []"""module = student.deepcopy()evaluate = Evaluate(devset=devset, metric=self.metric, **eval_kwargs)total_calls = 0diff --git a/dspy/teleprompt/signature_opt_bayesian.py b/dspy/teleprompt/signature_opt_bayesian.pyindex e316462c9..87fc9ad4e 100644--- a/dspy/teleprompt/signature_opt_bayesian.py+++ b/dspy/teleprompt/signature_opt_bayesian.py@@ -58,7 +58,8 @@ class BasicGenerateInstructionWithDataObservations(Signature):class BasicGenerateInstructionWithExamples(dspy.Signature):("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will also provide you with the current ``basic instruction`` that is being used for this task. I will also provide you with some ``examples`` of the expected inputs and outputs.-Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative.""")+ Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative.+ """)# attempted_instructions = dspy.InputField(format=str, desc="Previously attempted task instructions, along with their resulting validation score, and an example of the instruction in use on a sample from our dataset.")basic_instruction = dspy.InputField(desc="The initial instructions before optimization")# examples = dspy.InputField(format=dsp.passages2text, desc="Example(s) of the task")@@ -69,7 +70,8 @@ Your task is to propose an instruction that will lead a good language model to pclass BasicGenerateInstructionWithExamplesAndDataObservations(dspy.Signature):("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will also provide you with the current ``basic instruction`` that is being used for this task. I will also provide you with some ``observations`` I have made about the dataset and task, along with some ``examples`` of the expected inputs and outputs.-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")+ Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.+ """)basic_instruction = dspy.InputField(desc="The initial instructions before optimization")observations = dspy.InputField(desc="Observations about the dataset and task")examples = dspy.InputField(format=dsp.passages2text, desc="Example(s) of the task")diff --git a/testing/tasks/biodex.py b/testing/tasks/biodex.pyindex 6e51745b3..79d4bb84d 100644--- a/testing/tasks/biodex.py+++ b/testing/tasks/biodex.py@@ -365,10 +365,8 @@ def base_ground(reaction, K):@lru_cache(maxsize=100000)def ground_v1(reaction, K=3):+ """Prefers exact matches over fuzzy matches, when available."""- Prefers exact matches over fuzzy matches, when available.- """-exact_matches, fuzzy_matches = base_ground(reaction, K)matches = exact_matches[:1] or fuzzy_matches@@ -379,10 +377,8 @@ def ground_v1(reaction, K=3):@lru_cache(maxsize=100000)def ground_v2(reaction, K=1):+ """When K=1 (default), returns exact matches (if available) or the best fuzzy match."""- When K=1 (default), returns exact matches (if available) or the best fuzzy match.- """-exact_matches, fuzzy_matches = base_ground(reaction, K)matches = [(match.score / 100.0, match.node.term) for match in (exact_matches[:1] + fuzzy_matches)]@@ -395,8 +391,7 @@ ground_v3 = lambda reaction: ground_v2(reaction, K=3)@lru_cache(maxsize=100000)def ground_v4(reaction, K=3):- """- Returns the best three matches (including one exact match, if available) and applies a prior.+ """Returns the best three matches (including one exact match, if available) and applies a prior."""exact_matches, fuzzy_matches = base_ground(reaction, K)@@ -408,10 +403,8 @@ def ground_v4(reaction, K=3):@lru_cache(maxsize=100000)def ground_v4b(reaction, K=3):+ """Returns the best three matches (including one exact match, if available) and applies a prior."""- Returns the best three matches (including one exact match, if available) and applies a prior.- """-exact_matches, fuzzy_matches = base_ground(reaction, K)matches = [((match.score / 100.0) * math.log(match.node.count + 0.1), match.node.term)@@ -422,10 +415,8 @@ def ground_v4b(reaction, K=3):@lru_cache(maxsize=100000)def ground_v4c(reaction, K=3):+ """Returns the best three matches (including one exact match, if available) and applies a prior."""- Returns the best three matches (including one exact match, if available) and applies a prior.- """-exact_matches, fuzzy_matches = base_ground(reaction, K)matches = [((match.score / 100.0) * (match.node.count + 0.1), match.node.term)