模型代码理解本地知识库

import os

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

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

embedding = OllamaEmbeddings()

if not os.path.exists("var"):
    root_dir = "/home/cmcc/server/file/pyfiletest/"
    docs = []

    for dirpath, dirnames, filenames in os.walk(root_dir):
        for file in filenames:
            try:
                loader = TextLoader(os.path.join(dirpath, file), encoding="utf-8")
                docs.extend(loader.load_and_split())
            except Exception as e:
                print(e)
                pass
    docsearch = FAISS.from_documents(docs, embedding)
    docsearch.save_local("var", "index")
else:
    docsearch = FAISS.load_local("var", embedding)

qa = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=docsearch.as_retriever())

response = qa.run("如何通过历史消息聊天,只给出代码实现")
print(response)

 

posted @ 2024-04-08 10:28  林**  阅读(41)  评论(0编辑  收藏  举报