赞
踩
本文介绍如何在llamaindex框架下,使用本地嵌入模型和llama3大模型来对文档摘要进行总结和查询。我这里让大模型读取的文档是一个英文文档,其格式是.txt的。当然,也可以是其他文档格式,比如:pdf,doc等。本文是一个完全私有化部署的例子。
摘要查询要求 LLM 遍历许多(如果不是大多数)文档,以便合成答案。例如,摘要查询可以是下面的一种:
一般来说,摘要索引适合这种用例。默认情况下,摘要索引会浏览所有数据。
根据经验,设置 response_mode="tree_summarize "也会带来更好的摘要结果。
index = SummaryIndex.from_documents(documents)
query_engine = index.as_query_engine(response_mode="tree_summarize")
response = query_engine.query("<summarization_query>")
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.llms.ollama import Ollama # 加载数据 documents = SimpleDirectoryReader("data").load_data() # 设置本地嵌入模型 local_model = "/opt/models/BAAI/bge-base-en-v1.5" # bge-base embedding model Settings.embed_model = HuggingFaceEmbedding(model_name=local_model) # 设置本地大模型:llama3 Settings.llm = Ollama(model="llama3", request_timeout=360.0) index = VectorStoreIndex.from_documents( documents, show_progress=True ) # 设置查询模式 query_engine = index.as_query_engine(response_mode="tree_summarize") # 查询问题设置 response = query_engine.query("What did the author do growing up?") # 打印结果 print(response)
本文完全通过本地的大模型实现了和某个目录下的文本对话的功能。这里可以根据自己的需要来改变需要加载的数据目录,可以使用本地不同的大模型。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。