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

Output printing multiple times #3384

Closed
AshwinNS opened this issue Jun 13, 2024 · 3 comments
Closed

Output printing multiple times #3384

AshwinNS opened this issue Jun 13, 2024 · 3 comments

Comments

@AshwinNS
Copy link

Hi Team, first off all great package, wonderful work, kudos.
I have a method which is returning a stream-able object and i'm using for loop to print the output to the console. here is a sample code.

def get_streaming_answer(query):
    rag = RAG(
        streaming      = True,
        callbacks       = [StreamingStdOutCallbackHandler()],
        search_args  = {'k': 10},
        verbose         = False
    )

    console = Console()
    progress = Progress(auto_refresh=False)
    result = rag.question(query)

    progress.console.print(
    Panel(
        f"[bold blue]{query}",
        padding=1,
        )
    )

    for chunk in result:
        # console.clear(home=False)
        console.print(Markdown(chunk.get('answer')), highlight=True, soft_wrap=True)

The problem I'm facing is when the output is printed it prints unstyled version and the Markdown formatted output too. i tried using console.clean() but it was clearing the question panels too. here is an image of the terminal.

Screenshot 2024-06-13 at 4 33 45 PM

what am i missing here, even if the unstyled data is o/p to terminal, is there a way to remove them once the Markdown version is displayed like console.clean().

Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@AshwinNS
Copy link
Author

The issue was StreamingStdOutCallbackHandler() is writing the first part and rich is printing the second part. The solution was to redirect the rich output to stderr and used contextlib.redirect_stdout to redirect stdout to a dummy stream.

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

1 participant