Chainlit / cookbook

Chainlit's cookbook repo
https://github.com/Chainlit/chainlit
662 stars 245 forks source link

How fix retrieval_qa_chain issue in chainlit #110

Open QsingularityAi opened 2 months ago

QsingularityAi commented 2 months ago

def retrieval_qa_chain(model, retriever_instance):

Convert the functions to runnables using RunnableLambda

split_image_text_types_runnable = RunnableLambda(split_image_text_types)
img_prompt_func_runnable = RunnableLambda(img_prompt_func)

# Create a runnable chain
qa_chain = (
    RunnablePassthrough.assign(
        context=retriever_instance | split_image_text_types_runnable
    )
    | img_prompt_func_runnable
    | model
    | StrOutputParser()
)

return qa_chain

def qa_bot(): llm = load_model() qa = retrieval_qa_chain(llm, retriever_instance) return qa

@cl.on_chat_start async def start(): """ Initializes the bot when a new chat starts.

This asynchronous function creates a new instance of the retrieval QA bot,
sends a welcome message, and stores the bot instance in the user's session.
"""
chain = qa_bot()
welcome_message = cl.Message(content="Starting the bot...")
await welcome_message.send()
welcome_message.content = (
    "Hi, Welcome to Chat With Data."
)
await welcome_message.update()
cl.user_session.set("chain", chain)
return qa_chain

Error Issue 

TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'NoneType'> 2024-05-06 10:21:04 - Translation file for en-GB not found. Using default translation en-US. 2024-05-06 10:21:05 - Translated markdown file for en-GB not found. Defaulting to chainlit.md. 2024-05-06 10:21:05 - Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'NoneType'> Traceback (most recent call last): File "/home/anurag/conda3/envs/botgpt/lib/python3.8/site-packages/chainlit/utils.py", line 39, in wrapper return await user_function(**params_values) File "main.py", line 121, in start chain = qa_bot() File "main.py", line 110, in qa_bot qa = retrieval_qa_chain(llm, retriever_instance) File "main.py", line 99, in retrieval_qa_chain context=retriever_instance | split_image_text_types_runnable File "/home/anurag/conda3/envs/botgpt/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 434, in ror return RunnableSequence(coerce_to_runnable(other), self) File "/home/anurag/conda3/envs/botgpt/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 4734, in coerce_to_runnable raise TypeError( TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'NoneType'>