Skip to main content

dspy.ProgramOfThought

Constructor

The constructor initializes the ProgramOfThought class and sets up its attributes. It is designed to generate and execute Python code based on input fields, producing a single output field through iterative refinement. It supports multiple iterations to refine the code in case of errors.

import dsp
import dspy
from ..primitives.program import Module
from ..primitives.python_interpreter import CodePrompt, PythonInterpreter
import re

class ProgramOfThought(Module):
def __init__(self, signature, max_iters=3):
...

Parameters:

  • signature (dspy.Signature): Signature defining the input and output fields for the program.
  • max_iters (int, optional): Maximum number of iterations for refining the generated code. Defaults to 3.

Methods

_generate_signature(self, mode)

Generates a signature dict for different modes: generate, regenerate, and answer.

The generate mode serves as an initial generation of Python code with the signature (question -> generated_code). The regenerate mode serves as a refining generation of Python code, accounting for the past generated code and existing error with the signature (question, previous_code, error -> generated_code). The answer mode serves to execute the last stored generated code and output the final answer to the question with the signature (question, final_generated_code, code_output -> answer).

Parameters:

  • mode (str): Mode of operation of Program of Thought.

Returns:

  • A dictionary representing the signature for specified mode.

_generate_instruction(self, mode)

Generates instructions for code generation based on the mode. This ensures the signature accounts for relevant instructions in generating an answer to the inputted question by producing executable Python code.

The instructional modes mirror the signature modes: generate, regenerate, answer

Parameters:

  • mode (str): Mode of operation.

Returns:

  • A string representing instructions for specified mode.

parse_code(self, code_data)

Parses the generated code and checks for formatting errors.

Parameters:

  • code_data (dict): Data containing the generated code. key - generated_code, val - {Python code string}

Returns:

  • Tuple containing parsed code and any error message.

execute_code(self, code)

Executes the parsed code and captures the output or error.

Parameters:

  • code (str): Code to be executed.

Returns:

  • Tuple containing the code, its output, and any error message.

forward(self, **kwargs)

Main method to execute the code generation and refinement process.

Parameters:

  • **kwargs: Keyword arguments corresponding to input fields.

Returns:

  • The final answer generated by the program or None in case of persistent errors.

Examples

#Define a simple signature for basic question answering
class GenerateAnswer(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")

# Pass signature to ProgramOfThought Module
pot = dspy.ProgramOfThought(GenerateAnswer)

#Call the ProgramOfThought module on a particular input
question = 'Sarah has 5 apples. She buys 7 more apples from the store. How many apples does Sarah have now?'
result = pot(question=question)

print(f"Question: {question}")
print(f"Final Predicted Answer (after ProgramOfThought process): {result.answer}")