商城网站方案模板,做网站为什么要做备案接入,0基础学做网站,珠海建设工程网站【RAG框架】GoMate#xff1a;RAG Framework within Reliable input,Trusted output 【项目链接】#xff1a;https://github.com/gomate-community/GoMate 一、赛题背景
RAG#xff08;检索增强生成#xff09;是一种结合了检索模型和生成模型的技术#xff0c;它通过检… 【RAG框架】GoMateRAG Framework within Reliable input,Trusted output 【项目链接】https://github.com/gomate-community/GoMate 一、赛题背景
RAG检索增强生成是一种结合了检索模型和生成模型的技术它通过检索大量外部知识来辅助文本生成从而提高大型语言模型LLMs的准确度和可靠性。
RAG特别适合于需要不断更新知识的知识密集型场景或特定领域应用它通过引入外部信息源有效缓解了大语言模型在领域知识缺乏、信息准确性问题以及生成虚假内容等方面的挑战。本次挑战赛旨在探索RAG技术的极限鼓励开发者、研究人员和爱好者利用RAG技术解决实际问题推动人工智能领域的进步。
二、赛题任务
赛题需要参赛选手设计并实现一个RAG模型该模型能够从给定的问题出发检索知识库中的相关信息。利用检索到的信息结合问题本身生成准确、全面、权威的回答。
三、评审规则
1.数据说明
数据集还可能包括一些未标注的文本需要参赛者使用RAG技术中的检索增强方法来找到相关信息并生成答案。这要求参赛者不仅要有强大的检索能力还要能够生成准确、连贯且符合上下文的文本。
测试集为模拟生成的用户提问需要参赛选手结合提问和语料完成回答。需注意在问题中存在部分问题无法回答需要选手设计合适的策略进行拒绝回答的逻辑。
• corpus.txt.zip语料库每行为一篇新闻
• test_question.csv测试提问
评审规则
对于测试提问的回答采用字符重合比例进行评价分数最高为1。
四、数据分析 检索语料 文本长度
五、RAG基线实现
import pickleimport pandas as pd
from tqdm import tqdmfrom gomate.modules.document.chunk import TextChunker
from gomate.modules.document.txt_parser import TextParser
from gomate.modules.document.utils import PROJECT_BASE
from gomate.modules.generator.llm import GLM4Chat
from gomate.modules.reranker.bge_reranker import BgeRerankerConfig, BgeReranker
from gomate.modules.retrieval.bm25s_retriever import BM25RetrieverConfig
from gomate.modules.retrieval.dense_retriever import DenseRetrieverConfig
from gomate.modules.retrieval.hybrid_retriever import HybridRetriever, HybridRetrieverConfigdef generate_chunks():tp TextParser()tc TextChunker()paragraphs tp.parse(rH:/2024-Xfyun-RAG/data/corpus.txt, encodingutf-8)print(len(paragraphs))chunks []for content in tqdm(paragraphs):chunk tc.chunk_sentences([content], chunk_size1024)chunks.append(chunk)with open(f{PROJECT_BASE}/output/chunks.pkl, wb) as f:pickle.dump(chunks, f)if __name__ __main__:# test_pathH:/2024-Xfyun-RAG/data/test_question.csv# embedding_model_pathH:/pretrained_models/mteb/bge-m3# llm_model_pathH:/pretrained_models/llm/Qwen2-1.5B-Instructtest_path /data/users/searchgpt/yq/GoMate_dev/data/competitions/xunfei/test_question.csvembedding_model_path /data/users/searchgpt/pretrained_models/bge-large-zh-v1.5llm_model_path /data/users/searchgpt/pretrained_models/glm-4-9b-chat# 文件解析切片generate_chunks()with open(f{PROJECT_BASE}/output/chunks.pkl, rb) as f:chunks pickle.load(f)corpus []for chunk in chunks:corpus.extend(chunk)# 检索器配置# BM25 and Dense Retriever configurationsbm25_config BM25RetrieverConfig(methodlucene,index_pathindexs/description_bm25.index,k11.6,b0.7)bm25_config.validate()print(bm25_config.log_config())dense_config DenseRetrieverConfig(model_name_or_pathembedding_model_path,dim1024,index_pathindexs/dense_cache)config_info dense_config.log_config()print(config_info)# Hybrid Retriever configuration# 由于分数框架不在同一维度建议可以合并hybrid_config HybridRetrieverConfig(bm25_configbm25_config,dense_configdense_config,bm25_weight0.7, # bm25检索结果权重dense_weight0.3 # dense检索结果权重)hybrid_retriever HybridRetriever(confighybrid_config)# 构建索引# hybrid_retriever.build_from_texts(corpus)# 保存索引# hybrid_retriever.save_index()# 加载索引hybrid_retriever.load_index()# 检索测试query 新冠肺炎疫情results hybrid_retriever.retrieve(query, top_k5)# Output resultsfor result in results:print(fText: {result[text]}, Score: {result[score]})# 排序配置reranker_config BgeRerankerConfig(model_name_or_path/data/users/searchgpt/pretrained_models/bge-reranker-large)bge_reranker BgeReranker(reranker_config)# 生成器配置# qwen_chat QwenChat(llm_model_path)glm4_chat GLM4Chat(llm_model_path)# 检索问答test pd.read_csv(test_path)answers []for question in tqdm(test[question], totallen(test)):search_docs hybrid_retriever.retrieve(question)search_docs bge_reranker.rerank(queryquestion,documents[doc[text] for idx, doc in enumerate(search_docs)])# print(search_docs)content /n.join([f信息[{idx}] doc[text] for idx, doc in enumerate(search_docs)])answer glm4_chat.chat(promptquestion, contentcontent)answers.append(answer[0])print(question)print(answer[0])print(************************************/n)test[answer] answerstest[[answer]].to_csv(f{PROJECT_BASE}/output/gomate_baseline.csv, indexFalse)