gkamradt / langchain-tutorials

Overview and tutorial of the LangChain Library
6.63k stars 1.92k forks source link

How to use map_reduce in RetrievalQA chain #54

Closed wanghang-github closed 1 year ago

wanghang-github commented 1 year ago

I want to use RetrievalQA chain to achieve a QA about PDF,how I don't know why my response always be split although it‘s total tokens just 1800 or 2500 such as image so I want to use a map_reduce,but it give me a error image and my code is ` top_matches = vector_db.similarity_search_with_score(query=question, k=int(top_k)) top_matches_contexts = " " print("top_matches",top_matches)

print("top_k",top_k)
for i, val in enumerate(top_matches):
    top_matches_contexts += "{}、{}\n".format(i+1, val[0].page_content)
if top_matches_contexts == []:
    return "请详细描述你的问题"

top_matches_contexts = remove_spaces_and_newlines(top_matches_contexts)

global language
query = prompt.format(query=question, reference=top_matches_contexts,key_word=key_word,language=language)

# chat_history,top_matches_contexts = deal_total_token(chat_history,query)

qa_chain = RetrievalQA.from_chain_type(llm=OpenAI(temperature=0.0,openai_api_key="XXXXX"), chain_type="map_reduce",
                                       retriever=vector_db.as_retriever())

result = qa_chain({"query": query,"chat_history": chat_history})
count_tokens(qa_chain,query)

`

gkamradt commented 1 year ago

Your K might be too high which results in too many tokens when you're trying to answer the question.

Try reducing K or reducing the chunk size

wanghang-github commented 1 year ago

Yes, you are right.When I use top_k is 5 or 3 ,the errror was solve. but I hope to analyze the answer I want through more text, so I use map-reduce here. How should I complete my analysis task without changing the chunk_size and top_k conditions?

gkamradt commented 1 year ago

You can make a custom map prompt and ask for a shorter summary of the answer. Or else do map reduce again on the output from the first one