模型程式碼理解本地知識庫

林**發表於2024-04-08
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)

相關文章