Skip to content

Per module lm history #8199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

chenmoneygithub
Copy link
Collaborator

Make it possible to do module.inspect_history, which gives the LM history of the module. In nesting case, the same LM history message will be available to all the ancestor callers.

Example code:

import dspy


class MyProgram(dspy.Module):
    def __init__(self):
        super().__init__()
        self.cot = dspy.ChainOfThought("question -> answer")

    def forward(self, question: str, **kwargs) -> str:
        return self.cot(question=question)


dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini", cache=False), adapter=dspy.JSONAdapter())
program = MyProgram()
program(question="What is the capital of France?")

program.inspect_history()

It will give:

[2025-05-08T14:21:06.581570]

System message:

Your input fields are:
1. `question` (str)
Your output fields are:
1. `reasoning` (str)
2. `answer` (str)
All interactions will be structured in the following way, with the appropriate values filled in.

Inputs will have the following structure:

[[ ## question ## ]]
{question}

Outputs will be a JSON object with the following fields.

{
  "reasoning": "{reasoning}",
  "answer": "{answer}"
}
In adhering to this structure, your objective is: 
        Given the fields `question`, produce the fields `answer`.


User message:

[[ ## question ## ]]
What is the capital of France?

Respond with a JSON object in the following order of fields: `reasoning`, then `answer`.


Response:

{"reasoning":"The capital of France is a well-known fact, commonly taught in geography. Paris is recognized globally as the capital city, serving as the political, economic, and cultural center of the country.","answer":"Paris"}

You can also access the history by program.cot.inspect_history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant