Skip to content
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

print statement in LM.inspect_history() method causing empty lines on console #1120

Closed
tumma72 opened this issue Jun 7, 2024 · 4 comments
Closed

Comments

@tumma72
Copy link

tumma72 commented Jun 7, 2024

There is a print statement in the dsp.modules.lm.LM.inspect_history() method which causes even an empty string to be printed in console. When running long workflows it has the effect of messing up the console output, I am not sure if it was meant to be a debug print, but it is definitely not needed.

I propose to remove it :-)

   def inspect_history(self, n: int = 1, skip: int = 0):
        """Prints the last n prompts and their completions.

        TODO: print the valid choice that contains filled output field instead of the first.
        """
        provider: str = self.provider

        last_prompt = None
        printed = []
        n = n + skip

        for x in reversed(self.history[-100:]):
            prompt = x["prompt"]

            if prompt != last_prompt:
                if provider == "clarifai" or provider == "google" or provider == "groq" or provider == "Bedrock" or provider == "Sagemaker":
                    printed.append((prompt, x["response"]))
                elif provider == "anthropic":
                    blocks = [{"text": block.text} for block in x["response"].content if block.type == "text"]
                    printed.append((prompt, blocks))
                elif provider == "cohere":
                    printed.append((prompt, x["response"].text))
                elif provider == "mistral":
                    printed.append((prompt, x['response'].choices))
                else:
                    printed.append((prompt, x["response"]["choices"]))

            last_prompt = prompt

            if len(printed) >= n:
                break

        printing_value = ""
        for idx, (prompt, choices) in enumerate(reversed(printed)):
            # skip the first `skip` prompts
            if (n - idx - 1) < skip:
                continue
            printing_value += "\n\n\n"
            printing_value += prompt

            text = ""
            if provider == "cohere" or provider == "Bedrock" or provider == "Sagemaker":
                text = choices
            elif provider == "openai" or provider == "ollama":
                text = ' ' + self._get_choice_text(choices[0]).strip()
            elif provider == "clarifai" or provider == "claude" :
                text=choices
            elif provider == "groq":
                text = ' ' + choices
            elif provider == "google":
                text = choices[0].parts[0].text
            elif provider == "mistral":
                text = choices[0].message.content
            else:
                text = choices[0]["text"]
            printing_value += self.print_green(text, end="")

            if len(choices) > 1:
                printing_value += self.print_red(f" \t (and {len(choices)-1} other completions)", end="")

            printing_value += "\n\n\n"

        # THIS ONE HERE ⇒ print(printing_value)
        return printing_value

Thanks for reading...

@arnavsinghvi11
Copy link
Collaborator

Hi @tumma72 , the print value was added from #622 to ensure the model history was returnable. The empty string you see is from the initialized printing_value = "".

If you'd like to avoid the outputted value and only see the history via the in-built prints (print_green (completion) and print_red), you can just set output = lm.inspect_history(n=1).

@tumma72
Copy link
Author

tumma72 commented Jun 20, 2024

Hi @tumma72 , the print value was added from #622 to ensure the model history was returnable. The empty string you see is from the initialized printing_value = "".

If you'd like to avoid the outputted value and only see the history via the in-built prints (print_green (completion) and print_red), you can just set output = lm.inspect_history(n=1).

Hi and thanks for the reply, I am not sure I get it though:

  1. The print statement has nothing to do with the method returning a value or not, so to me it sounds like a debug print statement, that should be removed because it is only causing a new line print in the console (the print("") adds a "").
  2. Adding n=1 as a parameter would solve nothing as it is the default value anyway.

The best way to solve the problem is to put an if statement in front of the print, or remove it all together. You could add a parameter such as debug: bool = False then set it to True if you want the printout in console. That would ensure that the print out is only present if someone needs it.

@okhat
Copy link
Collaborator

okhat commented Jun 22, 2024

Hmm. The goal of this method is to print, so removing the print would be very strange.

Folks added a return value. I don't like that addition but I'm OK with it.

If you just want to access the history manually, just access lm.history[-1] for example.

LMK if that resolves things.

@okhat okhat closed this as completed Jun 22, 2024
@tumma72
Copy link
Author

tumma72 commented Jul 4, 2024

Hmm. The goal of this method is to print, so removing the print would be very strange.

Folks added a return value. I don't like that addition but I'm OK with it.

If you just want to access the history manually, just access lm.history[-1] for example.

LMK if that resolves things.

Ok thanks for the tip, I would at least consider changing the name of the method because from inspect_history() I wouldn't expect a print, so if the goal is to print than call it print_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

No branches or pull requests

3 participants