Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.87k stars 904 forks source link

How can the final answer be prevented from changing the name of the source_name? #689

Open Irfan123fw opened 8 months ago

Irfan123fw commented 8 months ago

I want the naming in source_name to remain unchanged in the final answer. For example, if the content of source_name is '1-Book,' in the final answer, it changes to 'found in book no 1,' and the text element cannot be clicked. How can this be addressed?

retriever = await assistant.retriever_configurator.configure_retriever()
chain = await assistant.chain_configurator.configure_chain(retriever= retriever, search_type="similarity", conversation = False)
memory = cl.user_session.get("memory") 
answer_prefix_tokens=["FINAL", "ANSWER"]  

cb = cl.AsyncLangchainCallbackHandler(
    stream_final_answer=True,
    answer_prefix_tokens=answer_prefix_tokens,
)
cb.answer_reached = True

history = ChainConfigurator.Get_Chat_History(conversation_history)
print(history)
refined_query = ChainConfigurator.RefinedQuery("gpt-3.5-turbo", history, message.content)
print("Refined Query :", refined_query)
res = await chain.acall(refined_query, callbacks=[cb])
answer = res["answer"]
source_documents = res["source_documents"]
text_elements = [] 

if source_documents:
    for source_doc in source_documents:
        source_name = source_doc.metadata.get("source", "Unknown Source").replace(".", "")
        text_elements.append(
            cl.Text(content=source_doc.page_content, name=source_name)
        )
    source_names = [text_el.name for text_el in text_elements]

    if source_names:
        answer += f"\n\nSumber: {', '.join(source_names)}"
    else:
        answer += "\nNo sources found"
if cb.has_streamed_final_answer:
    cb.final_stream.elements = text_elements
    await cb.final_stream.update()
else:
    await cl.Message(content=answer, elements=text_elements).send()
willydouhard commented 8 months ago

In that case there is probably the original source name in the source_doc variable. You could just use that?

Irfan123fw commented 8 months ago

@willydouhard Yes, there is. I have used it in the variable source_name, but when the final answer is streamed, the original source name changes.