stanfordnlp / dspy

DSPy: The framework for programming—not prompting—foundation models
https://dspy-docs.vercel.app/
MIT License
18.28k stars 1.4k forks source link

Optimization: BootstrapFewShotWithRandomSearch with ChainOfThought student generates: no attribute `reset_copy` #834

Closed wodecki closed 6 months ago

wodecki commented 6 months ago

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File [/Users/wodecki/Dysk](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk) Google/1. Projects/1. Research/! LLM/KR - ewaluacja raportów rynkowych/S3. Klasyfikacja rekomendacji/1. is_forecast/3. DSPy/4.1.py:1
----> [1](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/!%20LLM/KR%20-%20ewaluacja%20raport%C3%B3w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/4.1.py:1) your_dspy_program_compiled = fewshot_optimizer.compile(student = check_if_forecast, trainset=trainset, valset=devset)

File [~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:66](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:66), in BootstrapFewShotWithRandomSearch.compile(self, student, teacher, trainset, valset, restrict, labeled_sample)
     [62](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:62) trainset2 = list(self.trainset)
     [64](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:64) if seed == -3:
     [65](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:65)     # zero-shot
---> [66](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:66)     program2 = student.reset_copy()
     [68](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:68) elif seed == -2:
     [69](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:69)     # labels only
     [70](https://file+.vscode-resource.vscode-cdn.net/Users/wodecki/Dysk%20Google/1.%20Projects/1.%20Research/%21%20LLM/KR%20-%20ewaluacja%20raporto%CC%81w%20rynkowych/S3.%20Klasyfikacja%20rekomendacji/1.%20is_forecast/3.%20DSPy/~/miniconda3/envs/langchain/lib/python3.12/site-packages/dspy/teleprompt/random_search.py:70)     teleprompter = LabeledFewShot(k=self.max_labeled_demos)

AttributeError: 'ChainOfThought' object has no attribute 'reset_copy'

Code to reproduce:

# Module
class is_forecast(dspy.Signature):
    """Determine whether the text is a forecast or not? Return Y - when the text is a forecast or N - when the text is not a forecast."""

    text = dspy.InputField()
    answer = dspy.OutputField(desc="Y lub N")

check_if_forecast = dspy.ChainOfThought(is_forecast)

# Evaluation
from dspy.evaluate.evaluate import Evaluate
evaluate = Evaluate(devset=devset, num_threads=1, display_progress=True, display_table=5)
# Evaluate the program with the `answer_exact_match` metric.
metric = dspy.evaluate.answer_exact_match

# Optimization
from dspy.teleprompt import BootstrapFewShotWithRandomSearch

fewshot_optimizer = BootstrapFewShotWithRandomSearch(metric=metric, max_bootstrapped_demos=2, num_candidate_programs=8, num_threads=6)

your_dspy_program_compiled = fewshot_optimizer.compile(student = check_if_forecast, trainset=trainset, valset=devset)
wodecki commented 6 months ago

Problem solved. The reason was an incorrect script. Correct one is below:

class is_forecast(dspy.Signature):
    """Określ, czy dany tekst jest prognozą czy nie? Zwróć Y - gdy tekst jest prognozą lub N - gdy tekst nie jest prognozą."""

    text = dspy.InputField()
    answer = dspy.OutputField(desc="Y lub N")

class check_if_forecast(dspy.Module):
    def __init__(self):
        super().__init__()
        self.prog = dspy.ChainOfThought(is_forecast)

    def forward(self, text):
        return self.prog(text=text)

program = check_if_forecast()

# Evaluation
from dspy.evaluate.evaluate import Evaluate
evaluate = Evaluate(devset=devset, num_threads=1, display_progress=True, display_table=5)
# Evaluate the program with the `answer_exact_match` metric.
metric = dspy.evaluate.answer_exact_match

# Optimization
from dspy.teleprompt import BootstrapFewShotWithRandomSearch

fewshot_optimizer = BootstrapFewShotWithRandomSearch(metric=metric, max_bootstrapped_demos=2, num_candidate_programs=8, num_threads=6)

your_dspy_program_compiled = fewshot_optimizer.compile(student = program, trainset=trainset, valset=devset)