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

Stream Problem #1

Open
Littleor opened this issue Aug 12, 2023 · 0 comments
Open

Stream Problem #1

Littleor opened this issue Aug 12, 2023 · 0 comments

Comments

@Littleor
Copy link

Hello, thank you for providing such a great chat-message library. I have understood how to use it correctly according to your example and can also use stream.

But when I use it with LangChain's CallBack, I directly use st.experimental_rerun() will cause the entire script to run again, making the langchain callBack function unable to continue executing, making it permanently stuck in the first place. Here is my code:

class StreamHandler(BaseCallbackHandler):
    def __init__(self, stream_key: str, initial_text=""):
        # self.text = initial_text
        self.key = stream_key

    def on_llm_start(
        self,
        *args,
        **kwargs: Any,
    ) -> Any:
        st.session_state.current_text = ''
        st.session_state.partial = True
        message(st.session_state.current_text, key=key, partial=st.session_state.partial)


    def on_llm_new_token(self, token: str, **kwargs) -> None:
        st.session_state.current_text += token
        st.experimental_rerun()

    def on_llm_end(
        self,
        response: LLMResult,
        **kwargs
    ):
        st.session_state.partial = False

key = f"message_{len(st.session_state.langchain_messages) + 1}"
stream_handler = StreamHandler(key)
response = agent.run(prompt, callbacks=[stream_handler])

So I'm thinking, is there a way for messages to control internal state updates through Python? Just like:

class StreamHandler(BaseCallbackHandler):
    def __init__(self, container, initial_text=""):
        self.container = container
        self.text = initial_text

    def on_llm_new_token(self, token: str, **kwargs) -> None:
        self.text += token
        self.container.markdown(self.text)

with st.chat_message("assistant"):
            stream_handler = StreamHandler(st.empty())
            response = st.session_state.agent.run(prompt, callbacks=[stream_handler])
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