DSPy: The framework for programming—not prompting—language models
Python
feat(dspy): move tests using DspyDummyLM to own folder for deprecation
Michael Jonesb0d4f2c0cdd8
diff --git a/tests/dsp_LM/__init__.py b/tests/dsp_LM/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/tests/dsp_LM/evaluate/test_evaluate.py b/tests/dsp_LM/evaluate/test_evaluate.pynew file mode 100644index 000000000..5c6c1f82e--- /dev/null+++ b/tests/dsp_LM/evaluate/test_evaluate.py@@ -0,0 +1,100 @@+import signal+import threading+from unittest.mock import patch++import pytest++import dsp+import dspy+from dspy.evaluate.evaluate import Evaluate+from dspy.evaluate.metrics import answer_exact_match+from dspy.predict import Predict+from dspy.utils.dummies import DSPDummyLM+++def new_example(question, answer):+ """Helper function to create a new example."""+ return dspy.Example(+ question=question,+ answer=answer,+ ).with_inputs("question")+++def test_evaluate_call():+ dspy.settings.configure(lm=DSPDummyLM({"What is 1+1?": "2", "What is 2+2?": "4"}))+ devset = [new_example("What is 1+1?", "2"), new_example("What is 2+2?", "4")]+ program = Predict("question -> answer")+ assert program(question="What is 1+1?").answer == "2"+ ev = Evaluate(+ devset=devset,+ metric=answer_exact_match,+ display_progress=False,+ )+ score = ev(program)+ assert score == 100.0+++def test_multithread_evaluate_call():+ dspy.settings.configure(lm=DSPDummyLM({"What is 1+1?": "2", "What is 2+2?": "4"}))+ devset = [new_example("What is 1+1?", "2"), new_example("What is 2+2?", "4")]+ program = Predict("question -> answer")+ assert program(question="What is 1+1?").answer == "2"+ ev = Evaluate(+ devset=devset,+ metric=answer_exact_match,+ display_progress=False,+ num_threads=2,+ )+ score = ev(program)+ assert score == 100.0+++def test_multi_thread_evaluate_call_cancelled(monkeypatch):+ # slow LM that sleeps for 1 second before returning the answer+ class SlowLM(DSPDummyLM):+ def __call__(self, prompt, **kwargs):+ import time++ time.sleep(1)+ return super().__call__(prompt, **kwargs)++ dspy.settings.configure(lm=SlowLM({"What is 1+1?": "2", "What is 2+2?": "4"}))++ devset = [new_example("What is 1+1?", "2"), new_example("What is 2+2?", "4")]+ program = Predict("question -> answer")+ assert program(question="What is 1+1?").answer == "2"++ # spawn a thread that will sleep for .1 seconds then send a KeyboardInterrupt+ def sleep_then_interrupt():+ import time++ time.sleep(0.1)+ import os++ os.kill(os.getpid(), signal.SIGINT)++ input_thread = threading.Thread(target=sleep_then_interrupt)+ input_thread.start()++ with pytest.raises(KeyboardInterrupt):+ ev = Evaluate(+ devset=devset,+ metric=answer_exact_match,+ display_progress=False,+ num_threads=2,+ )+ score = ev(program)+ assert score == 100.0+++def test_evaluate_call_bad():+ dspy.settings.configure(lm=DSPDummyLM({"What is 1+1?": "0", "What is 2+2?": "0"}))+ devset = [new_example("What is 1+1?", "2"), new_example("What is 2+2?", "4")]+ program = Predict("question -> answer")+ ev = Evaluate(+ devset=devset,+ metric=answer_exact_match,+ display_progress=False,+ )+ score = ev(program)+ assert score == 0.0diff --git a/tests/dsp_LM/evaluate/test_metrics.py b/tests/dsp_LM/evaluate/test_metrics.pynew file mode 100644index 000000000..04e32e68c--- /dev/null+++ b/tests/dsp_LM/evaluate/test_metrics.py@@ -0,0 +1,36 @@+# FILEPATH: /Users/ahle/repos/dspy/tests/evaluate/test_metrics.py++import dsp+import dspy+from dspy.evaluate.metrics import answer_exact_match+from dspy.predict import Predict+++def test_answer_exact_match_string():+ example = dspy.Example(+ question="What is 1+1?",+ answer="2",+ ).with_inputs("question")+ pred = Predict("question -> answer")+ pred.answer = "2"+ assert answer_exact_match(example, pred)+++def test_answer_exact_match_list():+ example = dspy.Example(+ question="What is 1+1?",+ answer=["2", "two"],+ ).with_inputs("question")+ pred = Predict("question -> answer")+ pred.answer = "2"+ assert answer_exact_match(example, pred)+++def test_answer_exact_match_no_match():+ example = dspy.Example(+ question="What is 1+1?",+ answer="2",+ ).with_inputs("question")+ pred = Predict("question -> answer")+ pred.answer = "3"+ assert not answer_exact_match(example, pred)diff --git a/tests/dsp_LM/examples/test_baleen.py b/tests/dsp_LM/examples/test_baleen.pynew file mode 100644index 000000000..f0b804269--- /dev/null+++ b/tests/dsp_LM/examples/test_baleen.py@@ -0,0 +1,122 @@+import pytest++import dspy+import dspy.evaluate+from dsp.utils import deduplicate+from dspy.datasets import HotPotQA+from dspy.evaluate.evaluate import Evaluate+from dspy.teleprompt.bootstrap import BootstrapFewShot+++class GenerateAnswer(dspy.Signature):+ """Answer questions with short factoid answers."""++ context = dspy.InputField(desc="may contain relevant facts")+ question = dspy.InputField()+ answer = dspy.OutputField(desc="often between 1 and 5 words")+++class GenerateSearchQuery(dspy.Signature):+ """Write a simple search query that will help answer a complex question."""++ context = dspy.InputField(desc="may contain relevant facts")+ question = dspy.InputField()+ query = dspy.OutputField()+++class SimplifiedBaleen(dspy.Module):+ def __init__(self, passages_per_hop=3, max_hops=2):+ super().__init__()++ self.generate_query = [dspy.ChainOfThought(GenerateSearchQuery) for _ in range(max_hops)]+ self.retrieve = dspy.Retrieve(k=passages_per_hop)+ self.generate_answer = dspy.ChainOfThought(GenerateAnswer)+ self.max_hops = max_hops++ def forward(self, question):+ context = []++ for hop in range(self.max_hops):+ query = self.generate_query[hop](context=context, question=question).query+ passages = self.retrieve(query).passages+ context = deduplicate(context + passages)++ pred = self.generate_answer(context=context, question=question)+ return dspy.Prediction(context=context, answer=pred.answer)+++def load_hotpotqa():+ # Load the dataset.+ dataset = HotPotQA(train_seed=1, train_size=20, eval_seed=2023, dev_size=50, test_size=0)+ # Tell DSPy that the 'question' field is the input. Any other fields are labels and/or metadata.+ trainset = [x.with_inputs("question") for x in dataset.train]+ devset = [x.with_inputs("question") for x in dataset.dev]+ return trainset, devset+++# @pytest.mark.slow_test+# TODO: Find a way to make this test run without openai+def _test_baleen():+ lm = dspy.OpenAI(model="gpt-3.5-turbo")+ rm = dspy.ColBERTv2(url="http://20.102.90.50:2017/wiki17_abstracts")+ dspy.settings.configure(lm=lm, rm=rm)++ # Ask any question you like to this simple RAG program.+ my_question = "How many storeys are in the castle that David Gregory inherited?"++ # Get the prediction. This contains `pred.context` and `pred.answer`.+ uncompiled_baleen = SimplifiedBaleen() # uncompiled (i.e., zero-shot) program+ pred = uncompiled_baleen(my_question)++ assert pred.answer == "five"+++def validate_context_and_answer_and_hops(example, pred, trace=None):+ if not dspy.evaluate.answer_exact_match(example, pred):+ return False+ if not dspy.evaluate.answer_passage_match(example, pred):+ return False++ hops = [example.question] + [outputs.query for *_, outputs in trace if "query" in outputs]++ if max([len(h) for h in hops]) > 100:+ return False+ if any(dspy.evaluate.answer_exact_match_str(hops[idx], hops[:idx], frac=0.8) for idx in range(2, len(hops))):+ return False++ return True+++def gold_passages_retrieved(example, pred, trace=None):+ gold_titles = set(map(dspy.evaluate.normalize_text, example["gold_titles"]))+ found_titles = set(map(dspy.evaluate.normalize_text, [c.split(" | ")[0] for c in pred.context]))++ return gold_titles.issubset(found_titles)+++# @pytest.mark.slow_test+# TODO: Find a way to make this test run without the slow hotpotqa dataset+def _test_compiled_baleen():+ trainset, devset = load_hotpotqa()+ lm = dspy.OpenAI(model="gpt-3.5-turbo")+ rm = dspy.ColBERTv2(url="http://20.102.90.50:2017/wiki17_abstracts")+ dspy.settings.configure(lm=lm, rm=rm)++ uncompiled_baleen = SimplifiedBaleen() # uncompiled (i.e., zero-shot) program++ teleprompter = BootstrapFewShot(metric=validate_context_and_answer_and_hops)+ compiled_baleen = teleprompter.compile(+ SimplifiedBaleen(),+ teacher=SimplifiedBaleen(passages_per_hop=2),+ trainset=trainset,+ )++ evaluate_on_hotpotqa = Evaluate(devset=devset, num_threads=1, display_progress=True, display_table=5)+ uncompiled_baleen_retrieval_score = evaluate_on_hotpotqa(+ uncompiled_baleen, metric=gold_passages_retrieved, display=False+ )+ # assert uncompiled_baleen_retrieval_score / 100 == 18 / 50++ compiled_baleen_retrieval_score = evaluate_on_hotpotqa(compiled_baleen, metric=gold_passages_retrieved)+ # assert compiled_baleen_retrieval_score / 100 == 27 / 50+ assert uncompiled_baleen_retrieval_score < compiled_baleen_retrieval_scorediff --git a/tests/dsp_LM/functional/test_functional.py b/tests/dsp_LM/functional/test_functional.pynew file mode 100644index 000000000..bc844a82c--- /dev/null+++ b/tests/dsp_LM/functional/test_functional.py@@ -0,0 +1,906 @@+import datetime+import textwrap+from typing import Annotated, Any, Generic, List, Literal, Optional, TypeVar++import pydantic+import pytest+from pydantic import AfterValidator, BaseModel, Field, ValidationError, field_validator, model_validator++import dspy+from dspy.functional import FunctionalModule, TypedChainOfThought, TypedPredictor, cot, predictor+from dspy.predict.predict import Predict+from dspy.primitives.example import Example+from dspy.teleprompt.bootstrap import BootstrapFewShot+from dspy.teleprompt.vanilla import LabeledFewShot+from dspy.utils.dummies import DSPDummyLM+++def test_simple():+ @predictor+ def hard_question(topic: str) -> str:+ """Think of a hard factual question about a topic."""++ expected = "What is the speed of light?"+ lm = DSPDummyLM([expected])+ dspy.settings.configure(lm=lm)++ question = hard_question(topic="Physics")+ lm.inspect_history(n=2)++ assert question == expected+++def test_list_output():+ @predictor+ def hard_questions(topics: List[str]) -> List[str]:+ pass++ expected = ["What is the speed of light?", "What is the speed of sound?"]+ lm = DSPDummyLM(['["What is the speed of light?", "What is the speed of sound?"]'])+ dspy.settings.configure(lm=lm)++ question = hard_questions(topics=["Physics", "Music"])+ lm.inspect_history(n=2)++ assert question == expected+++def test_simple_type():+ class Question(pydantic.BaseModel):+ value: str++ @predictor+ def hard_question(topic: str) -> Question:+ """Think of a hard factual question about a topic."""++ expected = "What is the speed of light?"+ lm = DSPDummyLM([f'{{"value": "{expected}"}}'])+ dspy.settings.configure(lm=lm)++ question = hard_question(topic="Physics")++ assert isinstance(question, Question)+ assert question.value == expected+++def test_simple_type_input():+ class Question(pydantic.BaseModel):+ value: str++ class Answer(pydantic.BaseModel):+ value: str++ @predictor+ def answer(question: Question) -> Answer:+ pass++ question = Question(value="What is the speed of light?")+ lm = DSPDummyLM([f'{{"value": "3e8"}}'])+ dspy.settings.configure(lm=lm)++ result = answer(question=question)++ assert result == Answer(value="3e8")+++def test_simple_class():+ class Answer(pydantic.BaseModel):+ value: float+ certainty: float+ comments: List[str] = pydantic.Field(description="At least two comments about the answer")++ class QA(FunctionalModule):+ @predictor+ def hard_question(self, topic: str) -> str:+ """Think of a hard factual question about a topic. It should be answerable with a number."""++ @cot+ def answer(self, question: Annotated[str, "Question to answer"]) -> Answer:+ pass++ def forward(self, **kwargs):+ question = self.hard_question(**kwargs)+ return (question, self.answer(question=question))++ expected = Answer(+ value=3e8,+ certainty=0.9,+ comments=["It is the speed of light", "It is a constant"],+ )++ lm = DSPDummyLM(+ [+ "What is the speed of light?",+ "Some bad reasoning, 3e8 m/s.",+ "3e8", # Bad answer 1+ "{...}", # Model is asked to create an example+ "Some good reasoning...",+ expected.model_dump_json(), # Good answer+ ]+ )+ dspy.settings.configure(lm=lm)++ qa = QA()+ assert isinstance(qa, FunctionalModule)+ assert isinstance(qa.answer, dspy.Module)++ question, answer = qa(topic="Physics")++ print(qa.answer)++ assert question == "What is the speed of light?"+ assert answer == expected+++def test_simple_oop():+ class Question(pydantic.BaseModel):+ value: str++ class MySignature(dspy.Signature):+ topic: str = dspy.InputField()+ output: Question = dspy.OutputField()++ # Run the signature+ program = TypedPredictor(MySignature)+ expected = "What is the speed of light?"+ lm = DSPDummyLM(+ [+ Question(value=expected).model_dump_json(),+ ]+ )+ dspy.settings.configure(lm=lm)++ question = program(topic="Physics").output++ assert isinstance(question, Question)+ assert question.value == expected+++def test_bootstrap_effectiveness():+ class SimpleModule(FunctionalModule):+ @predictor+ def output(self, input: str) -> str:+ pass++ def forward(self, **kwargs):+ return self.output(**kwargs)++ def simple_metric(example, prediction, trace=None):+ return example.output == prediction.output++ examples = [+ ex.with_inputs("input")+ for ex in (+ Example(input="What is the color of the sky?", output="blue"),+ Example(+ input="What does the fox say?",+ output="Ring-ding-ding-ding-dingeringeding!",+ ),+ )+ ]+ trainset = [examples[0]]+ valset = [examples[1]]++ # This test verifies if the bootstrapping process improves the student's predictions+ student = SimpleModule()+ teacher = SimpleModule()+ assert student.output.predictor.signature.equals(teacher.output.predictor.signature)++ lm = DSPDummyLM(["blue", "Ring-ding-ding-ding-dingeringeding!"], follow_examples=True)+ dspy.settings.configure(lm=lm, trace=[])++ bootstrap = BootstrapFewShot(metric=simple_metric, max_bootstrapped_demos=1, max_labeled_demos=1)+ compiled_student = bootstrap.compile(student, teacher=teacher, trainset=trainset)++ lm.inspect_history(n=2)++ # Check that the compiled student has the correct demos+ _, predict = next(compiled_student.named_sub_modules(Predict, skip_compiled=False))+ demos = predict.demos+ assert len(demos) == 1+ assert demos[0].input == trainset[0].input+ assert demos[0].output == trainset[0].output++ # Test the compiled student's prediction.+ # We are using a DspDummyLM with follow_examples=True, which means that+ # even though it would normally reply with "Ring-ding-ding-ding-dingeringeding!"+ # on the second output, if it seems an example that perfectly matches the+ # prompt, it will use that instead. That is why we expect "blue" here.+ prediction = compiled_student(input=trainset[0].input)+ assert prediction == trainset[0].output++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `input`, produce the fields `output`.++ ---++ Follow the following format.++ Input: ${input}+ Output: ${output}++ ---++ Input: What is the color of the sky?+ Output: blue++ ---++ Input: What is the color of the sky?+ Output: blue"""+ )+++def test_regex():+ class TravelInformation(BaseModel):+ origin: str = Field(pattern=r"^[A-Z]{3}$")+ destination: str = Field(pattern=r"^[A-Z]{3}$")+ date: datetime.date++ @predictor+ def flight_information(email: str) -> TravelInformation:+ pass++ email = textwrap.dedent(+ """\+ We're excited to welcome you aboard your upcoming flight from+ John F. Kennedy International Airport (JFK) to Los Angeles International Airport (LAX)+ on December 25, 2022. Here's everything you need to know before you take off: ...+ """+ )+ lm = DSPDummyLM(+ [+ # Example with a bad origin code.+ '{"origin": "JF0", "destination": "LAX", "date": "2022-12-25"}',+ # Example to help the model understand+ "{...}",+ # Fixed+ '{"origin": "JFK", "destination": "LAX", "date": "2022-12-25"}',+ ]+ )+ dspy.settings.configure(lm=lm)++ assert flight_information(email=email) == TravelInformation(+ origin="JFK", destination="LAX", date=datetime.date(2022, 12, 25)+ )+++def test_custom_model_validate_json():+ class Airport(BaseModel):+ code: str = Field(pattern=r"^[A-Z]{3}$")+ lat: float+ lon: float++ class TravelInformation(BaseModel):+ origin: Airport+ destination: Airport+ date: datetime.date++ @classmethod+ def model_validate_json(+ cls, json_data: str, *, strict: Optional[bool] = None, context: Optional[dict[str, Any]] = None+ ) -> "TravelInformation":+ try:+ __tracebackhide__ = True+ return cls.__pydantic_validator__.validate_json(json_data, strict=strict, context=context)+ except ValidationError:+ for substring_length in range(len(json_data), 1, -1):+ for start in range(len(json_data) - substring_length + 1):+ substring = json_data[start : start + substring_length]+ try:+ __tracebackhide__ = True+ res = cls.__pydantic_validator__.validate_json(substring, strict=strict, context=context)+ return res+ except ValidationError as exc:+ last_exc = exc+ pass+ raise ValueError("Could not find valid json") from last_exc++ @predictor+ def flight_information(email: str) -> TravelInformation:+ pass++ email = textwrap.dedent(+ """\+ We're excited to welcome you aboard your upcoming flight from+ John F. Kennedy International Airport (JFK) to Los Angeles International Airport (LAX)+ on December 25, 2022. Here's everything you need to know before you take off: ...+ """+ )+ lm = DSPDummyLM(+ [+ # Example with a bad origin code.+ (+ "Here is your json: "+ "{"+ '"origin": {"code":"JFK", "lat":40.6446, "lon":-73.7797}, '+ '"destination": {"code":"LAX", "lat":33.942791, "lon":-118.410042}, '+ '"date": "2022-12-25"}'+ ),+ ]+ )+ dspy.settings.configure(lm=lm)++ assert flight_information(email=email) == TravelInformation(+ origin={"code": "JFK", "lat": 40.6446, "lon": -73.7797},+ destination={"code": "LAX", "lat": 33.942791, "lon": -118.410042},+ date=datetime.date(2022, 12, 25),+ )+++def test_raises():+ class TravelInformation(BaseModel):+ origin: str = Field(pattern=r"^[A-Z]{3}$")+ destination: str = Field(pattern=r"^[A-Z]{3}$")+ date: datetime.date++ @predictor+ def flight_information(email: str) -> TravelInformation:+ pass++ lm = DSPDummyLM(+ [+ "A list of bad inputs",+ '{"origin": "JF0", "destination": "LAX", "date": "2022-12-25"}',+ '{"origin": "JFK", "destination": "LAX", "date": "bad date"}',+ ]+ )+ dspy.settings.configure(lm=lm)++ with pytest.raises(ValueError):+ flight_information(email="Some email")+++def test_multi_errors():+ class TravelInformation(BaseModel):+ origin: str = Field(pattern=r"^[A-Z]{3}$")+ destination: str = Field(pattern=r"^[A-Z]{3}$")+ date: datetime.date++ @predictor+ def flight_information(email: str) -> TravelInformation:+ pass++ lm = DSPDummyLM(+ [+ # First origin is wrong, then destination, then all is good+ '{"origin": "JF0", "destination": "LAX", "date": "2022-12-25"}',+ "{...}", # Example to help the model understand+ '{"origin": "JFK", "destination": "LA0", "date": "2022-12-25"}',+ "{...}", # Example to help the model understand+ '{"origin": "JFK", "destination": "LAX", "date": "2022-12-25"}',+ ]+ )+ dspy.settings.configure(lm=lm)++ assert flight_information(email="Some email") == TravelInformation(+ origin="JFK", destination="LAX", date=datetime.date(2022, 12, 25)+ )+ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `email`, produce the fields `flight_information`.++ ---++ Follow the following format.++ Email: ${email}++ Past Error in Flight Information: An error to avoid in the future++ Past Error (2) in Flight Information: An error to avoid in the future++ Flight Information: ${flight_information}. Respond with a single JSON object. JSON Schema: {"properties": {"origin": {"pattern": "^[A-Z]{3}$", "title": "Origin", "type": "string"}, "destination": {"pattern": "^[A-Z]{3}$", "title": "Destination", "type": "string"}, "date": {"format": "date", "title": "Date", "type": "string"}}, "required": ["origin", "destination", "date"], "title": "TravelInformation", "type": "object"}++ ---++ Email: Some email++ Past Error in Flight Information: String should match pattern '^[A-Z]{3}$': origin (error type: string_pattern_mismatch)++ Past Error (2) in Flight Information: String should match pattern '^[A-Z]{3}$': destination (error type: string_pattern_mismatch)++ Flight Information: {"origin": "JFK", "destination": "LAX", "date": "2022-12-25"}"""+ )+++def test_field_validator():+ class UserDetails(BaseModel):+ name: str+ age: int++ @field_validator("name")+ @classmethod+ def validate_name(cls, v):+ if v.upper() != v:+ raise ValueError("Name must be in uppercase.")+ return v++ @predictor+ def get_user_details() -> UserDetails:+ pass++ # Keep making the mistake (lower case name) until we run+ # out of retries.+ lm = DSPDummyLM(+ [+ '{"name": "lower case name", "age": 25}',+ ]+ * 10+ )+ dspy.settings.configure(lm=lm)++ with pytest.raises(ValueError):+ get_user_details()++ print(lm.get_convo(-1))+ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields , produce the fields `get_user_details`.++ ---++ Follow the following format.++ Past Error in Get User Details: An error to avoid in the future+ Past Error (2) in Get User Details: An error to avoid in the future+ Get User Details: ${get_user_details}. Respond with a single JSON object. JSON Schema: {"properties": {"name": {"title": "Name", "type": "string"}, "age": {"title": "Age", "type": "integer"}}, "required": ["name", "age"], "title": "UserDetails", "type": "object"}++ ---++ Past Error in Get User Details: Value error, Name must be in uppercase.: name (error type: value_error)+ Past Error (2) in Get User Details: Value error, Name must be in uppercase.: name (error type: value_error)+ Get User Details: {"name": "lower case name", "age": 25}"""+ )+++def test_annotated_field():+ @predictor+ def test(input: Annotated[str, Field(description="description")]) -> Annotated[float, Field(gt=0, lt=1)]:+ pass++ # First try 0, which fails, then try 0.5, which passes+ lm = DSPDummyLM(["0", "0.5"])+ dspy.settings.configure(lm=lm)++ output = test(input="input")++ assert output == 0.5+++def test_multiple_outputs():+ lm = DSPDummyLM([str(i) for i in range(100)])+ dspy.settings.configure(lm=lm)++ test = TypedPredictor("input -> output")+ output = test(input="input", config=dict(n=3)).completions.output+ assert output == ["0", "1", "2"]+++def test_multiple_outputs_int():+ lm = DSPDummyLM([str(i) for i in range(100)])+ dspy.settings.configure(lm=lm)++ class TestSignature(dspy.Signature):+ input: int = dspy.InputField()+ output: int = dspy.OutputField()++ test = TypedPredictor(TestSignature)++ output = test(input=8, config=dict(n=3)).completions.output+ assert output == [0, 1, 2]+++def test_multiple_outputs_int_cot():+ # Note: Multiple outputs only work when the language model "speculatively" generates all the outputs in one go.+ lm = DSPDummyLM(+ [+ "thoughts 0\nOutput: 0\n",+ "thoughts 1\nOutput: 1\n",+ "thoughts 2\nOutput: 2\n",+ ]+ )+ dspy.settings.configure(lm=lm)++ test = TypedChainOfThought("input:str -> output:int")++ output = test(input="8", config=dict(n=3)).completions.output+ assert output == [0, 1, 2]+++def test_parse_type_string():+ lm = DSPDummyLM([str(i) for i in range(100)])+ dspy.settings.configure(lm=lm)++ test = TypedPredictor("input:int -> output:int")++ output = test(input=8, config=dict(n=3)).completions.output+ assert output == [0, 1, 2]+++def test_literal():+ lm = DSPDummyLM(['"2"', '"3"'])+ dspy.settings.configure(lm=lm)++ @predictor+ def f() -> Literal["2", "3"]:+ pass++ assert f() == "2"+++def test_literal_missmatch():+ lm = DSPDummyLM([f'"{i}"' for i in range(5, 100)])+ dspy.settings.configure(lm=lm)++ @predictor(max_retries=1)+ def f() -> Literal["2", "3"]:+ pass++ with pytest.raises(Exception) as e_info:+ f()++ assert e_info.value.args[1]["f"] == "Input should be '2' or '3': (error type: literal_error)"+++def test_literal_int():+ lm = DSPDummyLM(["2", "3"])+ dspy.settings.configure(lm=lm)++ @predictor+ def f() -> Literal[2, 3]:+ pass++ assert f() == 2+++def test_literal_int_missmatch():+ lm = DSPDummyLM([f"{i}" for i in range(5, 100)])+ dspy.settings.configure(lm=lm)++ @predictor(max_retries=1)+ def f() -> Literal[2, 3]:+ pass++ with pytest.raises(Exception) as e_info:+ f()++ assert e_info.value.args[1]["f"] == "Input should be 2 or 3: (error type: literal_error)"+++def test_fields_on_base_signature():+ class SimpleOutput(dspy.Signature):+ output: float = dspy.OutputField(gt=0, lt=1)++ lm = DSPDummyLM(+ [+ "2.1", # Bad output+ "0.5", # Good output+ ]+ )+ dspy.settings.configure(lm=lm)++ predictor = TypedPredictor(SimpleOutput)++ assert predictor().output == 0.5+++def test_synthetic_data_gen():+ class SyntheticFact(BaseModel):+ fact: str = Field(..., description="a statement")+ varacity: bool = Field(..., description="is the statement true or false")++ class ExampleSignature(dspy.Signature):+ """Generate an example of a synthetic fact."""++ fact: SyntheticFact = dspy.OutputField()++ lm = DSPDummyLM(+ [+ '{"fact": "The sky is blue", "varacity": true}',+ '{"fact": "The sky is green", "varacity": false}',+ '{"fact": "The sky is red", "varacity": true}',+ '{"fact": "The earth is flat", "varacity": false}',+ '{"fact": "The earth is round", "varacity": true}',+ '{"fact": "The earth is a cube", "varacity": false}',+ ]+ )+ dspy.settings.configure(lm=lm)++ generator = TypedPredictor(ExampleSignature)+ examples = generator(config=dict(n=3))+ for ex in examples.completions.fact:+ assert isinstance(ex, SyntheticFact)+ assert examples.completions.fact[0] == SyntheticFact(fact="The sky is blue", varacity=True)++ # If you have examples and want more+ existing_examples = [+ dspy.Example(fact="The sky is blue", varacity=True),+ dspy.Example(fact="The sky is green", varacity=False),+ ]+ trained = LabeledFewShot().compile(student=generator, trainset=existing_examples)++ augmented_examples = trained(config=dict(n=3))+ for ex in augmented_examples.completions.fact:+ assert isinstance(ex, SyntheticFact)+++def test_list_input2():+ # Inspired by the Signature Optimizer++ class ScoredString(pydantic.BaseModel):+ string: str+ score: float++ class ScoredSignature(dspy.Signature):+ attempted_signatures: list[ScoredString] = dspy.InputField()+ proposed_signature: str = dspy.OutputField()++ program = TypedChainOfThought(ScoredSignature)++ lm = DSPDummyLM(["Thoughts", "Output"])+ dspy.settings.configure(lm=lm)++ output = program(+ attempted_signatures=[+ ScoredString(string="string 1", score=0.5),+ ScoredString(string="string 2", score=0.4),+ ScoredString(string="string 3", score=0.3),+ ]+ ).proposed_signature++ print(lm.get_convo(-1))++ assert output == "Output"++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `attempted_signatures`, produce the fields `proposed_signature`.++ ---++ Follow the following format.++ Attempted Signatures: ${attempted_signatures}+ Reasoning: Let's think step by step in order to ${produce the proposed_signature}. We ...+ Proposed Signature: ${proposed_signature}++ ---++ Attempted Signatures: [{"string":"string 1","score":0.5},{"string":"string 2","score":0.4},{"string":"string 3","score":0.3}]+ Reasoning: Let's think step by step in order to Thoughts+ Proposed Signature: Output"""+ )+++def test_custom_reasoning_field():+ class Question(pydantic.BaseModel):+ value: str++ class QuestionSignature(dspy.Signature):+ topic: str = dspy.InputField()+ question: Question = dspy.OutputField()++ reasoning = dspy.OutputField(+ prefix="Custom Reasoning: Let's break this down. To generate a question about",+ desc="${topic}, we should ...",+ )++ program = TypedChainOfThought(QuestionSignature, reasoning=reasoning)++ expected = "What is the speed of light?"+ lm = DSPDummyLM(["Thoughts", f'{{"value": "{expected}"}}'])+ dspy.settings.configure(lm=lm)++ output = program(topic="Physics")++ assert isinstance(output.question, Question)+ assert output.question.value == expected++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `topic`, produce the fields `question`.++ ---++ Follow the following format.++ Topic: ${topic}+ Custom Reasoning: Let's break this down. To generate a question about ${topic}, we should ...+ Question: ${question}. Respond with a single JSON object. JSON Schema: {"properties": {"value": {"title": "Value", "type": "string"}}, "required": ["value"], "title": "Question", "type": "object"}++ ---++ Topic: Physics+ Custom Reasoning: Let's break this down. To generate a question about Thoughts+ Question: {"value": "What is the speed of light?"}"""+ )+++def test_generic_signature():+ T = TypeVar("T")++ class GenericSignature(dspy.Signature, Generic[T]):+ """My signature"""++ output: T = dspy.OutputField()++ predictor = TypedPredictor(GenericSignature[int])+ assert predictor.signature.instructions == "My signature"++ lm = DSPDummyLM(["23"])+ dspy.settings.configure(lm=lm)++ assert predictor().output == 23+++def test_lm_as_validator():+ @predictor+ def is_square(n: int) -> bool:+ """Is n a square number?"""++ def check_square(n):+ assert is_square(n=n)+ return n++ @predictor+ def next_square(n: int) -> Annotated[int, AfterValidator(check_square)]:+ """What is the next square number after n?"""++ lm = DSPDummyLM(["3", "False", "4", "True"])+ dspy.settings.configure(lm=lm)++ m = next_square(n=2)+ lm.inspect_history(n=2)++ assert m == 4+++def test_annotated_validator():+ def is_square(n: int) -> int:+ root = n**0.5+ if not root.is_integer():+ raise ValueError(f"{n} is not a square")+ return n++ class MySignature(dspy.Signature):+ """What is the next square number after n?"""++ n: int = dspy.InputField()+ next_square: Annotated[int, AfterValidator(is_square)] = dspy.OutputField()++ lm = DSPDummyLM(["3", "4"])+ dspy.settings.configure(lm=lm)++ m = TypedPredictor(MySignature)(n=2).next_square+ lm.inspect_history(n=2)++ assert m == 4+++def test_annotated_validator_functional():+ def is_square(n: int) -> int:+ if not (n**0.5).is_integer():+ raise ValueError(f"{n} is not a square")+ return n++ @predictor+ def next_square(n: int) -> Annotated[int, AfterValidator(is_square)]:+ """What is the next square number after n?"""++ lm = DSPDummyLM(["3", "4"])+ dspy.settings.configure(lm=lm)++ m = next_square(n=2)+ lm.inspect_history(n=2)++ assert m == 4+++def test_demos():+ demos = [+ dspy.Example(input="What is the speed of light?", output="3e8"),+ ]+ program = LabeledFewShot(k=len(demos)).compile(+ student=dspy.TypedPredictor("input -> output"),+ trainset=[ex.with_inputs("input") for ex in demos],+ )++ lm = DSPDummyLM(["Paris"])+ dspy.settings.configure(lm=lm)++ assert program(input="What is the capital of France?").output == "Paris"++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `input`, produce the fields `output`.++ ---++ Follow the following format.++ Input: ${input}+ Output: ${output}++ ---++ Input: What is the speed of light?+ Output: 3e8++ ---++ Input: What is the capital of France?+ Output: Paris"""+ )+++def _test_demos_missing_input():+ demos = [dspy.Example(input="What is the speed of light?", output="3e8")]+ program = LabeledFewShot(k=len(demos)).compile(+ student=dspy.TypedPredictor("input -> output, thoughts"),+ trainset=[ex.with_inputs("input") for ex in demos],+ )+ dspy.settings.configure(lm=DSPDummyLM(["My thoughts", "Paris"]))+ assert program(input="What is the capital of France?").output == "Paris"++ assert dspy.settings.lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `input`, produce the fields `output`.++ ---++ Follow the following format.++ Input: ${input}+ Thoughts: ${thoughts}+ Output: ${output}++ ---++ Input: What is the speed of light?+ Output: 3e8++ ---++ Input: What is the capital of France?+ Thoughts: My thoughts+ Output: Paris"""+ )+++def test_conlist():+ dspy.settings.configure(lm=DSPDummyLM(["[]", "[1]", "[1, 2]", "[1, 2, 3]"]))++ @predictor+ def make_numbers(input: str) -> Annotated[list[int], Field(min_items=2)]:+ pass++ assert make_numbers(input="What are the first two numbers?") == [1, 2]+++def test_conlist2():+ dspy.settings.configure(lm=DSPDummyLM(["[]", "[1]", "[1, 2]", "[1, 2, 3]"]))++ make_numbers = TypedPredictor("input:str -> output:Annotated[List[int], Field(min_items=2)]")+ assert make_numbers(input="What are the first two numbers?").output == [1, 2]+++def test_model_validator():+ class MySignature(dspy.Signature):+ input_data: str = dspy.InputField()+ allowed_categories: list[str] = dspy.InputField()+ category: str = dspy.OutputField()++ @model_validator(mode="after")+ def check_cateogry(self):+ if self.category not in self.allowed_categories:+ raise ValueError(f"category not in {self.allowed_categories}")+ return self++ lm = DSPDummyLM(["horse", "dog"])+ dspy.settings.configure(lm=lm)+ predictor = TypedPredictor(MySignature)++ pred = predictor(input_data="What is the best animal?", allowed_categories=["cat", "dog"])+ assert pred.category == "dog"diff --git a/tests/dsp_LM/functional/test_signature_opt_typed.py b/tests/dsp_LM/functional/test_signature_opt_typed.pynew file mode 100644index 000000000..3533926b6--- /dev/null+++ b/tests/dsp_LM/functional/test_signature_opt_typed.py@@ -0,0 +1,187 @@+import json+from typing import Generic, TypeVar++import pydantic+from pydantic_core import to_jsonable_python++import dspy+from dspy.evaluate import Evaluate+from dspy.evaluate.metrics import answer_exact_match+from dspy.functional import TypedPredictor+from dspy.teleprompt.signature_opt_typed import make_info, optimize_signature+from dspy.utils import DSPDummyLM++hotpotqa = [+ ex.with_inputs("question")+ for ex in [+ dspy.Example(+ question="At My Window was released by which American singer-songwriter?",+ answer="John Townes Van Zandt",+ ),+ dspy.Example(+ question="which American actor was Candace Kita guest starred with ",+ answer="Bill Murray",+ ),+ dspy.Example(+ question="Which of these publications was most recently published, Who Put the Bomp or Self?",+ answer="Self",+ ),+ dspy.Example(+ question="The Victorians - Their Story In Pictures is a documentary series written by an author born in what year?",+ answer="1950",+ ),+ dspy.Example(+ question="Which magazine has published articles by Scott Shaw, Tae Kwon Do Times or Southwest Art?",+ answer="Tae Kwon Do Times",+ ),+ dspy.Example(+ question="In what year was the club founded that played Manchester City in the 1972 FA Charity Shield",+ answer="1874",+ ),+ dspy.Example(+ question="Which is taller, the Empire State Building or the Bank of America Tower?",+ answer="The Empire State Building",+ ),+ dspy.Example(+ question='Which American actress who made their film debut in the 1995 teen drama "Kids" was the co-founder of Voto Latino?',+ answer="Rosario Dawson",+ ),+ dspy.Example(+ question="Tombstone stared an actor born May 17, 1955 known as who?",+ answer="Bill Paxton",+ ),+ dspy.Example(+ question="What is the code name for the German offensive that started this Second World War engagement on the Eastern Front (a few hundred kilometers from Moscow) between Soviet and German forces, which included 102nd Infantry Division?",+ answer="Operation Citadel",+ ),+ dspy.Example(+ question='Who acted in the shot film The Shore and is also the youngest actress ever to play Ophelia in a Royal Shakespeare Company production of "Hamlet." ?',+ answer="Kerry Condon",+ ),+ dspy.Example(+ question="Which company distributed this 1977 American animated film produced by Walt Disney Productions for which Sherman Brothers wrote songs?",+ answer="Buena Vista Distribution",+ ),+ dspy.Example(+ question="Samantha Cristoforetti and Mark Shuttleworth are both best known for being first in their field to go where? ",+ answer="space",+ ),+ dspy.Example(+ question="Having the combination of excellent foot speed and bat speed helped Eric Davis, create what kind of outfield for the Los Angeles Dodgers? ",+ answer="Outfield of Dreams",+ ),+ dspy.Example(+ question="Which Pakistani cricket umpire who won 3 consecutive ICC umpire of the year awards in 2009, 2010, and 2011 will be in the ICC World Twenty20?",+ answer="Aleem Sarwar Dar",+ ),+ dspy.Example(+ question="The Organisation that allows a community to influence their operation or use and to enjoy the benefits arisingwas founded in what year?",+ answer="2010",+ ),+ dspy.Example(+ question='"Everything Has Changed" is a song from an album released under which record label ?',+ answer="Big Machine Records",+ ),+ dspy.Example(+ question="Who is older, Aleksandr Danilovich Aleksandrov or Anatoly Fomenko?",+ answer="Aleksandr Danilovich Aleksandrov",+ ),+ dspy.Example(+ question="On the coast of what ocean is the birthplace of Diogal Sakho?",+ answer="Atlantic",+ ),+ dspy.Example(+ question="This American guitarist best known for her work with the Iron Maidens is an ancestor of a composer who was known as what?",+ answer="The Waltz King",+ ),+ ]+]+++def test_opt():+ class BasicQA(dspy.Signature):+ question: str = dspy.InputField()+ answer: str = dspy.OutputField()++ qa_model = DSPDummyLM([])+ prompt_model = DSPDummyLM(+ [+ # Seed prompts+ "some thoughts",+ '[{"instructions": "I", "question_desc": "$q", "question_prefix": "Q:", "answer_desc": "$a", "answer_prefix": "A:"}]',+ ]+ )+ dspy.settings.configure(lm=qa_model)++ result = optimize_signature(+ student=TypedPredictor(BasicQA),+ evaluator=Evaluate(devset=hotpotqa, metric=answer_exact_match, num_threads=1),+ initial_prompts=1,+ n_iterations=2,+ verbose=True,+ prompt_model=prompt_model,+ strategy="last",+ )++ # Since we are requesting the last signature, it doesn't matter that our qa_model is+ # bad, and gets 0 score. We should still get the last signature.+ class ExpectedSignature(dspy.Signature):+ "I"++ question: str = dspy.InputField(desc="$q", prefix="Q:")+ answer: str = dspy.OutputField(desc="$a", prefix="A:")++ assert result.program.signature.equals(ExpectedSignature)++ assert result.scores == [0, 0]+++def test_opt_composed():+ class MyModule(dspy.Module):+ def __init__(self):+ self.p1 = TypedPredictor("question:str -> considerations:list[str]", max_retries=1)+ self.p2 = TypedPredictor("considerations:list[str] -> answer:str", max_retries=1)++ def forward(self, question):+ considerations = self.p1(question=question).considerations+ return self.p2(considerations=considerations)++ class ExpectedSignature1(dspy.Signature):+ "I1"++ question: str = dspy.InputField(desc="$q", prefix="Q:")+ considerations: list[str] = dspy.OutputField(desc="$c", prefix="C:")++ info1 = make_info(ExpectedSignature1)++ class ExpectedSignature2(dspy.Signature):+ "I2"++ considerations: list[str] = dspy.InputField(desc="$c", prefix="C:")+ answer: str = dspy.OutputField(desc="$a", prefix="A:")++ info2 = make_info(ExpectedSignature2)++ qa_model = DSPDummyLM([])+ prompt_model = DSPDummyLM(+ [+ "some thoughts",+ json.dumps([to_jsonable_python(info1)]),+ "some thoughts",+ json.dumps([to_jsonable_python(info2)]),+ ]+ )+ dspy.settings.configure(lm=qa_model)++ result = optimize_signature(+ student=MyModule(),+ evaluator=lambda x: 0, # We don't care about the evaluator here+ initial_prompts=1,+ n_iterations=2,+ verbose=True,+ prompt_model=prompt_model,+ strategy="last",+ )++ assert result.program.p1.signature.equals(ExpectedSignature1)+ assert result.program.p2.signature.equals(ExpectedSignature2)diff --git a/tests/dsp_LM/functional/test_signature_typed.py b/tests/dsp_LM/functional/test_signature_typed.pynew file mode 100644index 000000000..6e875d851--- /dev/null+++ b/tests/dsp_LM/functional/test_signature_typed.py@@ -0,0 +1,155 @@+from typing import Any, Optional, Union++import pydantic+import pytest++import dspy+from dspy.functional import TypedPredictor+from dspy.signatures.signature import signature_to_template+++def get_field_and_parser(signature: dspy.Signature) -> tuple[Any, Any]:+ module = TypedPredictor(signature)+ signature = module._prepare_signature()+ assert "answer" in signature.fields, "'answer' not in signature.fields"+ field = signature.fields.get("answer")+ parser = field.json_schema_extra.get("parser")+ return field, parser+++class Mysubmodel(pydantic.BaseModel):+ sub_floating: float+++class MyModel(pydantic.BaseModel):+ floating: float+ string: str+ boolean: bool+ integer: int+ optional: Optional[str]+ sequence_of_strings: list[str]+ union: Union[str, float]+ submodel: Mysubmodel+ optional_submodel: Optional[Mysubmodel]+ optional_existing_submodule: Optional[Mysubmodel]+++def build_model_instance() -> MyModel:+ return MyModel(+ floating=3.14,+ string="foobar",+ boolean=True,+ integer=42,+ optional=None,+ sequence_of_strings=["foo", "bar"],+ union=3.14,+ submodel=Mysubmodel(sub_floating=42.42),+ optional_submodel=None,+ optional_existing_submodule=Mysubmodel(sub_floating=42.42),+ )+++ "test_type,serialized, expected", [(str, "foo", "foo"), (int, "42", 42), (float, "42.42", 42.42)]+)+def test_basic_types(test_type: type, serialized: str, expected: Any):+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: test_type = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)+ assert parser is test_type, "Parser is not correct for 'answer'"+ assert parser(serialized) == expected, f"{test_type}({serialized})!= {expected}"+++def test_boolean():+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: bool = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)+ assert parser("true"), f"Parsing 'true' failed"+ assert not parser("false"), f"Parsing 'false' failed"+++ "test_type,serialized, expected",+ [(list[str], '["foo", "bar"]', ["foo", "bar"]), (tuple[int, float], "[42, 3.14]", (42, 3.14))],+)+def test_sequences(test_type: type, serialized: str, expected: Any):+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: test_type = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)++ assert parser(serialized) == expected, f"Parsing {expected} failed"+++ "test_type,serialized, expected",+ [+ (Optional[str], '"foobar"', "foobar"),+ (Optional[str], "null", None),+ (Union[str, float], "3.14", 3.14),+ (Union[str, bool], "true", True),+ ],+)+def test_unions(test_type: type, serialized: str, expected: Any):+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: test_type = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)++ assert parser(serialized) == expected, f"Parsing {expected} failed"+++def test_pydantic():+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: MyModel = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)++ instance = build_model_instance()+ parsed_instance = parser(instance.model_dump_json())++ assert parsed_instance == instance, f"{instance} != {parsed_instance}"+++def test_optional_pydantic():+ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: Optional[MyModel] = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)++ instance = build_model_instance()+ parsed_instance = parser(instance.model_dump_json())+ assert parsed_instance == instance, f"{instance} != {parsed_instance}"++ # Check null case+ parsed_instance = parser("null")+ assert parsed_instance == None, "Optional[MyModel] should be None"+++def test_dataclass():+ from dataclasses import dataclass++ @dataclass(frozen=True)+ class MyDataclass:+ string: str+ number: int+ floating: float+ boolean: bool++ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: MyDataclass = dspy.OutputField()++ _, parser = get_field_and_parser(MySignature)++ instance = MyDataclass("foobar", 42, 3.14, True)+ parsed_instance = parser('{"string": "foobar", "number": 42, "floating": 3.14, "boolean": true}')+ assert parsed_instance == instance, f"{instance} != {parsed_instance}"diff --git a/tests/dsp_LM/modules/test_aws_models.py b/tests/dsp_LM/modules/test_aws_models.pynew file mode 100644index 000000000..b6e018b33--- /dev/null+++ b/tests/dsp_LM/modules/test_aws_models.py@@ -0,0 +1,70 @@+"""Tests for AWS models.+Note: Requires configuration of your AWS credentials with the AWS CLI and creating sagemaker endpoints.+TODO: Create mock fixtures for pytest to remove the need for AWS credentials and endpoints.+"""++import dsp+import dspy+++def get_lm(lm_provider: str, model_path: str, **kwargs) -> dsp.modules.lm.LM:+ """get the language model"""+ # extract model vendor and name from model name+ # Model path format is <MODEL_VENDOR>/<MODEL_NAME_OR_ENDPOINT>+ model_vendor = model_path.split("/")[0]+ model_name = model_path.split("/")[1]++ if lm_provider == "Bedrock":+ bedrock = dspy.Bedrock(region_name="us-west-2")+ if model_vendor == "mistral":+ return dspy.AWSMistral(bedrock, model_name, **kwargs)+ elif model_vendor == "anthropic":+ return dspy.AWSAnthropic(bedrock, model_name, **kwargs)+ elif model_vendor == "meta":+ return dspy.AWSMeta(bedrock, model_name, **kwargs)+ else:+ raise ValueError(+ "Model vendor missing or unsupported: Model path format is <MODEL_VENDOR>/<MODEL_NAME_OR_ENDPOINT>"+ )+ elif lm_provider == "Sagemaker":+ sagemaker = dspy.Sagemaker(region_name="us-west-2")+ if model_vendor == "mistral":+ return dspy.AWSMistral(sagemaker, model_name, **kwargs)+ elif model_vendor == "meta":+ return dspy.AWSMeta(sagemaker, model_name, **kwargs)+ else:+ raise ValueError(+ "Model vendor missing or unsupported: Model path format is <MODEL_VENDOR>/<MODEL_NAME_OR_ENDPOINT>"+ )+ else:+ raise ValueError(f"Unsupported model: {model_name}")+++def run_tests():+ """Test the providers and models"""+ # Configure your AWS credentials with the AWS CLI before running this script+ provider_model_tuples = [+ ("Bedrock", "mistral/mistral.mixtral-8x7b-instruct-v0:1"),+ ("Bedrock", "anthropic/anthropic.claude-3-haiku-20240307-v1:0"),+ ("Bedrock", "anthropic/anthropic.claude-3-sonnet-20240229-v1:0"),+ ("Bedrock", "meta/meta.llama2-70b-chat-v1"),+ ("Bedrock", "meta/meta.llama3-8b-instruct-v1:0"),+ ("Bedrock", "meta/meta.llama3-70b-instruct-v1:0"),+ # ('Sagemaker', 'mistral/<YOUR_ENDPOINT_NAME>'), # REPLACE YOUR_ENDPOINT_NAME with your sagemaker endpoint+ ]++ predict_func = dspy.Predict("question -> answer")+ for provider, model_path in provider_model_tuples:+ print(f"Provider: {provider}, Model: {model_path}")+ lm = get_lm(provider, model_path)+ with dspy.context(lm=lm):+ question = "What is the capital of France?"+ answer = predict_func(question=question).answer+ print(f"Question: {question}\nAnswer: {answer}")+ print("---------------------------------")+ lm.inspect_history()+ print("---------------------------------\n")+++if __name__ == "__main__":+ run_tests()diff --git a/tests/dsp_LM/modules/test_cloudflare_models.py b/tests/dsp_LM/modules/test_cloudflare_models.pynew file mode 100644index 000000000..188d2424e--- /dev/null+++ b/tests/dsp_LM/modules/test_cloudflare_models.py@@ -0,0 +1,61 @@+"""Tests for Cloudflare models.+Note: Requires configuration of your Cloudflare account_id and api_key.+"""++import dspy++models = {+ "@cf/qwen/qwen1.5-0.5b-chat": "https://huggingface.co/qwen/qwen1.5-0.5b-chat",+ "@hf/meta-llama/meta-llama-3-8b-instruct": "https://llama.meta.com",+ "@hf/nexusflow/starling-lm-7b-beta": "https://huggingface.co/Nexusflow/Starling-LM-7B-beta",+ "@cf/meta/llama-3-8b-instruct": "https://llama.meta.com",+ "@hf/thebloke/neural-chat-7b-v3-1-awq": "",+ "@cf/meta/llama-2-7b-chat-fp16": "https://ai.meta.com/llama/",+ "@cf/mistral/mistral-7b-instruct-v0.1": "https://mistral.ai/news/announcing-mistral-7b/",+ "@cf/tinyllama/tinyllama-1.1b-chat-v1.0": "https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0",+ "@hf/mistral/mistral-7b-instruct-v0.2": "https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2",+ "@cf/fblgit/una-cybertron-7b-v2-bf16": "",+ "@hf/thebloke/codellama-7b-instruct-awq": "https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-AWQ",+ "@cf/thebloke/discolm-german-7b-v1-awq": "https://huggingface.co/TheBloke/DiscoLM_German_7b_v1-AWQ",+ "@cf/meta/llama-2-7b-chat-int8": "https://ai.meta.com/llama/",+ "@hf/thebloke/mistral-7b-instruct-v0.1-awq": "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-AWQ",+ "@hf/thebloke/openchat_3.5-awq": "",+ "@cf/qwen/qwen1.5-7b-chat-awq": "https://huggingface.co/qwen/qwen1.5-7b-chat-awq",+ "@hf/thebloke/llama-2-13b-chat-awq": "https://huggingface.co/TheBloke/Llama-2-13B-chat-AWQ",+ "@hf/thebloke/deepseek-coder-6.7b-base-awq": "",+ "@hf/thebloke/openhermes-2.5-mistral-7b-awq": "",+ "@hf/thebloke/deepseek-coder-6.7b-instruct-awq": "",+ "@cf/deepseek-ai/deepseek-math-7b-instruct": "https://huggingface.co/deepseek-ai/deepseek-math-7b-instruct",+ "@cf/tiiuae/falcon-7b-instruct": "https://huggingface.co/tiiuae/falcon-7b-instruct",+ "@hf/nousresearch/hermes-2-pro-mistral-7b": "https://huggingface.co/NousResearch/Hermes-2-Pro-Mistral-7B",+ "@hf/thebloke/zephyr-7b-beta-awq": "https://huggingface.co/TheBloke/zephyr-7B-beta-AWQ",+ "@cf/qwen/qwen1.5-1.8b-chat": "https://huggingface.co/qwen/qwen1.5-1.8b-chat",+ "@cf/defog/sqlcoder-7b-2": "https://huggingface.co/defog/sqlcoder-7b-2",+ "@cf/microsoft/phi-2": "https://huggingface.co/microsoft/phi-2",+ "@hf/google/gemma-7b-it": "https://ai.google.dev/gemma/docs",+}+++def get_lm(name: str): # -> dspy.LM:+ return dspy.CloudflareAI(model=name)+++def run_tests():+ """Test the providers and models"""+ # Configure your AWS credentials with the AWS CLI before running this script+ models++ predict_func = dspy.Predict("question -> answer")+ for model_name in models.keys():+ lm = get_lm(model_name)+ with dspy.context(lm=lm):+ question = "What is the capital of France?"+ answer = predict_func(question=question).answer+ print(f"Question: {question}\nAnswer: {answer}")+ print("---------------------------------")+ lm.inspect_history()+ print("---------------------------------\n")+++if __name__ == "__main__":+ run_tests()diff --git a/tests/dsp_LM/modules/test_hf_model.py b/tests/dsp_LM/modules/test_hf_model.pynew file mode 100644index 000000000..0b06c8042--- /dev/null+++ b/tests/dsp_LM/modules/test_hf_model.py@@ -0,0 +1,31 @@+from pytest_mock.plugin import MockerFixture+from transformers import AutoModelForSeq2SeqLM++import dspy+++class MockConfig:+ def __init__(self, architectures: list[str]):+ self.architectures = architectures+++def test_load_gated_model(mocker: MockerFixture):+ conf = MockConfig(architectures=["ConditionalGeneration"])+ mocker.patch("transformers.AutoModelForSeq2SeqLM.from_pretrained")+ mocker.patch("transformers.AutoConfig.from_pretrained", return_value=conf)+ mocker.patch("transformers.AutoTokenizer.from_pretrained")++ some_token = "asdfasdfasdf"+ model = "google/gemma-7b"+ _ = dspy.HFModel(model, token=some_token)+ AutoModelForSeq2SeqLM.from_pretrained.assert_called_with(model, device_map="auto", token=some_token)+++def test_load_ungated_model(mocker: MockerFixture):+ conf = MockConfig(architectures=["ConditionalGeneration"])+ mocker.patch("transformers.AutoModelForSeq2SeqLM.from_pretrained")+ mocker.patch("transformers.AutoConfig.from_pretrained", return_value=conf)+ mocker.patch("transformers.AutoTokenizer.from_pretrained")+ _ = dspy.HFModel("openai-community/gpt2")+ # no token used in automodel+ AutoModelForSeq2SeqLM.from_pretrained.assert_called_with("openai-community/gpt2", device_map="auto", token=None)diff --git a/tests/dsp_LM/modules/vectorizer/test_fastembed.py b/tests/dsp_LM/modules/vectorizer/test_fastembed.pynew file mode 100644index 000000000..e9a7335a4--- /dev/null+++ b/tests/dsp_LM/modules/vectorizer/test_fastembed.py@@ -0,0 +1,43 @@+import pytest++from dsp.modules.sentence_vectorizer import FastEmbedVectorizer+from dspy.primitives.example import Example++# Skip the test if the 'fastembed' package is not installed+pytest.importorskip("fastembed", reason="'fastembed' is not installed. Use `pip install fastembed` to install it.")+++ "n_dims,model_name", [(384, "BAAI/bge-small-en-v1.5"), (512, "jinaai/jina-embeddings-v2-small-en")]+)+def test_fastembed_with_examples(n_dims, model_name):+ vectorizer = FastEmbedVectorizer(model_name)++ examples = [+ Example(query="What's the price today?", response="The price is $10.00").with_inputs("query", "response"),+ Example(query="What's the weather today?", response="The weather is sunny").with_inputs("query", "response"),+ Example(query="Who was leading the team?", response="It was Jim. Rather enthusiastic guy.").with_inputs(+ "query", "response"+ ),+ ]++ embeddings = vectorizer(examples)++ assert embeddings.shape == (len(examples), n_dims)+++ "n_dims,model_name", [(384, "BAAI/bge-small-en-v1.5"), (512, "jinaai/jina-embeddings-v2-small-en")]+)+def test_fastembed_with_strings(n_dims, model_name):+ vectorizer = FastEmbedVectorizer(model_name)++ inputs = [+ "Jonathan Kent is a fictional character appearing in American comic books published by DC Comics.",+ "Clark Kent is a fictional character appearing in American comic books published by DC Comics.",+ "Martha Kent is a fictional character appearing in American comic books published by DC Comics.",+ ]++ embeddings = vectorizer(inputs)++ assert embeddings.shape == (len(inputs), n_dims)diff --git a/tests/dsp_LM/predict/test_aggregation.py b/tests/dsp_LM/predict/test_aggregation.pynew file mode 100644index 000000000..eb1d97536--- /dev/null+++ b/tests/dsp_LM/predict/test_aggregation.py@@ -0,0 +1,43 @@+from dsp.utils import normalize_text+from dspy.predict.aggregation import majority+from dspy.primitives.prediction import Completions, Prediction+++def test_majority_with_prediction():+ prediction = Prediction.from_completions([{"answer": "2"}, {"answer": "2"}, {"answer": "3"}])+ result = majority(prediction)+ assert result.completions[0]["answer"] == "2"+++def test_majority_with_completions():+ completions = Completions([{"answer": "2"}, {"answer": "2"}, {"answer": "3"}])+ result = majority(completions)+ assert result.completions[0]["answer"] == "2"+++def test_majority_with_list():+ completions = [{"answer": "2"}, {"answer": "2"}, {"answer": "3"}]+ result = majority(completions)+ assert result.completions[0]["answer"] == "2"+++def test_majority_with_normalize():+ completions = [{"answer": "2"}, {"answer": " 2"}, {"answer": "3"}]+ result = majority(completions, normalize=normalize_text)+ assert result.completions[0]["answer"] == "2"+++def test_majority_with_field():+ completions = [+ {"answer": "2", "other": "1"},+ {"answer": "2", "other": "1"},+ {"answer": "3", "other": "2"},+ ]+ result = majority(completions, field="other")+ assert result.completions[0]["other"] == "1"+++def test_majority_with_no_majority():+ completions = [{"answer": "2"}, {"answer": "3"}, {"answer": "4"}]+ result = majority(completions)+ assert result.completions[0]["answer"] == "2" # The first completion is returned in case of a tiediff --git a/tests/dsp_LM/predict/test_chain_of_thought.py b/tests/dsp_LM/predict/test_chain_of_thought.pynew file mode 100644index 000000000..2567ae032--- /dev/null+++ b/tests/dsp_LM/predict/test_chain_of_thought.py@@ -0,0 +1,36 @@+import textwrap++import dspy+from dspy import ChainOfThought+from dspy.utils import DSPDummyLM+++def test_initialization_with_string_signature():+ lm = DSPDummyLM(["find the number after 1", "2"])+ dspy.settings.configure(lm=lm)+ predict = ChainOfThought("question -> answer")+ assert list(predict.extended_signature.output_fields.keys()) == [+ "rationale",+ "answer",+ ]+ assert predict(question="What is 1+1?").answer == "2"++ print(lm.get_convo(-1))+ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `question`, produce the fields `answer`.++ ---++ Follow the following format.++ Question: ${question}+ Reasoning: Let's think step by step in order to ${produce the answer}. We ...+ Answer: ${answer}++ ---++ Question: What is 1+1?+ Reasoning: Let's think step by step in order to find the number after 1+ Answer: 2"""+ )diff --git a/tests/dsp_LM/predict/test_chain_of_thought_with_hint.py b/tests/dsp_LM/predict/test_chain_of_thought_with_hint.pynew file mode 100644index 000000000..0afa06b1c--- /dev/null+++ b/tests/dsp_LM/predict/test_chain_of_thought_with_hint.py@@ -0,0 +1,43 @@+import dspy+from dspy import ChainOfThoughtWithHint+from dspy.utils import DSPDummyLM+++def test_cot_with_no_hint():+ lm = DSPDummyLM(["find the number after 1", "2"])+ dspy.settings.configure(lm=lm)+ predict = ChainOfThoughtWithHint("question -> answer")+ # Check output fields have the right order+ assert list(predict.extended_signature2.output_fields.keys()) == [+ "rationale",+ "hint",+ "answer",+ ]+ assert predict(question="What is 1+1?").answer == "2"++ final_convo = lm.get_convo(-1)+ assert final_convo.endswith(+ "Question: What is 1+1?\n"+ "Reasoning: Let's think step by step in order to find the number after 1\n"+ "Answer: 2"+ )+++def test_cot_with_hint():+ lm = DSPDummyLM(["find the number after 1", "2"])+ dspy.settings.configure(lm=lm)+ predict = ChainOfThoughtWithHint("question -> answer")+ assert list(predict.extended_signature2.output_fields.keys()) == [+ "rationale",+ "hint",+ "answer",+ ]+ assert predict(question="What is 1+1?", hint="think small").answer == "2"++ final_convo = lm.get_convo(-1)+ assert final_convo.endswith(+ "Question: What is 1+1?\n\n"+ "Reasoning: Let's think step by step in order to find the number after 1\n\n"+ "Hint: think small\n\n"+ "Answer: 2"+ )diff --git a/tests/dsp_LM/predict/test_knn.py b/tests/dsp_LM/predict/test_knn.pynew file mode 100644index 000000000..7f35b42e4--- /dev/null+++ b/tests/dsp_LM/predict/test_knn.py@@ -0,0 +1,51 @@+import numpy as np+import pytest++import dsp+import dspy+from dspy.predict import KNN+from dspy.utils import DummyVectorizer+++def mock_example(question: str, answer: str) -> dsp.Example:+ """Creates a mock DSP example with specified question and answer."""+ return dspy.Example(question=question, answer=answer).with_inputs("question")+++def setup_knn():+ """Sets up a KNN instance with a mocked vectorizer for testing."""+ dsp.SentenceTransformersVectorizer = DummyVectorizer+ trainset = [+ mock_example("What is the capital of France?", "Paris"),+ mock_example("What is the largest ocean?", "Pacific"),+ mock_example("What is 2+2?", "4"),+ ]+ knn = KNN(k=2, trainset=trainset)+ return knn+++def test_knn_initialization(setup_knn):+ """Tests the KNN initialization and checks if the trainset vectors are correctly created."""+ knn = setup_knn+ assert knn.k == 2, "Incorrect k value"+ assert len(knn.trainset_vectors) == 3, "Incorrect size of trainset vectors"+ assert isinstance(knn.trainset_vectors, np.ndarray), "Trainset vectors should be a NumPy array"+++def test_knn_query(setup_knn):+ """Tests the KNN query functionality for retrieving the nearest neighbors."""+ knn = setup_knn+ query = {"question": "What is 3+3?"} # A query close to "What is 2+2?"+ nearest_samples = knn(**query)+ assert len(nearest_samples) == 2, "Incorrect number of nearest samples returned"+ assert nearest_samples[0].answer == "4", "Incorrect nearest sample returned"+++def test_knn_query_specificity(setup_knn):+ """Tests the KNN query functionality for specificity of returned examples."""+ knn = setup_knn+ query = {"question": "What is the capital of Germany?"} # A query close to "What is the capital of France?"+ nearest_samples = knn(**query)+ assert len(nearest_samples) == 2, "Incorrect number of nearest samples returned"+ assert "Paris" in [sample.answer for sample in nearest_samples], "Expected Paris to be a nearest sample answer"diff --git a/tests/dsp_LM/predict/test_langchain.py b/tests/dsp_LM/predict/test_langchain.pynew file mode 100644index 000000000..89fa1d75d--- /dev/null+++ b/tests/dsp_LM/predict/test_langchain.py@@ -0,0 +1,55 @@+import pytest++pytest.importorskip("langchain")++import os++from langchain import hub+from langchain_chroma import Chroma+from langchain_community.embeddings import FakeEmbeddings+from langchain_core.documents import Document+from langchain_core.output_parsers import StrOutputParser+from langchain_core.runnables import RunnablePassthrough+from langchain_openai import ChatOpenAI+from langchain_text_splitters import RecursiveCharacterTextSplitter++from dspy.predict.langchain import LangChainModule, LangChainPredict+++def test_copying_module():+ os.environ["OPENAI_API_KEY"] = "fake-key"+ llm = ChatOpenAI(model="gpt-4o-mini")+ docs = [Document(page_content="Hello, world!", metadata={"source": "https://example.com"})]++ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10, chunk_overlap=10)+ splits = text_splitter.split_documents(docs)+ vectorstore = Chroma.from_documents(documents=splits, embedding=FakeEmbeddings(size=5))++ # Retrieve and generate using the relevant snippets of the blog.+ retriever = vectorstore.as_retriever()++ prompt = hub.pull("rlm/rag-prompt")++ def format_docs(docs):+ return "\n\n".join(doc.page_content for doc in docs)++ rag_chain = (+ {"context": retriever | format_docs, "question": RunnablePassthrough()}+ | LangChainPredict(prompt, llm)+ | StrOutputParser()+ )+ # Now we wrap it in LangChainModule.+ rag_dspy_module = LangChainModule(rag_chain)++ copied_module = rag_dspy_module.reset_copy()+ assert len(copied_module.chain.steps) == len(rag_dspy_module.chain.steps)+ for module, copied_module in zip(rag_dspy_module.chain.steps, copied_module.chain.steps):+ if isinstance(module, LangChainPredict):+ # The LangChainPredict modules are deep copied.+ assert module != copied_module+ assert module.langchain_llm.model_name == copied_module.langchain_llm.model_name+ else:+ # The rest of the modules are just copied by reference.+ assert module == copied_module+ # Clean up.+ os.environ["OPENAI_API_KEY"] = Nonediff --git a/tests/dsp_LM/predict/test_multi_chain_comparison.py b/tests/dsp_LM/predict/test_multi_chain_comparison.pynew file mode 100644index 000000000..e97c3dfbd--- /dev/null+++ b/tests/dsp_LM/predict/test_multi_chain_comparison.py@@ -0,0 +1,40 @@+import pytest++import dspy+from dspy.utils.dummies import DSPDummyLM+++def test_basic_example():+ class BasicQA(dspy.Signature):+ """Answer questions with short factoid answers."""++ question = dspy.InputField()+ answer = dspy.OutputField(desc="often between 1 and 5 words")++ # Example completions generated by a model for reference+ completions = [+ dspy.Prediction(+ rationale="I recall that during clear days, the sky often appears this color.",+ answer="blue",+ ),+ dspy.Prediction(+ rationale="Based on common knowledge, I believe the sky is typically seen as this color.",+ answer="green",+ ),+ dspy.Prediction(+ rationale="From images and depictions in media, the sky is frequently represented with this hue.",+ answer="blue",+ ),+ ]++ # Pass signature to MultiChainComparison module+ compare_answers = dspy.MultiChainComparison(BasicQA)++ # Call the MultiChainComparison on the completions+ question = "What is the color of the sky?"+ lm = DSPDummyLM(["my rationale", "blue"])+ dspy.settings.configure(lm=lm)+ final_pred = compare_answers(completions, question=question)++ assert final_pred.rationale == "my rationale"+ assert final_pred.answer == "blue"diff --git a/tests/dsp_LM/predict/test_predict.py b/tests/dsp_LM/predict/test_predict.pynew file mode 100644index 000000000..9158987e3--- /dev/null+++ b/tests/dsp_LM/predict/test_predict.py@@ -0,0 +1,101 @@+import copy+import textwrap++import pydantic+import pytest+import ujson++import dspy+from dspy import Predict, Signature, TypedPredictor+from dspy.utils.dummies import DSPDummyLM+++def test_call_method():+ predict_instance = Predict("input -> output")+ lm = DSPDummyLM(["test output"])+ dspy.settings.configure(lm=lm)+ result = predict_instance(input="test input")+ assert result.output == "test output"+ assert lm.get_convo(-1) == (+ "Given the fields `input`, produce the fields `output`.\n"+ "\n---\n\n"+ "Follow the following format.\n\n"+ "Input: ${input}\n"+ "Output: ${output}\n"+ "\n---\n\n"+ "Input: test input\n"+ "Output: test output"+ )+++def test_forward_method():+ program = Predict("question -> answer")+ dspy.settings.configure(lm=DSPDummyLM([]))+ result = program(question="What is 1+1?").answer+ assert result == "No more responses"+++def test_forward_method2():+ program = Predict("question -> answer1, answer2")+ dspy.settings.configure(lm=DSPDummyLM(["my first answer", "my second answer"]))+ result = program(question="What is 1+1?")+ assert result.answer1 == "my first answer"+ assert result.answer2 == "my second answer"+++def test_config_management():+ predict_instance = Predict("input -> output")+ predict_instance.update_config(new_key="value")+ config = predict_instance.get_config()+ assert "new_key" in config and config["new_key"] == "value"+++def test_multi_output():+ program = Predict("question -> answer", n=2)+ dspy.settings.configure(lm=DSPDummyLM(["my first answer", "my second answer"]))+ results = program(question="What is 1+1?")+ assert results.completions.answer[0] == "my first answer"+ assert results.completions.answer[1] == "my second answer"+++def test_multi_output2():+ program = Predict("question -> answer1, answer2", n=2)+ dspy.settings.configure(+ lm=DSPDummyLM(+ [+ "my 0 answer\nAnswer 2: my 2 answer",+ "my 1 answer\nAnswer 2: my 3 answer",+ ],+ )+ )+ results = program(question="What is 1+1?")+ assert results.completions.answer1[0] == "my 0 answer"+ assert results.completions.answer1[1] == "my 1 answer"+ assert results.completions.answer2[0] == "my 2 answer"+ assert results.completions.answer2[1] == "my 3 answer"+++def test_output_only():+ class OutputOnlySignature(dspy.Signature):+ output = dspy.OutputField()++ predictor = Predict(OutputOnlySignature)++ lm = DSPDummyLM(["short answer"])+ dspy.settings.configure(lm=lm)+ assert predictor().output == "short answer"++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields , produce the fields `output`.++ ---++ Follow the following format.++ Output: ${output}++ ---++ Output: short answer"""+ )diff --git a/tests/dsp_LM/predict/test_program_of_thought.py b/tests/dsp_LM/predict/test_program_of_thought.pynew file mode 100644index 000000000..e5522a847--- /dev/null+++ b/tests/dsp_LM/predict/test_program_of_thought.py@@ -0,0 +1,135 @@+import textwrap++import dspy+from dspy import ProgramOfThought, Signature+from dspy.utils import DSPDummyLM+++class BasicQA(Signature):+ question = dspy.InputField()+ answer = dspy.OutputField(desc="often between 1 and 5 words")+++def test_pot_code_generation():+ pot = ProgramOfThought(BasicQA)+ lm = DSPDummyLM(+ [+ "Reason_A",+ "```python\nresult = 1+1\n```",+ "Reason_B",+ "2",+ ]+ )+ dspy.settings.configure(lm=lm)+ res = pot(question="What is 1+1?")+ assert res.answer == "2"+ assert lm.get_convo(index=-1) == textwrap.dedent(+ """\+ Given the final code `question`, `final_generated_code`, `code_output`, provide the final `answer`.++ ---++ Follow the following format.++ Question: ${question}++ Code: python code that answers the question++ Code Output: output of previously-generated python code++ Reasoning: Let's think step by step in order to ${produce the answer}. We ...++ Answer: often between 1 and 5 words++ ---++ Question: What is 1+1?++ Code: result = 1+1++ Code Output: 2++ Reasoning: Let's think step by step in order to Reason_B++ Answer: 2"""+ )+++def test_pot_code_generation_with_error():+ pot = ProgramOfThought(BasicQA)+ lm = DSPDummyLM(+ [+ "Reason_A",+ "```python\nresult = 1+0/0\n```",+ "Reason_B", # Error: division by zero+ "```python\nresult = 1+1\n```",+ "Reason_C",+ "2",+ ]+ )+ dspy.settings.configure(lm=lm)+ res = pot(question="What is 1+1?")+ assert res.answer == "2"++ # The first code example failed+ assert lm.get_convo(index=2) == textwrap.dedent(+ """\+ You are given `question`, `previous_code`, `error` due to an error in previous code.+ Your task is to correct the error and provide the new `generated_code`.++ ---++ Follow the following format.++ Question: ${question}++ Previous Code: previously-generated python code that errored++ Error: error message from previously-generated python code++ Reasoning: Let's think step by step in order to ${produce the generated_code}. We ...++ Code: python code that answers the question++ ---++ Question: What is 1+1?++ Previous Code: result = 1+0/0++ Error: division by zero++ Reasoning: Let's think step by step in order to Reason_B"""+ )++ # The second code example succeeded+ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the final code `question`, `final_generated_code`, `code_output`, provide the final `answer`.++ ---++ Follow the following format.++ Question: ${question}++ Code: python code that answers the question++ Code Output: output of previously-generated python code++ Reasoning: Let's think step by step in order to ${produce the answer}. We ...++ Answer: often between 1 and 5 words++ ---++ Question: What is 1+1?++ Code: result = 1+1++ Code Output: 2++ Reasoning: Let's think step by step in order to Reason_C++ Answer: 2"""+ )diff --git a/tests/dsp_LM/predict/test_react.py b/tests/dsp_LM/predict/test_react.pynew file mode 100644index 000000000..e3f11d438--- /dev/null+++ b/tests/dsp_LM/predict/test_react.py@@ -0,0 +1,154 @@+from dataclasses import dataclass++import dspy+from dspy.utils.dummies import dummy_rm+++def test_example_no_tools():+ # Createa a simple dataset which the model will use with the Retrieve tool.+ lm = dspy.utils.DspDummyLM(+ [+ "Initial thoughts", # Thought_1+ "Finish[blue]", # Action_1+ ]+ )+ dspy.settings.configure(lm=lm, rm=dummy_rm())++ program = dspy.ReAct("question -> answer")++ # Check default tools+ assert isinstance(program.tools["Finish"], dspy.Example)++ # Call the ReAct module on a particular input+ question = "What is the color of the sky?"+ result = program(question=question)+ assert result.answer == "blue"++ # For debugging+ print("---")+ for row in lm.history:+ print(row["prompt"])+ print("Response:", row["response"]["choices"][0]["text"])+ print("---")++ assert lm.get_convo(-1).endswith(+ "Question: What is the color of the sky?\n" "Thought 1: Initial thoughts\n" "Action 1: Finish[blue]"+ )+++def test_example_search():+ # Createa a simple dataset which the model will use with the Retrieve tool.+ lm = dspy.utils.DspDummyLM(+ [+ "Initial thoughts", # Thought_1+ "Search[the color of the sky]", # Thought_1+ "More thoughts", # Thought_2+ "Finish[blue]", # Action_2+ ]+ )+ rm = dummy_rm(+ [+ "We all know the color of the sky is blue.",+ "Somethng about the sky colors",+ "This sentence is completely irellevant to answer the question.",+ "Let's add some more sentences to act as summy passages.",+ "Let's add some more sentences to act as summy passages.",+ "Let's add some more sentences to act as summy passages.",+ ]+ )+ dspy.settings.configure(lm=lm, rm=rm)++ program = dspy.ReAct("question -> answer")++ # Check default tools+ assert len(program.tools) == 2+ assert isinstance(program.tools["Search"], dspy.Retrieve)+ assert isinstance(program.tools["Finish"], dspy.Example)++ # Call the ReAct module on a particular input+ question = "What is the color of the sky?"+ result = program(question=question)+ assert result.answer == "blue"++ # For debugging+ print(lm.get_convo(-1))++ assert lm.get_convo(-1).endswith(+ "Question: What is the color of the sky?\n\n"+ "Thought 1: Initial thoughts\n\n"+ "Action 1: Search[the color of the sky]\n\n"+ "Observation 1:\n"+ "[1] «We all know the color of the sky is blue.»\n"+ "[2] «Somethng about the sky colors»\n"+ "[3] «This sentence is completely irellevant to answer the question.»\n\n"+ "Thought 2: More thoughts\n\n"+ "Action 2: Finish[blue]"+ )+++class DummyTool1:+ name = "Tool1"+ input_variable = "query"+ desc = ""+ num_calls = 0++ def __call__(self, *args, **kwargs):+ # test case with no passages attribute+ assert args[0] == "foo"+ self.num_calls += 1+ return "tool 1 output"+++@dataclass+class DummyOutput:+ passages: str+++class DummyTool2:+ name = "Tool2"+ input_variable = "query"+ desc = ""+ num_calls = 0++ def __call__(self, *args, **kwargs):+ # test case with passages attribute+ assert args[0] == "bar"+ self.num_calls += 1+ return DummyOutput(passages="tool 2 output")+++def test_custom_tools():+ lm = dspy.utils.DspDummyLM(+ [+ "Initial thoughts",+ "Tool1[foo]",+ "More thoughts",+ "Tool2[bar]",+ "Even more thoughts",+ "Finish[baz]",+ ]+ )+ dspy.settings.configure(lm=lm)++ tool1 = DummyTool1()+ tool2 = DummyTool2()+ program = dspy.ReAct("question -> answer", tools=[tool1, tool2])++ question = "What is the color of the sky?"+ result = program(question=question)+ assert result.answer == "baz"++ # each tool should be called only once+ assert tool1.num_calls == 1+ assert tool2.num_calls == 1+ assert lm.get_convo(-1).endswith(+ "Question: What is the color of the sky?\n\n"+ "Thought 1: Initial thoughts\n\n"+ "Action 1: Tool1[foo]\n\n"+ "Observation 1: tool 1 output\n\n"+ "Thought 2: More thoughts\n\n"+ "Action 2: Tool2[bar]\n\n"+ "Observation 2: tool 2 output\n\n"+ "Thought 3: Even more thoughts\n\n"+ "Action 3: Finish[baz]"+ )diff --git a/tests/dsp_LM/predict/test_retry.py b/tests/dsp_LM/predict/test_retry.pynew file mode 100644index 000000000..bd22984d4--- /dev/null+++ b/tests/dsp_LM/predict/test_retry.py@@ -0,0 +1,110 @@+import functools++import pydantic++import dspy+from dspy.primitives.assertions import assert_transform_module, backtrack_handler+from dspy.utils import DSPDummyLM+++def test_retry_simple():+ predict = dspy.Predict("question -> answer")+ retry_module = dspy.Retry(predict)++ # Test Retry has created the correct new signature+ for field in predict.signature.output_fields:+ assert f"past_{field}" in retry_module.new_signature.input_fields+ assert "feedback" in retry_module.new_signature.input_fields++ lm = DSPDummyLM(["blue"])+ dspy.settings.configure(lm=lm)+ result = retry_module.forward(+ question="What color is the sky?",+ past_outputs={"answer": "red"},+ feedback="Try harder",+ )+ assert result.answer == "blue"++ print(lm.get_convo(-1))+ assert lm.get_convo(-1).endswith(+ "Question: What color is the sky?\n\n" "Previous Answer: red\n\n" "Instructions: Try harder\n\n" "Answer: blue"+ )+++def test_retry_forward_with_feedback():+ # First we make a mistake, then we fix it+ lm = DSPDummyLM(["red", "blue"])+ dspy.settings.configure(lm=lm, trace=[])++ class SimpleModule(dspy.Module):+ def __init__(self):+ super().__init__()+ self.predictor = dspy.Predict("question -> answer")++ def forward(self, **kwargs):+ result = self.predictor(**kwargs)+ print(f"SimpleModule got {result.answer=}")+ dspy.Suggest(result.answer == "blue", "Please think harder")+ return result++ program = SimpleModule()+ program = assert_transform_module(+ program.map_named_predictors(dspy.Retry),+ functools.partial(backtrack_handler, max_backtracks=1),+ )++ result = program(question="What color is the sky?")++ assert result.answer == "blue"++ print(lm.get_convo(-1))+ assert lm.get_convo(-1).endswith(+ "Question: What color is the sky?\n\n"+ "Previous Answer: red\n\n"+ "Instructions: Please think harder\n\n"+ "Answer: blue"+ )+++def test_retry_forward_with_typed_predictor():+ # First we make a mistake, then we fix it+ lm = DSPDummyLM(['{"answer":"red"}', '{"answer":"blue"}'])+ dspy.settings.configure(lm=lm, trace=[])++ class AnswerQuestion(dspy.Signature):+ """Answer questions with succint responses."""++ class Input(pydantic.BaseModel):+ question: str++ class Output(pydantic.BaseModel):+ answer: str++ input: Input = dspy.InputField()+ output: Output = dspy.OutputField()++ class QuestionAnswerer(dspy.Module):+ def __init__(self):+ super().__init__()+ self.answer_question = dspy.TypedPredictor(AnswerQuestion)++ def forward(self, **kwargs):+ result = self.answer_question(input=AnswerQuestion.Input(**kwargs)).output+ dspy.Suggest(result.answer == "blue", "Please think harder")+ return result++ program = QuestionAnswerer()+ program = assert_transform_module(+ program.map_named_predictors(dspy.Retry),+ functools.partial(backtrack_handler, max_backtracks=1),+ )++ result = program(question="What color is the sky?")++ assert result.answer == "blue"+ assert lm.get_convo(-1).endswith(+ 'Input: {"question":"What color is the sky?"}\n\n'+ 'Previous Output: {"answer":"red"}\n\n'+ "Instructions: Please think harder\n\n"+ 'Output: {"answer":"blue"}'+ )diff --git a/tests/dsp_LM/primitives/test_example.py b/tests/dsp_LM/primitives/test_example.pynew file mode 100644index 000000000..39ad0b749--- /dev/null+++ b/tests/dsp_LM/primitives/test_example.py@@ -0,0 +1,110 @@+import pytest++from dspy import Example+++def test_example_initialization():+ example = Example(a=1, b=2)+ assert example.a == 1+ assert example.b == 2+++def test_example_initialization_from_base():+ base = Example(a=1, b=2)+ example = Example(base=base, c=3)+ assert example.a == 1+ assert example.b == 2+ assert example.c == 3+++def test_example_initialization_from_dict():+ base_dict = {"a": 1, "b": 2}+ example = Example(base=base_dict, c=3)+ assert example.a == 1+ assert example.b == 2+ assert example.c == 3+++def test_example_set_get_item():+ example = Example()+ example["a"] = 1+ assert example["a"] == 1+++def test_example_attribute_access():+ example = Example(a=1)+ assert example.a == 1+ example.a = 2+ assert example.a == 2+++def test_example_deletion():+ example = Example(a=1, b=2)+ del example["a"]+ with pytest.raises(AttributeError):+ _ = example.a+++def test_example_len():+ example = Example(a=1, b=2, dspy_hidden=3)+ assert len(example) == 2+++def test_example_repr_str():+ example = Example(a=1)+ assert repr(example) == "Example({'a': 1}) (input_keys=None)"+ assert str(example) == "Example({'a': 1}) (input_keys=None)"+++def test_example_eq():+ example1 = Example(a=1, b=2)+ example2 = Example(a=1, b=2)+ assert example1 == example2+ assert example1 != ""+++def test_example_hash():+ example1 = Example(a=1, b=2)+ example2 = Example(a=1, b=2)+ assert hash(example1) == hash(example2)+++def test_example_keys_values_items():+ example = Example(a=1, b=2, dspy_hidden=3)+ assert set(example.keys()) == {"a", "b"}+ assert 1 in example.values()+ assert ("b", 2) in example.items()+++def test_example_get():+ example = Example(a=1, b=2)+ assert example.get("a") == 1+ assert example.get("c", "default") == "default"+++def test_example_with_inputs():+ example = Example(a=1, b=2).with_inputs("a")+ assert example._input_keys == {"a"}+++def test_example_inputs_labels():+ example = Example(a=1, b=2).with_inputs("a")+ inputs = example.inputs()+ assert inputs.toDict() == {"a": 1}+ labels = example.labels()+ assert labels.toDict() == {"b": 2}+++def test_example_copy_without():+ example = Example(a=1, b=2)+ copied = example.copy(c=3)+ assert copied.a == 1+ assert copied.c == 3+ without_a = copied.without("a")+ with pytest.raises(AttributeError):+ _ = without_a.a+++def test_example_to_dict():+ example = Example(a=1, b=2)+ assert example.toDict() == {"a": 1, "b": 2}diff --git a/tests/dsp_LM/primitives/test_module.py b/tests/dsp_LM/primitives/test_module.pynew file mode 100644index 000000000..4ba6df220--- /dev/null+++ b/tests/dsp_LM/primitives/test_module.py@@ -0,0 +1,49 @@+import threading++import dspy+++def test_deepcopy_basic():+ signature = dspy.Signature("q -> a")+ cot = dspy.ChainOfThought(signature)+ cot_copy = cot.deepcopy()+ assert len(cot.parameters()) == len(cot_copy.parameters())+ # Parameters should be different objects with the same values.+ assert id(cot.parameters()[0]) != id(cot_copy.parameters()[0])+ assert cot.parameters()[0].__dict__ == cot_copy.parameters()[0].__dict__+++def test_deepcopy_with_uncopyable_modules():+ class CustomClass(dspy.Module):+ def __init__(self):+ self.lock = threading.Lock() # Non-copyable object.+ self.cot = dspy.ChainOfThought(dspy.Signature("q -> a"))++ model = CustomClass()+ model_copy = model.deepcopy()+ assert len(model.parameters()) == len(model_copy.parameters())+ # The lock should be refer to the same object (shallow copy).+ assert id(model.lock) == id(model_copy.lock)+ # Parameters should be different objects with the same values.+ assert id(model.parameters()[0]) != id(model_copy.parameters()[0])+ assert model.parameters()[0].__dict__ == model_copy.parameters()[0].__dict__+++def test_deepcopy_with_nested_modules():+ class CustomClass1(dspy.Module):+ def __init__(self):+ self.lock = threading.Lock() # Non-copyable object.+ self.cot = dspy.ChainOfThought(dspy.Signature("q -> a"))++ class CustomClass2(dspy.Module):+ def __init__(self):+ self.submodel = CustomClass1()++ model = CustomClass2()+ model_copy = model.deepcopy()+ assert len(model.parameters()) == len(model_copy.parameters())+ # The lock should be refer to the same object (shallow copy).+ assert id(model.submodel.lock) == id(model_copy.submodel.lock)+ # Parameters should be different objects with the same values.+ assert id(model.parameters()[0]) != id(model_copy.parameters()[0])+ assert model.parameters()[0].__dict__ == model_copy.parameters()[0].__dict__diff --git a/tests/dsp_LM/primitives/test_program.py b/tests/dsp_LM/primitives/test_program.pynew file mode 100644index 000000000..dc817882a--- /dev/null+++ b/tests/dsp_LM/primitives/test_program.py@@ -0,0 +1,21 @@+import dspy+from dspy.primitives.program import Module, set_attribute_by_name # Adjust the import based on your file structure+from dspy.utils import DSPDummyLM+++class HopModule(dspy.Module):+ def __init__(self):+ super().__init__()+ self.predict1 = dspy.Predict("question -> query")+ self.predict2 = dspy.Predict("query -> answer")++ def forward(self, question):+ query = self.predict1(question=question).query+ return self.predict2(query=query)+++def test_forward():+ program = HopModule()+ dspy.settings.configure(lm=DSPDummyLM({"What is 1+1?": "let me check", "let me check": "2"}))+ result = program(question="What is 1+1?").answer+ assert result == "2"diff --git a/tests/dsp_LM/primitives/test_python_interpreter.py b/tests/dsp_LM/primitives/test_python_interpreter.pynew file mode 100644index 000000000..a1c071388--- /dev/null+++ b/tests/dsp_LM/primitives/test_python_interpreter.py@@ -0,0 +1,53 @@+import pytest++from dspy.primitives.python_interpreter import CodePrompt, PythonInterpreter, TextPrompt+++def test_execute_simple_code():+ interpreter = PythonInterpreter(action_space={"print": print})+ code = "print('Hello, World!')"+ result = interpreter.execute(code)+ assert result is None, "Simple print statement should return None"+++def test_action_space_limitation():+ def func(string):+ pass++ interpreter = PythonInterpreter(action_space={})+ code = "func('This should not execute')"+ with pytest.raises(Exception):+ interpreter.execute(code)+++def test_import_whitelist():+ interpreter = PythonInterpreter(action_space={}, import_white_list=["math"])+ code = "import math\nresult = math.sqrt(4)"+ result = interpreter.execute(code)+ assert result == 2, "Should be able to import and use math.sqrt"+++def test_fuzzy_variable_matching():+ interpreter = PythonInterpreter(action_space={})+ code = "result = number + 1"+ result = interpreter.execute(code, fuzz_state={"number": 4})+ assert result == 5, "Fuzzy variable matching should work"+++def test_text_prompt_keyword_extraction():+ prompt = TextPrompt("Hello {name}, how are you?")+ assert "name" in prompt.key_words, "Keyword 'name' should be extracted"+++def test_text_prompt_formatting():+ prompt = TextPrompt("Hello {name}, how are you?")+ formatted = prompt.format(name="Alice")+ assert formatted == "Hello Alice, how are you?", "Should format with provided value"+++def test_code_prompt_execution():+ action_space = {"len": len}+ interpreter = PythonInterpreter(action_space=action_space)+ code_prompt = CodePrompt("result = len('hello')")+ result, _ = code_prompt.execute(interpreter)+ assert result == 5, "Code execution should return the length of 'hello'"diff --git a/tests/dsp_LM/retrieve/integration_test_pgvectorrm.py b/tests/dsp_LM/retrieve/integration_test_pgvectorrm.pynew file mode 100644index 000000000..524209bea--- /dev/null+++ b/tests/dsp_LM/retrieve/integration_test_pgvectorrm.py@@ -0,0 +1,94 @@+"""Instructions:+Add to dev container features:+ "ghcr.io/itsmechlark/features/postgresql:1": {},+ "ghcr.io/robbert229/devcontainer-features/postgresql-client:1": {}+Add to .personalization.sh:+ sudo apt install -y postgresql-16-pgvector++ sudo /etc/init.d/postgresql restart++ psql -v ON_ERROR_STOP=1 --user ${PGUSER} <<EOF+ create extension if not exists vector;+ EOF+poetry install -E postgres+"""+import psycopg2+import pytest++from dspy.primitives.example import Example+from dspy.retrieve.pgvector_rm import PgVectorRM++DB_URL = "postgresql://postgres:password@localhost/postgres"+PG_TABLE_NAME = "test_table"+++def get_pgvectorrm():+ openai_client = None # Mock or use a real OpenAI client+ pgvectorrm = PgVectorRM(+ DB_URL, PG_TABLE_NAME, openai_client=openai_client, embedding_func=lambda x: "[2,3,4]", include_similarity=True+ )+ return pgvectorrm+++def setup_pgvectorrm():+ pgvectorrm = get_pgvectorrm()+ conn = psycopg2.connect(DB_URL)+ cursor = conn.cursor()+ cursor.execute(f"DROP TABLE IF EXISTS {PG_TABLE_NAME}")+ conn.commit()++ cursor.execute(f"CREATE TABLE IF NOT EXISTS {PG_TABLE_NAME} (id SERIAL PRIMARY KEY, text TEXT, embedding VECTOR)")+ cursor.execute(+ f"INSERT INTO {PG_TABLE_NAME} (text, embedding) VALUES ('Dummy text1', '[1,2,3]'), ('Dummy text2', '[4,5,6]')"+ )+ conn.commit()++ yield pgvectorrm++ cursor.execute(f"TRUNCATE TABLE {PG_TABLE_NAME}")+ conn.commit()++ cursor.close()+ conn.close()+++def test_pgvectorrm_retrieve(setup_pgvectorrm):+ pgvectorrm = setup_pgvectorrm+ query = "test query"+ results = pgvectorrm(query)+ assert len(results) == 2+ assert results == [+ Example(text="Dummy text2", similarity=0.9946115458726394),+ Example(text="Dummy text1", similarity=0.9925833339709302),+ ]+++ "k, expected",+ [+ (1, 1),+ (2, 2),+ (3, 2), # Assuming only 2 entries exist+ ],+)+def test_pgvectorrm_retrieve_diff_k(setup_pgvectorrm, k, expected):+ setup_pgvectorrm.k = k+ query = "test query"+ results = setup_pgvectorrm(query)+ assert len(results) == expected+++def test_empty_table():+ # Assuming setup_pgvectorrm cleans up after yielding+ query = "test query"+ results = get_pgvectorrm()(query)+ assert len(results) == 0+++def test_retrieval_without_similarity(setup_pgvectorrm):+ setup_pgvectorrm.include_similarity = False+ query = "test query"+ results = setup_pgvectorrm(query)+ # Ensure 'similarity' key is not in results+ assert all("similarity" not in result for result in results)diff --git a/tests/dsp_LM/retrieve/test_llama_index_rm.py b/tests/dsp_LM/retrieve/test_llama_index_rm.pynew file mode 100644index 000000000..497ed87fc--- /dev/null+++ b/tests/dsp_LM/retrieve/test_llama_index_rm.py@@ -0,0 +1,61 @@+import logging++import pytest++import dspy+from dsp.modules.dummy_lm import DspDummyLM+from dspy.datasets import HotPotQA++try:+ from llama_index.core import Settings, VectorStoreIndex+ from llama_index.core.base.base_retriever import BaseRetriever+ from llama_index.core.embeddings.mock_embed_model import MockEmbedding+ from llama_index.core.readers.string_iterable import StringIterableReader++ from dspy.retrieve.llama_index_rm import LlamaIndexRM++except ImportError:+ logging.info("Optional dependency llama-index is not installed - skipping LlamaIndexRM tests.")+++def rag_setup() -> dict:+ """Builds the necessary fixtures to test LI"""+ pytest.importorskip("llamaindex")+ dataset = HotPotQA(train_seed=1, train_size=8, eval_seed=2023, dev_size=4, test_size=0)+ trainset = [x.with_inputs("question") for x in dataset.train]+ devset = [x.with_inputs("question") for x in dataset.dev]+ ragset = [f"Question: {x.question} Answer: {x.answer}" for x in dataset.train]+ dummyset = {x.question: x.answer for x in dataset.train}++ Settings.embed_model = MockEmbedding(8)+ docs = StringIterableReader().load_data(texts=ragset)+ index = VectorStoreIndex.from_documents(documents=docs)+ retriever = index.as_retriever()+ rm = LlamaIndexRM(retriever)++ return {+ "index": index,+ "retriever": retriever,+ "rm": rm,+ "lm": DspDummyLM(answers=dummyset),+ "trainset": trainset,+ "devset": devset,+ }+++def test_lirm_as_rm(rag_setup):+ """Test the retriever as retriever method"""+ pytest.importorskip("llamaindex")+ retriever = rag_setup.get("retriever")+ test_res_li = retriever.retrieve("At My Window was released by which American singer-songwriter?")+ rm = rag_setup.get("rm")+ test_res_dspy = rm.forward("At My Window was released by which American singer-songwriter?")++ assert isinstance(retriever, BaseRetriever), "Ensuring that the retriever is a LI Retriever object"+ assert isinstance(test_res_li, list), "Ensuring results are a list from LI Retriever"++ assert isinstance(rm, dspy.Retrieve), "Ensuring the RM is a retriever object from dspy"+ assert isinstance(test_res_dspy, list), "Ensuring the results are a list from the DSPy retriever"++ assert len(test_res_li) == len(test_res_dspy), "Rough equality check of the results"diff --git a/tests/dsp_LM/signatures/test_signature.py b/tests/dsp_LM/signatures/test_signature.pynew file mode 100644index 000000000..fffa58ab2--- /dev/null+++ b/tests/dsp_LM/signatures/test_signature.py@@ -0,0 +1,41 @@+import textwrap+from typing import List++import pydantic+import pytest++import dspy+from dspy import InputField, OutputField, Signature, infer_prefix+from dspy.utils.dummies import DSPDummyLM+++def test_multiline_instructions():+ class MySignature(Signature):+ """First line+ Second line+ Third line"""++ output = OutputField()++ predictor = dspy.Predict(MySignature)++ lm = DSPDummyLM(["short answer"])+ dspy.settings.configure(lm=lm)+ assert predictor().output == "short answer"++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ First line+ Second line+ Third line++ ---++ Follow the following format.++ Output: ${output}++ ---++ Output: short answer"""+ )diff --git a/tests/dsp_LM/teleprompt/test_bootstrap.py b/tests/dsp_LM/teleprompt/test_bootstrap.pynew file mode 100644index 000000000..6bc41ec61--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_bootstrap.py@@ -0,0 +1,156 @@+import textwrap++import pytest++import dspy+from dspy import Example+from dspy.predict import Predict+from dspy.teleprompt import BootstrapFewShot+from dspy.utils.dummies import DSPDummyLM+++# Define a simple metric function for testing+def simple_metric(example, prediction, trace=None):+ # Simplified metric for testing: true if prediction matches expected output+ return example.output == prediction.output+++examples = [+ Example(input="What is the color of the sky?", output="blue").with_inputs("input"),+ Example(input="What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!"),+]+trainset = [examples[0]]+valset = [examples[1]]+++class SimpleModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ self.predictor = Predict(signature)++ def forward(self, **kwargs):+ return self.predictor(**kwargs)+++def test_compile_with_predict_instances():+ # Create Predict instances for student and teacher+ # Note that dspy.Predict is not itself a module, so we can't use it directly here+ student = SimpleModule("input -> output")+ teacher = SimpleModule("input -> output")++ lm = DSPDummyLM(["Initial thoughts", "Finish[blue]"])+ dspy.settings.configure(lm=lm)++ # Initialize BootstrapFewShot and compile the student+ bootstrap = BootstrapFewShot(metric=simple_metric, max_bootstrapped_demos=1, max_labeled_demos=1)+ compiled_student = bootstrap.compile(student, teacher=teacher, trainset=trainset)++ assert compiled_student is not None, "Failed to compile student"+ assert hasattr(compiled_student, "_compiled") and compiled_student._compiled, "Student compilation flag not set"+++def test_bootstrap_effectiveness():+ # This test verifies if the bootstrapping process improves the student's predictions+ student = SimpleModule("input -> output")+ teacher = SimpleModule("input -> output")+ lm = DSPDummyLM(["blue", "Ring-ding-ding-ding-dingeringeding!"], follow_examples=True)+ dspy.settings.configure(lm=lm, trace=[])++ bootstrap = BootstrapFewShot(metric=simple_metric, max_bootstrapped_demos=1, max_labeled_demos=1)+ compiled_student = bootstrap.compile(student, teacher=teacher, trainset=trainset)++ # Check that the compiled student has the correct demos+ assert len(compiled_student.predictor.demos) == 1+ assert compiled_student.predictor.demos[0].input == trainset[0].input+ assert compiled_student.predictor.demos[0].output == trainset[0].output++ # Test the compiled student's prediction.+ # We are using a DspDummyLM with follow_examples=True, which means that+ # even though it would normally reply with "Ring-ding-ding-ding-dingeringeding!"+ # on the second output, if it seems an example that perfectly matches the+ # prompt, it will use that instead. That is why we expect "blue" here.+ prediction = compiled_student(input=trainset[0].input)+ assert prediction.output == trainset[0].output++ # For debugging+ print("Convo")+ print(lm.get_convo(-1))++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Given the fields `input`, produce the fields `output`.++ ---++ Follow the following format.++ Input: ${input}+ Output: ${output}++ ---++ Input: What is the color of the sky?+ Output: blue++ ---++ Input: What is the color of the sky?+ Output: blue"""+ )+++def test_error_handling_during_bootstrap():+ """+ Test to verify error handling during the bootstrapping process+ """++ class BuggyModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ self.predictor = Predict(signature)++ def forward(self, **kwargs):+ raise RuntimeError("Simulated error")++ student = SimpleModule("input -> output")+ teacher = BuggyModule("input -> output")++ # Setup DspDummyLM to simulate an error scenario+ lm = DSPDummyLM(+ [+ "Initial thoughts", # Simulate initial teacher's prediction+ ]+ )+ dspy.settings.configure(lm=lm)++ bootstrap = BootstrapFewShot(+ metric=simple_metric,+ max_bootstrapped_demos=1,+ max_labeled_demos=1,+ max_errors=1,+ )++ with pytest.raises(RuntimeError, match="Simulated error"):+ bootstrap.compile(student, teacher=teacher, trainset=trainset)+++def test_validation_set_usage():+ """+ Test to ensure the validation set is correctly used during bootstrapping+ """+ student = SimpleModule("input -> output")+ teacher = SimpleModule("input -> output")++ lm = DSPDummyLM(+ [+ "Initial thoughts",+ "Finish[blue]", # Expected output for both training and validation+ ]+ )+ dspy.settings.configure(lm=lm)++ bootstrap = BootstrapFewShot(metric=simple_metric, max_bootstrapped_demos=1, max_labeled_demos=1)+ compiled_student = bootstrap.compile(student, teacher=teacher, trainset=trainset)++ # Check that validation examples are part of student's demos after compilation+ assert len(compiled_student.predictor.demos) >= len(valset), "Validation set not used in compiled student demos"diff --git a/tests/dsp_LM/teleprompt/test_copro_optimizer.py b/tests/dsp_LM/teleprompt/test_copro_optimizer.pynew file mode 100644index 000000000..50011eecc--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_copro_optimizer.py@@ -0,0 +1,149 @@+import textwrap++import pytest++import dspy+from dspy import Example+from dspy.teleprompt.signature_opt import COPRO+from dspy.utils.dummies import DSPDummyLM+++# Define a simple metric function for testing+def simple_metric(example, prediction):+ # Simplified metric for testing: true if prediction matches expected output+ return example.output == prediction.output+++# Example training and validation sets+trainset = [+ Example(input="Question: What is the color of the sky?", output="blue").with_inputs("input"),+ Example(input="Question: What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!").with_inputs(+ "input"+ ),+]+++def test_signature_optimizer_initialization():+ optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)+ assert optimizer.metric == simple_metric, "Metric not correctly initialized"+ assert optimizer.breadth == 2, "Breadth not correctly initialized"+ assert optimizer.depth == 1, "Depth not correctly initialized"+ assert optimizer.init_temperature == 1.4, "Initial temperature not correctly initialized"+++class SimpleModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ # COPRO doesn't work with dspy.Predict+ self.predictor = dspy.ChainOfThought(signature)++ def forward(self, **kwargs):+ return self.predictor(**kwargs)+++def test_signature_optimizer_optimization_process():+ optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)+ dspy.settings.configure(lm=DSPDummyLM(["Optimized instruction 1", "Optimized instruction 2"]))++ student = SimpleModule("input -> output")++ # Assuming the compile method of COPRO requires a student module, a development set, and evaluation kwargs+ optimized_student = optimizer.compile(+ student, trainset=trainset, eval_kwargs={"num_threads": 1, "display_progress": False}+ )++ # Check that the optimized student has been modified from the original+ # This check can be more specific based on how the optimization modifies the student+ assert optimized_student is not student, "Optimization did not modify the student"++ # Further tests can be added to verify the specifics of the optimization process,+ # such as checking the instructions of the optimized student's predictors.+++def test_signature_optimizer_statistics_tracking():+ optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)+ optimizer.track_stats = True # Enable statistics tracking++ dspy.settings.configure(lm=DSPDummyLM(["Optimized instruction"]))+ student = SimpleModule("input -> output")+ optimized_student = optimizer.compile(+ student, trainset=trainset, eval_kwargs={"num_threads": 1, "display_progress": False}+ )++ # Verify that statistics have been tracked and attached to the optimized student+ assert hasattr(optimized_student, "total_calls"), "Total calls statistic not tracked"+ assert hasattr(optimized_student, "results_best"), "Best results statistics not tracked"+++# Assuming the setup_signature_optimizer fixture and simple_metric function are defined as before+++def test_optimization_and_output_verification():+ lm = DSPDummyLM(+ [+ "Optimized Prompt",+ "Optimized Prefix",+ ]+ )+ dspy.settings.configure(lm=lm)+ optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)++ student = SimpleModule("input -> output")++ # Compile the student with the optimizer+ optimized_student = optimizer.compile(+ student, trainset=trainset, eval_kwargs={"num_threads": 1, "display_progress": False}+ )++ # Simulate calling the optimized student with a new input+ test_input = "What is the capital of France?"+ prediction = optimized_student(input=test_input)++ print(lm.get_convo(-1))++ assert prediction.output == "No more responses"++ assert lm.get_convo(-1) == textwrap.dedent(+ """\+ Optimized Prompt++ ---++ Follow the following format.++ Input: ${input}+ Reasoning: Let's think step by step in order to ${produce the output}. We ...+ Optimized Prefix ${output}++ ---++ Input: What is the capital of France?+ Reasoning: Let's think step by step in order to No more responses+ Optimized Prefix No more responses"""+ )+++def test_statistics_tracking_during_optimization():+ dspy.settings.configure(lm=DSPDummyLM(["Optimized instruction for stats tracking"]))++ optimizer = COPRO(metric=simple_metric, breadth=2, depth=1, init_temperature=1.4)+ optimizer.track_stats = True # Enable statistics tracking++ student = SimpleModule("input -> output")+ optimized_student = optimizer.compile(+ student, trainset=trainset, eval_kwargs={"num_threads": 1, "display_progress": False}+ )++ # Verify that statistics have been tracked+ assert hasattr(optimized_student, "total_calls"), "Optimizer did not track total metric calls"+ assert optimized_student.total_calls > 0, "Optimizer reported no metric calls"++ # Check if the results_best and results_latest contain valid statistics+ assert "results_best" in optimized_student.__dict__, "Optimizer did not track the best results"+ assert "results_latest" in optimized_student.__dict__, "Optimizer did not track the latest results"+ assert len(optimized_student.results_best) > 0, "Optimizer did not properly populate the best results statistics"+ assert (+ len(optimized_student.results_latest) > 0+ ), "Optimizer did not properly populate the latest results statistics"++ # Additional detailed checks can be added here to verify the contents of the tracked statisticsdiff --git a/tests/dsp_LM/teleprompt/test_ensemble.py b/tests/dsp_LM/teleprompt/test_ensemble.pynew file mode 100644index 000000000..c779e8782--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_ensemble.py@@ -0,0 +1,59 @@+import pytest++import dspy+from dspy.teleprompt import Ensemble+++class MockProgram(dspy.Module):+ def __init__(self, output):+ super().__init__()+ self.output = output++ def forward(self, *args, **kwargs):+ return self.output+++# Simple reduction function to test with+def mock_reduce_fn(outputs):+ return sum(outputs) / len(outputs)+++def test_ensemble_without_reduction():+ """Test that Ensemble correctly combines outputs without applying a reduce_fn."""+ programs = [MockProgram(i) for i in range(5)]+ ensemble = Ensemble()+ ensembled_program = ensemble.compile(programs)++ outputs = ensembled_program()+ assert len(outputs) == 5, "Ensemble did not combine the correct number of outputs"+++def test_ensemble_with_reduction():+ """Test that Ensemble correctly applies a reduce_fn to combine outputs."""+ programs = [MockProgram(i) for i in range(5)]+ ensemble = Ensemble(reduce_fn=mock_reduce_fn)+ ensembled_program = ensemble.compile(programs)++ output = ensembled_program()+ expected_output = sum(range(5)) / 5+ assert output == expected_output, "Ensemble did not correctly apply the reduce_fn"+++def test_ensemble_with_size_limitation():+ """Test that specifying a size limits the number of programs used in the ensemble."""+ programs = [MockProgram(i) for i in range(10)]+ ensemble_size = 3+ ensemble = Ensemble(size=ensemble_size)+ ensembled_program = ensemble.compile(programs)++ outputs = ensembled_program()+ assert len(outputs) == ensemble_size, "Ensemble did not respect the specified size limitation"+++def test_ensemble_deterministic_behavior():+ """Verify that the Ensemble class raises an assertion for deterministic behavior."""+ with pytest.raises(+ AssertionError,+ match="TODO: Implement example hashing for deterministic ensemble.",+ ):+ Ensemble(deterministic=True)diff --git a/tests/dsp_LM/teleprompt/test_knn_fewshot.py b/tests/dsp_LM/teleprompt/test_knn_fewshot.pynew file mode 100644index 000000000..771e01d55--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_knn_fewshot.py@@ -0,0 +1,65 @@+import pytest++import dsp+import dspy+from dspy.teleprompt.knn_fewshot import KNNFewShot+from dspy.utils.dummies import DSPDummyLM, DummyVectorizer+++def mock_example(question: str, answer: str) -> dsp.Example:+ """Creates a mock DSP example with specified question and answer."""+ return dspy.Example(question=question, answer=answer).with_inputs("question")+++def setup_knn_few_shot():+ """Sets up a KNNFewShot instance for testing."""+ trainset = [+ mock_example("What is the capital of France?", "Paris"),+ mock_example("What is the largest ocean?", "Pacific"),+ mock_example("What is 2+2?", "4"),+ ]+ dsp.SentenceTransformersVectorizer = DummyVectorizer+ knn_few_shot = KNNFewShot(k=2, trainset=trainset)+ return knn_few_shot+++class SimpleModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ self.predictor = dspy.Predict(signature)++ def forward(self, *args, **kwargs):+ return self.predictor(**kwargs)++ def reset_copy(self):+ # Creates a new instance of SimpleModule with the same predictor+ return SimpleModule(self.predictor.signature)+++# TODO: Test not working yet+def _test_knn_few_shot_compile(setup_knn_few_shot):+ """Tests the compile method of KNNFewShot with SimpleModule as student."""+ student = SimpleModule("input -> output")+ teacher = SimpleModule("input -> output") # Assuming teacher uses the same module type++ # Setup DspDummyLM with a response for a query similar to one of the training examples+ lm = DSPDummyLM(["Madrid", "10"])+ dspy.settings.configure(lm=lm) # Responses for the capital of Spain and the result of 5+5)++ knn_few_shot = setup_knn_few_shot+ trainset = knn_few_shot.KNN.trainset+ compiled_student = knn_few_shot.compile(student, teacher=teacher, trainset=trainset, valset=None)++ assert len(compiled_student.predictor.demos) == 1+ assert compiled_student.predictor.demos[0].input == trainset[0].input+ assert compiled_student.predictor.demos[0].output == trainset[0].output+ # Simulate a query that is similar to one of the training examples+ output = compiled_student.forward(input="What is the capital of Spain?").output++ print("CONVO")+ print(lm.get_convo(-1))++ # Validate that the output corresponds to one of the expected DspDummyLM responses+ # This assumes the compiled_student's forward method will execute the predictor with the given query+ assert output in ["Madrid", "10"], "The compiled student did not return the correct output based on the query"diff --git a/tests/dsp_LM/teleprompt/test_mipro_optimizer.py b/tests/dsp_LM/teleprompt/test_mipro_optimizer.pynew file mode 100644index 000000000..99f4ede21--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_mipro_optimizer.py@@ -0,0 +1,254 @@+import re+import textwrap++import pytest++import dspy+from dsp.modules import LM+from dspy import Example+from dspy.teleprompt.signature_opt_bayesian import MIPRO+from dspy.utils.dummies import DSPDummyLM+++# Define a simple metric function for testing+def simple_metric(example, prediction, trace=None):+ # Simplified metric for testing: true if prediction matches expected output+ return example.output == prediction.output+++# Some example data+capitals = {+ "Germany": "Berlin",+ "France": "Paris",+ "Denmark": "Copenhagen",+ "Sweden": "Stockholm",+ "Norway": "Oslo",+}+# Not used for training data+extra_capitals = {+ "Spain": "Madrid",+ "Portugal": "Lisbon",+ "Italy": "Rome",+}++# Example training and validation sets+trainset = [+ Example(input="What is the color of the sky?", output="blue").with_inputs("input"),+ Example(input="What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!").with_inputs("input"),+] + [+ Example(input=f"What is the capital of {country}?", output=capital).with_inputs("input")+ for country, capital in capitals.items()+]+++class ConditionalLM(LM):+ def __init__(self):+ super().__init__("conditional-lm")++ def basic_request(self, prompt, num_candidates=1, **kwargs):+ # If we are in the "optimization" stage, we don't say much.+ if prompt.endswith("Observations:"):+ answer = " (*silence*)"+ elif prompt.endswith("Proposed Instruction:"):+ answer = " Input: "+ elif prompt.endswith("Proposed Prefix For Output Field:"):+ answer = " Output: "+ elif prompt.endswith("Summary:"):+ answer = " summarizing..."+ else:+ pairs = re.findall(r"Input: (.*?)\n(?:Reasoning:.*?\n)?Output: (.*?)\n", prompt, re.DOTALL)++ # breakpoint()+ print("PROMPT:", prompt)+ print("PAIRS:", pairs)++ last = re.search(r"Input: (.*)\nReasoning: (.*)$", prompt)+ current_question = last.group(1)++ if match := re.match(r"What is the capital of (.*?)\?", current_question):+ country = match.group(1)+ # If we had a previous example of a question about a capital, the model+ # has learned the format, and will answer with question correctly.+ if any("capital" in question for question, _ in pairs):+ answer = (capitals | extra_capitals)[country]+ # Otherwise, it is confused and will answer with the country's name.+ else:+ answer = country+ # For other questions, the model will answer with the last word of the question.+ else:+ answer = current_question.split()[-1]++ answer = "think deeply.\nOutput: " + answer++ RED, GREEN, RESET = "\033[91m", "\033[92m", "\033[0m"+ print("=== DspDummyLM ===")+ print(prompt, end="")+ print(f"{RED}{answer}{RESET}")+ print("===")++ dummy_response = {"choices": []}+ for _ in range(num_candidates):+ dummy_response["choices"].append(+ {+ "text": answer,+ "finish_reason": "done",+ }+ )++ # Simulate processing and storing the request and response.+ history_entry = {+ "prompt": prompt,+ "response": dummy_response,+ "kwargs": kwargs,+ "raw_kwargs": kwargs,+ }+ self.history.append(history_entry)++ return dummy_response++ def __call__(self, prompt, only_completed=True, return_sorted=False, **kwargs):+ response = self.basic_request(prompt, **kwargs)+ return [choice["text"] for choice in response["choices"]]++ def get_convo(self, index):+ """get the prompt + anwer from the ith message"""+ return self.history[index]["prompt"] + " " + self.history[index]["response"]["choices"][0]["text"]+++class SimpleModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ # SignatureOptimizer doesn't work with dspy.Predict+ self.predictor = dspy.ChainOfThought(signature)++ def forward(self, **kwargs):+ return self.predictor(**kwargs)+++def test_signature_optimizer_optimization_process():+ lm = ConditionalLM()+ dspy.settings.configure(lm=lm)++ student = SimpleModule(signature="input -> output")++ optimizer = MIPRO(+ metric=simple_metric,+ num_candidates=10,+ init_temperature=1.4,+ verbose=False,+ track_stats=False,+ )++ # Adjustments: Include required parameters for the compile method+ optimized_student = optimizer.compile(+ student=student,+ trainset=trainset,+ num_trials=10,+ max_bootstrapped_demos=3,+ max_labeled_demos=5,+ eval_kwargs={"num_threads": 1, "display_progress": False},+ requires_permission_to_run=False,+ )++ assert len(optimized_student.predictor.demos) == 5+++def test_signature_optimizer_bad_lm():+ dspy.settings.configure(lm=DSPDummyLM([f"Optimized instruction {i}" for i in range(30)]))+ student = SimpleModule(signature="input -> output")+ optimizer = MIPRO(+ metric=simple_metric,+ num_candidates=10,+ init_temperature=1.4,+ verbose=False,+ track_stats=False,+ )++ # Krista: when the code tries to generate bootstrapped examples, the examples are generated using DspDummyLM,+ # which only outputs "Optimized instruction i" this means that none of the bootstrapped examples are successful,+ # and therefore the set of examples that we're using to generate new prompts is empty+ with pytest.raises(ValueError):+ _optimized_student = optimizer.compile(+ student=student,+ trainset=trainset,+ num_trials=10,+ max_bootstrapped_demos=3,+ max_labeled_demos=5,+ eval_kwargs={"num_threads": 1, "display_progress": False},+ requires_permission_to_run=False,+ )+++def test_optimization_and_output_verification():+ # Make a language model that is always right, except on the last+ # example in the train set.+ lm = ConditionalLM()+ dspy.settings.configure(lm=lm)++ optimizer = MIPRO(+ metric=simple_metric,+ num_candidates=10,+ init_temperature=1.4,+ verbose=False,+ track_stats=True,+ )++ student = SimpleModule("input -> output")++ # Compile the student with the optimizer+ optimized_student = optimizer.compile(+ student=student,+ trainset=trainset,+ num_trials=4,+ max_bootstrapped_demos=2,+ max_labeled_demos=3,+ eval_kwargs={"num_threads": 1, "display_progress": False},+ requires_permission_to_run=False,+ )++ # Simulate calling the optimized student with a new input+ test_input = "What is the capital of Spain?"+ prediction = optimized_student(input=test_input)++ print("CORRECT ANSWER")+ print(lm.get_convo(-1))++ assert prediction.output == "Madrid"++ expected_lm_output = textwrap.dedent(+ """\+ Input:++ ---++ Follow the following format.++ Input: ${input}+ Reasoning: Let's think step by step in order to ${produce the output}. We ...+ Output: ${output}++ ---++ Input: What is the capital of France?+ Reasoning: Let's think step by step in order to think deeply.+ Output: Paris++ ---++ Input: What is the capital of Norway?+ Reasoning: Let's think step by step in order to think deeply.+ Output: Oslo++ ---++ Input: What does the fox say?+ Output: Ring-ding-ding-ding-dingeringeding!++ ---++ Input: What is the capital of Spain?+ Reasoning: Let's think step by step in order to think deeply.+ Output: Madrid"""+ )++ assert lm.get_convo(-1) == expected_lm_outputdiff --git a/tests/dsp_LM/teleprompt/test_random_search.py b/tests/dsp_LM/teleprompt/test_random_search.pynew file mode 100644index 000000000..9d8e63d23--- /dev/null+++ b/tests/dsp_LM/teleprompt/test_random_search.py@@ -0,0 +1,39 @@+import dspy+from dspy import Example+from dspy.predict import Predict+from dspy.teleprompt import BootstrapFewShotWithRandomSearch+from dspy.utils.dummies import DSPDummyLM+++class SimpleModule(dspy.Module):+ def __init__(self, signature):+ super().__init__()+ self.predictor = Predict(signature)++ def forward(self, **kwargs):+ return self.predictor(**kwargs)+++def simple_metric(example, prediction, trace=None):+ return example.output == prediction.output+++def test_basic_workflow():+ """Test to ensure the basic compile flow runs without errors."""+ student = SimpleModule("input -> output")+ teacher = SimpleModule("input -> output")++ lm = DSPDummyLM(+ [+ "Initial thoughts",+ "Finish[blue]", # Expected output for both training and validation+ ]+ )+ dspy.settings.configure(lm=lm)++ optimizer = BootstrapFewShotWithRandomSearch(metric=simple_metric, max_bootstrapped_demos=1, max_labeled_demos=1)+ trainset = [+ Example(input="What is the color of the sky?", output="blue").with_inputs("input"),+ Example(input="What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!").with_inputs("input"),+ ]+ optimizer.compile(student, teacher=teacher, trainset=trainset)