chatGPT 本地知识库问答原理实践

chatGPT 本地知识库问答原理实践_第1张图片

参考

  • https://huggingface.co/spaces/ChallengeHub/Chinese-LangChain
  • https://huggingface.co/spaces/thomas-yanxin/LangChain-ChatLLM
  • https://huggingface.co/shibing624/text2vec-base-chinese
  • https://python.langchain.com/docs/integrations/vectorstores/faiss

原理

chatGPT 本地知识库问答原理实践_第2张图片

  • 本地知识(txt md word pdf 等) → 本地向量持久(redis mulvs elasticsearch local pg )
  • 读取知识 → 相似度搜索 → 进行总结推理

代码测试

import os
 
import gradio as gr
import nltk
import sentence_transformers
import torch
from langchain.chains import RetrievalQA
from langchain.document_loaders import UnstructuredFileLoader
from langchain.embeddings import JinaEmbeddings
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
from langchain.prompts.prompt import PromptTemplate
from langchain.vectorstores import FAISS
from langchain import OpenAI
import re
from typing import List
import openai
from langchain.text_splitter import CharacterTextSplitter
 
 
class LocalKnowledge:
    def __init__(self, local_file_path="./README.md", embedding_model_name_or_path="GanymedeNil/text2vec-base-chinese"):
        self.local_file_path = local_file_path
        self.embedding_model_name_or_path = embedding_model_name_or_path
 
    @property
    def embeddings(self):
        return HuggingFaceEmbeddings(model_name=self.embedding_model_name_or_path)
 
    @property
    def docs(self):
        from langchain.document_loaders import TextLoader
 
        loader = TextLoader("./README.md", encoding="UTF-8")
        documents = loader.load()
        text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)
        docs = text_splitter.split_documents(documents)
        return docs
 
    def init_knowledge_vector_store(self):
        return FAISS.from_documents(self.docs, self.embeddings)
 
    @classmethod
    def get_knowledge_based_answer(cls, query,
                                   vector_store,
                                   llm,
                                   VECTOR_SEARCH_TOP_K,
                                   web_content,
                                   history_len,
                                   temperature,
                                   top_p,
                                   chat_history=[]):
        if web_content:
            prompt_template = f"""基于以下已知信息,简洁和专业的来回答用户的问题。
                                如果无法从中得到答案,请说 "根据已知信息无法回答该问题" 或 "没有提供足够的相关信息",不允许在答案中添加编造成分,答案请使用中文。
                                已知网络检索内容:{web_content}""" + """
                                已知内容:
                                {context}
                                问题:
                                {question}"""
        else:
            prompt_template = """基于以下已知信息,请简洁并专业地回答用户的问题。
                如果无法从中得到答案,请说 "根据已知信息无法回答该问题" 或 "没有提供足够的相关信息"。不允许在答案中添加编造成分。另外,答案请使用中文。
                已知内容:
                {context}
                问题:
                {question}"""
        prompt = PromptTemplate(template=prompt_template,
                                input_variables=["context", "question"])
        knowledge_chain = RetrievalQA.from_llm(
            llm=llm,
            retriever=vector_store.as_retriever(
                search_kwargs={"k": VECTOR_SEARCH_TOP_K}),
            prompt=prompt)
        knowledge_chain.combine_documents_chain.document_prompt = PromptTemplate(
            input_variables=["page_content"], template="{page_content}")
 
        knowledge_chain.return_source_documents = True
 
        result = knowledge_chain({"query": query})
        return result
 
    def predict(self, input,
                llm,
                VECTOR_SEARCH_TOP_K=6,
                history_len=0,
                temperature=0.01,
                top_p=0.9,
                history=None):
        if history is None:
            history = []
        vector_store = self.init_knowledge_vector_store()
        web_content = ''
        resp = self.get_knowledge_based_answer(
            query=input,
            llm=llm,
            vector_store=vector_store,
            VECTOR_SEARCH_TOP_K=VECTOR_SEARCH_TOP_K,
            web_content=web_content,
            chat_history=history,
            history_len=history_len,
            temperature=temperature,
            top_p=top_p,
        )
        history.append((input, resp['result']))
        return history
 
 
if __name__ == '__main__':
    os.environ["OPENAI_API_KEY"] = "sk-xxx"
    os.environ["OPENAI_API_BASE"] = "http://10.9.115.77:50000/v1"  # 线上GCU chatGLM
    llm = OpenAI()
    DEVICE = "cpu"
    l = LocalKnowledge(embedding_model_name_or_path="./abcde")
    result = l.predict(input="Decoder-only 是什么", llm=llm, VECTOR_SEARCH_TOP_K=3)
    print(result)

效果

  • 实验使用的知识库来源:https://blog.csdn.net/xzpdxz/article/details/131358920

    chatGPT 本地知识库问答原理实践_第3张图片

你可能感兴趣的:(Ai,chatgpt,人工智能,AI写作)