基于 langchaine Gemini rag 的聊天机器人通过组合来自不同文档的信息产生幻觉

问题描述 投票:0回答:1

所以我使用 langchain

gemini LLM
为我的大学网站创建一个
 聊天机器人。
问题是当被问到一些问题时
答案是从 RAG 检索到的文档的组合,并且错误,这里是代码:

from langchain.docstore.document import Document docs = [] for x, instance in enumerate(formations): docs.append( Document(page_content=instance['content'], metadata=instance['metadata']) ) embeddings_model = GoogleGenerativeAIEmbeddings(model='models/embedding-001') llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro-latest', temperature=0.3) #%% db_2 = Chroma.from_documents(docs,embeddings_model, collection_metadata={"hnsw:space": "cosine",'k':4}) metadata_field_info = [ AttributeInfo( name="diplome", description="The name of the diploma. One of [DEUST, Licence en Sciences et Techniques, Master en Sciences et Techniques, Ingénieur d'État]", type="string", ),....] retriever = SelfQueryRetriever.from_llm( llm, db_2, document_content_description, metadata_field_info, enable_limit=True )
正如您所见,我使用自查询,因为数据采用 json 格式,并且具有有助于相似性搜索的元数据部分。
这是提示模板:

template = """you are now an ai assistant working at 'university name'. answer using only the context. if the given context can answer the input in anyway possible then you can answer it even if its not 100% what is required. if the topic of the input is similar to the context then answer based on that even if its not 100% what was asked. if more informations are needed you can guide the user to visit the website of the faculty which is: https://fstt.ac.ma/Portail2023/ your output should follow this structure: introduction the desired answer instruct the user to visit the website for informations ask if there is anything else you can help with. context : {context}. input : {input}. output: """ prompt = PromptTemplate.from_template(template) combine_chain_2 = create_stuff_documents_chain(llm, prompt) retrieval_chain_2 = create_retrieval_chain(retriever, combine_chain_2)
现在使用这个时得到问题的答案:

我想继续我的人工智能研究,请给我最好的大师名称以继续。(问题是法语)这里是回复:

掌握人工智能和大数据:Votre Passerelle vers l'IA

您将在热情的酒庄中尽情享受 科学与技术学院 丹吉尔技术公司提议对 vos 进行母版适配 愿望:

掌握人工智能和大数据

Ce 卓越计划...等 这个问题

大学没有一位名为“智能人工智能和大数据”的大师,当查看用于生成答案的上下文时: [文档(page_content =“大师的描述和目标...”,元数据= {'diplome':'科学与技术大师','email_cooperativeur':'...','模块':“[.. .]", 'nom_cooperativeur': '...', 'nom_filliere': 'MST: Intelligence Artificielle et Sciences de Données'}), Document(page_content='master 的描述和 objectifs ... ', 元数据={'diplome': '科学与技术硕士', 'email_cooperativeur': '...', '模块': '[....]', 'nom_cooperativeur': '...', 'nom_filliere' : 'MST:移动和大数据'})

正如你所看到的,答案是这两位大师名字的组合,它从第一个大师中取出了

人工智能,从第二个大师中取出了大数据。 我如何更改此行为(可能是我不知道的提示或参数)? 谢谢您的宝贵时间!

python chatbot langchain large-language-model retrieval-augmented-generation
1个回答
0
投票
看看双子座建议的解决方案:-

retriever = SelfQueryRetriever.from_llm(llm,db_2,...,enable_limit = True,top_k = 1)

这会将 top_k 替换为 1,确保仅检索单个最相关的文档来生成答案。请记住测试此方法并可能调整其他参数以获得最佳性能。

© www.soinside.com 2019 - 2024. All rights reserved.