本地知识库解锁答案

from langchain.chains import RetrievalQA
from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.llms.ollama import Ollama
from langchain_community.vectorstores.faiss import FAISS    #使用 vectorstore 进行存储
from langchain_text_splitters import RecursiveCharacterTextSplitter

llm = Ollama(model="qwen:7b")

loader = TextLoader("/home/cmcc/server/file/测试文档.txt", encoding="utf-8")

doc = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=3000, chunk_overlap=400)

docs = text_splitter.split_documents(doc)

#向量搜索引擎
embeddings = OllamaEmbeddings()

docsearch = FAISS.from_documents(docs, embeddings)
# FAISS.save_local("Ver", "index")

#创建你的检索引擎  retriever 设置检索的数据库是那个
qa = RetrievalQA.from_chain_type(llm = llm, chain_type="stuff", retriever=docsearch.as_retriever())

query = "小张的姐姐是谁"
response = qa.run(query)
print(response)

 

posted @ 2024-04-07 15:05  林**  阅读(23)  评论(0编辑  收藏  举报