Skip to content

Commit

Permalink
Upgraded to dspy-ai==2.5.7 resolved ValueError via inclusion of basic…
Browse files Browse the repository at this point in the history
… prompt instruction for critique signature and typed CoT
  • Loading branch information
NumberChiffre committed Oct 16, 2024
1 parent 5372949 commit 9a31a8f
Show file tree
Hide file tree
Showing 7 changed files with 63,624 additions and 501 deletions.
20 changes: 14 additions & 6 deletions mcts_llm/mctsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class CritiqueAnswer(dspy.Signature):


class RefineAnswer(dspy.Signature):
"""[[ ## proposed_instruction ## ]] Given a mathematical problem, a current answer, and a critique of that answer,
refine the current answer to provide a more accurate and well-reasoned solution. Begin by carefully analyzing the
problem and the critique, then think step by step to derive the correct answer. Ensure that your reasoning is clear
and logical, and that the final answer is justified by the steps taken.
[[ ## completed ## ]]
"""

problem: str = dspy.InputField()
current_answer: str = dspy.InputField()
critique: str = dspy.InputField()
Expand All @@ -59,7 +67,7 @@ class EvaluateAnswer(dspy.Signature):

class ZeroShotCoT(dspy.Module):
def __init__(self):
self.cot = dspy.ChainOfThought(ZeroShotAnswer)
self.cot = dspy.TypedChainOfThought(ZeroShotAnswer)

def forward(self, problem) -> dspy.Prediction:
return dspy.Prediction(answer=self.cot(problem=problem).answer)
Expand All @@ -69,8 +77,8 @@ class MultipleTurnSelfRefine(dspy.Module):
def __init__(self, num_turns: int = 1):
super().__init__()
self.zero_shot_cot = ZeroShotCoT()
self.critique_answer = dspy.ChainOfThought(CritiqueAnswer)
self.refine_answer = dspy.ChainOfThought(RefineAnswer)
self.critique_answer = dspy.TypedChainOfThought(CritiqueAnswer)
self.refine_answer = dspy.TypedChainOfThought(RefineAnswer)
self.num_turns = num_turns

def forward(self, problem) -> dspy.Prediction:
Expand Down Expand Up @@ -140,9 +148,9 @@ def __init__(
self.samples_per_node = samples_per_node

self.zero_shot = ZeroShotCoT()
self.critique = dspy.ChainOfThought(CritiqueAnswer)
self.evaluate = dspy.ChainOfThought(EvaluateAnswer)
self.refine = dspy.ChainOfThought(RefineAnswer)
self.critique = dspy.TypedChainOfThought(CritiqueAnswer)
self.evaluate = dspy.TypedChainOfThought(EvaluateAnswer)
self.refine = dspy.TypedChainOfThought(RefineAnswer)

def initialize(self, S: MCTSrState) -> MCTSrNode:
if self.initialize_strategy == InitializeStrategy.ZERO_SHOT:
Expand Down
1,524 changes: 1,524 additions & 0 deletions notebooks/evaluate_qwen25-7b-instruct.ipynb

Large diffs are not rendered by default.

60,572 changes: 60,572 additions & 0 deletions notebooks/finetune_qwen25-7b-instruct.ipynb

Large diffs are not rendered by default.

444 changes: 444 additions & 0 deletions notebooks/miprov2_mctsr_qwen25-7b-instruct.json

Large diffs are not rendered by default.

1,557 changes: 1,066 additions & 491 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.12,<3.13"
dspy-ai = "2.4.17"
dspy-ai = "^2.5.6"
python-dotenv = "^1.0.1"

[tool.poetry.group.dev.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_mctsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def mock_parse_integer_answer():

@pytest.fixture
def mock_chain_of_thought():
with patch("dspy.ChainOfThought") as mock:
with patch("dspy.TypedChainOfThought") as mock:
mock_instance = Mock()
mock_instance.return_value = Mock(answer="Mocked answer", critique="Mock critique")
mock.return_value = mock_instance
Expand Down Expand Up @@ -57,7 +57,7 @@ def mctsr(mock_parse_integer_answer, mock_chain_of_thought):


def test_zero_shot_cot():
with patch("mcts_llm.mctsr.dspy.ChainOfThought") as mock_chain_of_thought:
with patch("mcts_llm.mctsr.dspy.TypedChainOfThought") as mock_chain_of_thought:
mock_cot = Mock()
mock_chain_of_thought.return_value = mock_cot
mock_cot.return_value = dspy.Prediction(answer="Test answer")
Expand All @@ -72,7 +72,7 @@ def test_zero_shot_cot():

@pytest.mark.parametrize("num_turns", [1, 3])
def test_multiple_turn_self_refine(num_turns):
with patch("mcts_llm.mctsr.dspy.ChainOfThought") as mock_chain_of_thought:
with patch("mcts_llm.mctsr.dspy.TypedChainOfThought") as mock_chain_of_thought:
mock_zero_shot = Mock()
mock_critique = Mock()
mock_refine = Mock()
Expand Down

0 comments on commit 9a31a8f

Please sign in to comment.