Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Revert "run ruff linter"

Connor Shorten275f133dc9b8
diff --git a/dsp/modules/__init__.py b/dsp/modules/__init__.py
index fdf59eabc..06e1d6f07 100644
--- a/dsp/modules/__init__.py
+++ b/dsp/modules/__init__.py
@@ -1,4 +1,3 @@
-from .anthropic import Claude
from .azure_openai import AzureOpenAI
from .bedrock import *
from .cache_utils import *
@@ -14,3 +13,4 @@ from .ollama import *
from .pyserini import *
from .sbert import *
from .sentence_vectorizer import *
+from .anthropic import Claude
diff --git a/dsp/modules/anthropic.py b/dsp/modules/anthropic.py
index 1e2617551..99c9f225a 100644
--- a/dsp/modules/anthropic.py
+++ b/dsp/modules/anthropic.py
@@ -1,11 +1,12 @@
-import logging
import os
-from typing import Any, Optional
-
import backoff
+import json
+from typing import Optional, Any
from anthropic import Anthropic, RateLimitError
from dsp.modules.lm import LM
+import logging
+
logger = logging.getLogger(__name__)
@@ -22,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 False
return True
@@ -35,7 +36,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"
@@ -104,6 +105,7 @@ 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.py
index ee20e2c9a..a44e30a49 100644
--- a/dsp/modules/aws_lm.py
+++ b/dsp/modules/aws_lm.py
@@ -1,4 +1,5 @@
-"""A generalized AWS LLM.
+"""
+A generalized AWS LLM.
"""
from __future__ import annotations
@@ -16,7 +17,8 @@ CHARS2TOKENS: int = 4
class AWSLM(LM):
- """This class adds support for an AWS model
+ """
+ This class adds support for an AWS model
"""
def __init__(
@@ -32,6 +34,7 @@ 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.
@@ -98,6 +101,7 @@ class AWSLM(LM):
def basic_request(self, prompt, **kwargs) -> str | list[str]:
"""Query the endpoint."""
+
# Remove any texts that are too long
formatted_prompt: str
if self._truncate_long_prompt_prompts:
@@ -162,7 +166,8 @@ 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=False
right now.
diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.py
index b9c984395..c90f634e4 100644
--- a/dsp/modules/azure_openai.py
+++ b/dsp/modules/azure_openai.py
@@ -193,6 +193,7 @@ 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.py
index 232cd24b5..fedc59b01 100644
--- a/dsp/modules/azurecognitivesearch.py
+++ b/dsp/modules/azurecognitivesearch.py
@@ -44,16 +44,18 @@ 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 results
def 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.py
index 3afc19779..2d5839c62 100644
--- a/dsp/modules/clarifai.py
+++ b/dsp/modules/clarifai.py
@@ -11,7 +11,6 @@ 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 dspy
dspy.configure(lm=dspy.Clarifai(model=MODEL_URL,
diff --git a/dsp/modules/cohere.py b/dsp/modules/cohere.py
index 827ece89e..7308d5935 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 False
return True
@@ -42,7 +42,8 @@ class Cohere(LM):
stop_sequences: list[str] = [],
**kwargs,
):
- """Parameters
+ """
+ Parameters
----------
model : str
Which pre-trained model from Cohere to use?
diff --git a/dsp/modules/finetuning/finetune_hf.py b/dsp/modules/finetuning/finetune_hf.py
index 3ee2d0c50..a22ab10fc 100644
--- a/dsp/modules/finetuning/finetune_hf.py
+++ b/dsp/modules/finetuning/finetune_hf.py
@@ -229,7 +229,8 @@ 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)
@@ -248,7 +249,8 @@ def smart_tokenizer_and_embedding_resize(special_tokens_dict, tokenizer, model):
@dataclass
class DataCollatorForSupervisedDataset:
- """Collate examples for supervised fine-tuning.
+ """
+ Collate examples for supervised fine-tuning.
"""
tokenizer: PreTrainedTokenizer
diff --git a/dsp/modules/google.py b/dsp/modules/google.py
index 902f46d27..5d97534ea 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 False
return True
@@ -64,7 +64,8 @@ class Google(LM):
safety_settings: Optional[Iterable] = BLOCK_ONLY_HIGH,
**kwargs,
):
- """Parameters
+ """
+ Parameters
----------
model : str
Which pre-trained model from Google to use?
diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.py
index 2055b0756..d0b343af3 100644
--- a/dsp/modules/gpt3.py
+++ b/dsp/modules/gpt3.py
@@ -173,6 +173,7 @@ 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.py
index 54a0ad75a..aad0c0e36 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 models
Args:
model (str): HF model identifier to load and use
@@ -37,6 +37,7 @@ 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_client
diff --git a/dsp/modules/ollama.py b/dsp/modules/ollama.py
index a28c4a646..27304d271 100644
--- a/dsp/modules/ollama.py
+++ b/dsp/modules/ollama.py
@@ -164,6 +164,7 @@ 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.py
index fcea328f1..5523a76e7 100644
--- a/dsp/modules/pyserini.py
+++ b/dsp/modules/pyserini.py
@@ -15,18 +15,21 @@ 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 dependency
from pyserini.prebuilt_index_info import FAISS_INDEX_INFO, IMPACT_INDEX_INFO, TF_INDEX_INFO
from pyserini.search import FaissSearcher
diff --git a/dsp/modules/sentence_vectorizer.py b/dsp/modules/sentence_vectorizer.py
index 25587e4a0..b878420f7 100644
--- a/dsp/modules/sentence_vectorizer.py
+++ b/dsp/modules/sentence_vectorizer.py
@@ -6,11 +6,12 @@ import openai
class 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 object
field_to_vectorize = 'text_to_vectorize'
@@ -28,11 +29,12 @@ 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-transformers
More details about models:
https://www.sbert.net/docs/pretrained_models.html
- """
+ '''
def __init__(
self,
model_name_or_path: str = 'all-MiniLM-L6-v2',
@@ -91,9 +93,10 @@ 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
@@ -107,11 +110,12 @@ 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 retrieved
from https://dashboard.cohere.com/api-keys
- """
+ '''
def __init__(
self,
api_key: str,
@@ -156,10 +160,11 @@ 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 not
recommended. 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.py
index 99a25ee35..7dfe126b5 100644
--- a/dsp/primitives/demonstrate.py
+++ b/dsp/primitives/demonstrate.py
@@ -90,6 +90,7 @@ 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 = [
y
for y in train
@@ -126,13 +127,15 @@ 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.
"""
@@ -147,7 +150,8 @@ 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/KNN
index to search similar questions among `train` samples.
Args:
diff --git a/dsp/primitives/predict.py b/dsp/primitives/predict.py
index 5aa3420f4..41edf6c51 100644
--- a/dsp/primitives/predict.py
+++ b/dsp/primitives/predict.py
@@ -199,6 +199,7 @@ 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.py
index ea0eff94c..dcd3f09ce 100644
--- a/dsp/utils/ann_utils.py
+++ b/dsp/utils/ann_utils.py
@@ -11,7 +11,8 @@ except ImportError:
def determine_devices(max_gpu_devices: int = 0) -> Tuple[int, bool]:
- """Determine which device we should use
+ """
+ Determine which device we should use
Args:
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.
@@ -86,7 +87,8 @@ 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 training
Args:
emb_dim: size of each embedding
n_objects: size of a trainset for index. Used to determine optimal type
diff --git a/dsp/utils/dpr.py b/dsp/utils/dpr.py
index 8eaa231f4..d4d18f84f 100644
--- a/dsp/utils/dpr.py
+++ b/dsp/utils/dpr.py
@@ -1,6 +1,7 @@
-"""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
@@ -145,8 +146,9 @@ 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),
@@ -193,7 +195,8 @@ 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.py
index a3e1e91c7..1de0ada7e 100644
--- a/dsp/utils/settings.py
+++ b/dsp/utils/settings.py
@@ -10,8 +10,10 @@ class Settings:
_instance = None
def __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.py
index 69e8d812a..d5a30b0e3 100644
--- a/dsp/utils/utils.py
+++ b/dsp/utils/utils.py
@@ -47,8 +47,10 @@ 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))]
@@ -119,9 +121,11 @@ 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
@@ -163,8 +167,10 @@ 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
@@ -188,10 +194,12 @@ 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)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
diff --git a/dspy/datasets/dataset.py b/dspy/datasets/dataset.py
index 69225d3b4..c66c5edc2 100644
--- a/dspy/datasets/dataset.py
+++ b/dspy/datasets/dataset.py
@@ -56,9 +56,11 @@ 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.py
index 3f73ed020..4cb6df3f9 100644
--- a/dspy/predict/aggregation.py
+++ b/dspy/predict/aggregation.py
@@ -5,10 +5,12 @@ default_normalize = lambda s: normalize_text(s) or None
def 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.py
index 9cc00547c..86d439f50 100644
--- a/dspy/predict/langchain.py
+++ b/dspy/predict/langchain.py
@@ -17,8 +17,7 @@ from dspy.signatures.signature import infer_prefix
class 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.py
index aba9c2492..19f1aea3a 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -326,7 +326,8 @@ default_assertion_handler = backtrack_handler
def 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.py
index 9c22c799c..bee80338f 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -8,8 +8,10 @@ class BaseModule:
pass
def 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 Parameter
visited = set()
diff --git a/dspy/primitives/program.py b/dspy/primitives/program.py
index de286127f..aa499d908 100644
--- a/dspy/primitives/program.py
+++ b/dspy/primitives/program.py
@@ -55,7 +55,8 @@ class Module(BaseModule, metaclass=ProgramMeta):
return self
def 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.py
index 8264267f1..1b7456c7b 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 result
of 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.py
index f2f5aba20..07ef407d1 100644
--- a/dspy/retrieve/chromadb_rm.py
+++ b/dspy/retrieve/chromadb_rm.py
@@ -1,4 +1,5 @@
-"""Retriever model for chromadb
+"""
+Retriever model for chromadb
"""
from typing import List, Optional, Union
@@ -34,7 +35,8 @@ 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
@@ -92,6 +94,7 @@ 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.py
index 3fc132502..654234d2c 100644
--- a/dspy/retrieve/clarifai_rm.py
+++ b/dspy/retrieve/clarifai_rm.py
@@ -17,7 +17,8 @@ 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:
@@ -59,7 +60,6 @@ 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 queries
k : Top K relevant documents to return
diff --git a/dspy/retrieve/databricks_rm.py b/dspy/retrieve/databricks_rm.py
index 5fea160c7..c275bdddc 100644
--- a/dspy/retrieve/databricks_rm.py
+++ b/dspy/retrieve/databricks_rm.py
@@ -9,7 +9,8 @@ from dspy.primitives.prediction import Prediction
class 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 query
diff --git a/dspy/retrieve/deeplake_rm.py b/dspy/retrieve/deeplake_rm.py
index 176cda1fc..235108a91 100644
--- a/dspy/retrieve/deeplake_rm.py
+++ b/dspy/retrieve/deeplake_rm.py
@@ -1,4 +1,5 @@
-"""Retriever model for deeplake
+"""
+Retriever model for deeplake
"""
from collections import defaultdict
@@ -22,7 +23,9 @@ 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
@@ -78,6 +81,7 @@ 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 query
Args:
diff --git a/dspy/retrieve/marqo_rm.py b/dspy/retrieve/marqo_rm.py
index 5ec932e6c..29c52fdb4 100644
--- a/dspy/retrieve/marqo_rm.py
+++ b/dspy/retrieve/marqo_rm.py
@@ -12,7 +12,8 @@ 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 passage
diff --git a/dspy/retrieve/pgvector_rm.py b/dspy/retrieve/pgvector_rm.py
index 295461b5a..cf1773f17 100644
--- a/dspy/retrieve/pgvector_rm.py
+++ b/dspy/retrieve/pgvector_rm.py
@@ -15,7 +15,8 @@ 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
@@ -64,7 +65,8 @@ 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_client
diff --git a/dspy/retrieve/pinecone_rm.py b/dspy/retrieve/pinecone_rm.py
index 70589a0f6..0328bc6ba 100644
--- a/dspy/retrieve/pinecone_rm.py
+++ b/dspy/retrieve/pinecone_rm.py
@@ -1,4 +1,5 @@
-"""Retriever model for Pinecone
+"""
+Retriever model for Pinecone
Author: Dhar Rawal (@drawal1)
"""
@@ -33,7 +34,8 @@ 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
@@ -133,6 +135,7 @@ class PineconeRM(dspy.Retrieve):
Returns:
pinecone.Index: The loaded index.
"""
+
# Pinecone init overrides default if kwargs are present, so we need to exclude if None
kwargs = {}
if api_key:
diff --git a/dspy/retrieve/qdrant_rm.py b/dspy/retrieve/qdrant_rm.py
index 7ddaa47b7..5c2af050b 100644
--- a/dspy/retrieve/qdrant_rm.py
+++ b/dspy/retrieve/qdrant_rm.py
@@ -14,7 +14,8 @@ 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
@@ -58,7 +59,6 @@ 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.py
index 889993520..047c70d6c 100644
--- a/dspy/retrieve/vectara_rm.py
+++ b/dspy/retrieve/vectara_rm.py
@@ -15,7 +15,8 @@ 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
@@ -69,7 +70,6 @@ class VectaraRM(dspy.Retrieve):
limit: int = 3,
) -> List[str]:
"""Query Vectara index to get for top k matching passages.
-
Args:
query: query string
"""
@@ -135,7 +135,6 @@ 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.py
index 4951032aa..61e8d1ef0 100644
--- a/dspy/retrieve/weaviate_rm.py
+++ b/dspy/retrieve/weaviate_rm.py
@@ -12,7 +12,8 @@ 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
@@ -58,10 +59,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.k
queries = (
[query_or_queries]
diff --git a/dspy/retrieve/weaviate_rm_test.py b/dspy/retrieve/weaviate_rm_test.py
index d638639b2..fa2c97237 100644
--- a/dspy/retrieve/weaviate_rm_test.py
+++ b/dspy/retrieve/weaviate_rm_test.py
@@ -7,7 +7,8 @@
# Connect DSPy
# Test this API
-"""from dspy.retrieve.weaviate_rm import WeaviateRM
+"""
+from dspy.retrieve.weaviate_rm import WeaviateRM
retriever_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.py
index 8534a3645..25a62ccea 100644
--- a/dspy/retrieve/you_rm.py
+++ b/dspy/retrieve/you_rm.py
@@ -27,6 +27,7 @@ class YouRM(dspy.Retrieve):
Returns:
dspy.Prediction: An object containing the retrieved passages.
"""
+
k = k if k is not None else self.k
queries = (
diff --git a/dspy/teleprompt/ensemble.py b/dspy/teleprompt/ensemble.py
index 7ed13deb4..5e0db9bca 100644
--- a/dspy/teleprompt/ensemble.py
+++ b/dspy/teleprompt/ensemble.py
@@ -9,6 +9,7 @@ TODO: The EnsembledProgram should actually imitate the structure of the individu
class 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_fn
diff --git a/dspy/teleprompt/signature_opt.py b/dspy/teleprompt/signature_opt.py
index 47789ae9f..d71aa69a7 100644
--- a/dspy/teleprompt/signature_opt.py
+++ b/dspy/teleprompt/signature_opt.py
@@ -41,8 +41,7 @@ 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")
@@ -102,7 +101,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 = 0
diff --git a/dspy/teleprompt/signature_opt_bayesian.py b/dspy/teleprompt/signature_opt_bayesian.py
index 87fc9ad4e..e316462c9 100644
--- a/dspy/teleprompt/signature_opt_bayesian.py
+++ b/dspy/teleprompt/signature_opt_bayesian.py
@@ -58,8 +58,7 @@ 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")
@@ -70,8 +69,7 @@ class BasicGenerateInstructionWithExamples(dspy.Signature):
class 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.py
index 79d4bb84d..6e51745b3 100644
--- a/testing/tasks/biodex.py
+++ b/testing/tasks/biodex.py
@@ -365,8 +365,10 @@ 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
@@ -377,8 +379,10 @@ 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)]
@@ -391,7 +395,8 @@ 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)
@@ -403,8 +408,10 @@ 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)
@@ -415,8 +422,10 @@ 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)