paper-qa icon indicating copy to clipboard operation
paper-qa copied to clipboard

TypeError: expected string or bytes-like object - `query(get_callbacks)`

Open drrezaem opened this issue 2 years ago • 2 comments

Thank you so much @whitead for writing this package -- so helpful!!

I have written a callback handler to stream the query response to streamlit. Here is the handler.

 class StreamDisplayHandler(BaseCallbackHandler):
    def __init__(self, container, initial_text="", display_method='markdown'):
        self.container = container
        self.text = initial_text
        self.display_method = display_method
        self.new_sentence = ""

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

        display_function = getattr(self.container, self.display_method, None)
        if display_function is not None:
            display_function(self.text)
        else:
            raise ValueError(f"Invalid display_method: {self.display_method}")

    def on_llm_end(self, response, **kwargs) -> None:
        self.text=""

Here's how I am using it:

uploaded_files = st.file_uploader("choose one or more files", accept_multiple_files=True)
chat_box = st.empty()
stream_display_handler_instance = StreamDisplayHandler(chat_box, display_method='write')

def stream_display_handler(x):
    return stream_display_handler_instance

if len(uploaded_file) >0:
    docs = read_uploaded_file(uploaded_files)
    question = st.text_input(label="what would you like to know?")
    if question:
        answer = docs.query(question, get_callbacks = stream_display_handler)
        st.markdown(answer.formatted_answer)

I get this error now.

File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "C:\Users\abcdefg\paper.py", line 100, in <module>
    answer = docs.query(question, get_callbacks = stream_display_handler)
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\docs.py", line 452, in query
    return loop.run_until_complete(
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\docs.py", line 489, in aquery
    answer = await self.aget_evidence(
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\docs.py", line 412, in aget_evidence
    results = await gather_with_concurrency(
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\utils.py", line 85, in gather_with_concurrency
    return await asyncio.gather(*(sem_coro(c) for c in coros))
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\utils.py", line 83, in sem_coro
    return await coro
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\docs.py", line 396, in process
    if guess_is_4xx(e):
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\site-packages\paperqa\utils.py", line 89, in guess_is_4xx
    if re.search(r"4\d\d", msg):
File "C:\Users\abcdefg\Anaconda3\envs\paper-qa\lib\re.py", line 200, in search
    return _compile(pattern, flags).search(string)

The error doesn't seem to be related to the callback handler, but does not happen if get_callbacks is not used. Would appreciate any insights on how to fix this.

Thank you very much.

drrezaem avatar Jun 14 '23 06:06 drrezaem

Glad it's useful!

This should be fixed in v3, but if you do not want to update to that version you can just change line docs.py:396 to be guess_is_4xx(str(e)) or removing that statement. You're getting an exception in your callback that doesn't have an error message

whitead avatar Jun 14 '23 11:06 whitead

Amazing! Thank you.

drrezaem avatar Jun 16 '23 03:06 drrezaem