本項(xiàng)目使用Langchain
和 baichuan
大模型, 結(jié)合領(lǐng)域百科詞條數(shù)據(jù)(用xlsx保存),簡(jiǎn)單地實(shí)現(xiàn)了領(lǐng)域百科問(wèn)答實(shí)現(xiàn)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-832318.html
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
from langchain_community.embeddings import OpenAIEmbeddings, SentenceTransformerEmbeddings
from langchain_community.vectorstores import Chroma, FAISS
from langchain_community.llms import OpenAI, Baichuan
from langchain_community.chat_models import ChatOpenAI, ChatBaichuan
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
#import langchain_community import chat_models
#print(chat_models.__all__)
import streamlit as st
import pandas as pd
import os
import warnings
import time
warnings.filterwarnings('ignore')
# 對(duì)存儲(chǔ)了領(lǐng)域百科詞條的xlsx文件進(jìn)行解析
def get_xlsx_text(xlsx_file):
df = pd.read_excel(xlsx_file, engine='openpyxl')
text = ""
for index, row in df.iterrows():
text += row['title'].replace('\n', '')
text += row['content'].replace('\n', '')
text += '\n\n'
return text
# Splits a given text into smaller chunks based on specified conditions
def get_text_chunks(text):
text_splitter = RecursiveCharacterTextSplitter(
separators="\n\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
return chunks
# 對(duì)切分的文本塊構(gòu)建編碼向量并存儲(chǔ)到FASISS
# Generates embeddings for given text chunks and creates a vector store using FAISS
def get_vectorstore(text_chunks):
# embeddings = OpenAIEmbeddings() #有經(jīng)濟(jì)條件的可以使用 opanaiembending
embeddings = SentenceTransformerEmbeddings(model_name='all-MiniLM-L6-v2')
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
return vectorstore
# Initializes a conversation chain with a given vector store
# 對(duì)切分的文本塊構(gòu)建編碼向量并存儲(chǔ)到Chroma
# Generates embeddings for given text chunks and creates a vector store using Chroma
def get_vectorstore_chroma(text_chunks):
# embeddings = OpenAIEmbeddings()
embeddings = SentenceTransformerEmbeddings(model_name='all-MiniLM-L6-v2')
vectorstore = Chroma.from_texts(
texts=text_chunks, embedding=embeddings)
return vectorstore
def get_conversation_chain_baichuan(vectorstore):
memory = ConversationBufferWindowMemory(
memory_key='chat_history', return_message=True) # 設(shè)置記憶存儲(chǔ)器
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=Baichuan(temperature=temperature_input, model_name=model_select),
retriever=vectorstore.as_retriever(),
get_chat_history=lambda h: h,
memory=memory
)
return conversation_chain
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
# langchain 可以通過(guò)設(shè)置環(huán)境變量來(lái)設(shè)置參數(shù)
os.environ['BAICHUAN_API_KEY'] = 'sk-88888888888888888888888888888888'
temperature_input = 0.7
model_select = 'Baichuan2-Turbo-192K'
raw_text = get_xlsx_text('領(lǐng)域文件/twiki百科問(wèn)答.xlsx')
text_chunks = get_text_chunks(raw_text)
vectorstore = get_vectorstore_chroma(text_chunks)
# Create conversation chain
qa = get_conversation_chain_baichuan(vectorstore)
questions = [
"什么是森林經(jīng)營(yíng)項(xiàng)目?",
"風(fēng)電項(xiàng)目開(kāi)發(fā)過(guò)程中需要的主要資料?",
"什么是ESG"
]
for question in questions:
result = qa(question)
print(f"**Question**: {question} \n")
print(f"**Answer__**: {result['answer']} \n")
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-832318.html
到了這里,關(guān)于【Langchain】+ 【baichuan】實(shí)現(xiàn)領(lǐng)域知識(shí)庫(kù)【RAG】問(wèn)答系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!