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

implement on_llm_new_token to support streaming of responses #3497

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions langchain/callbacks/streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class StreamlitCallbackHandler(BaseCallbackHandler):
"""Callback Handler that logs to streamlit."""
def __init__(self) -> None:
self.tokens_area = st.empty()
self.tokens_stream = ""

def on_llm_start(
self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any
Expand All @@ -19,8 +22,9 @@ def on_llm_start(
st.write(prompt)

def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Do nothing."""
pass
"""Run on new LLM token. Only available when streaming is enabled."""
self.tokens_stream += token
self.tokens_area.write(self.tokens_stream)

def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Do nothing."""
Expand Down
Loading