【Langchain】+ 【baichuan】实现领域知识库【RAG】问答系统

本项目使用Langchainbaichuan 大模型, 结合领域百科词条数据(用xlsx保存),简单地实现了领域百科问答实现。

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')




# 对存储了领域百科词条的xlsx文件进行解析
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


# 对切分的文本块构建编码向量并存储到FASISS
# Generates embeddings for given text chunks and creates a vector store using FAISS
def get_vectorstore(text_chunks):
    # embeddings = OpenAIEmbeddings() #有经济条件的可以使用 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

# 对切分的文本块构建编码向量并存储到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) # 设置记忆存储器
    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 可以通过设置环境变量来设置参数
os.environ['BAICHUAN_API_KEY'] = 'sk-88888888888888888888888888888888'
temperature_input = 0.7
model_select = 'Baichuan2-Turbo-192K'
raw_text = get_xlsx_text('领域文件/twiki百科问答.xlsx')


text_chunks = get_text_chunks(raw_text)
vectorstore = get_vectorstore_chroma(text_chunks)
# Create conversation chain
qa = get_conversation_chain_baichuan(vectorstore)
questions = [
    "什么是森林经营项目?",
    "风电项目开发过程中需要的主要资料?",
    "什么是ESG"
]
for question in questions:
    result = qa(question)
    print(f"**Question**: {question} \n")
    print(f"**Answer__**: {result['answer']} \n")
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要将项目部署到 baichuan7b 上,您可以按照以下步骤进行操作: 1. 首先,确保您的项目已经完成并且可以在本地正常运行。 2. 登录到 baichuan7b 的服务器。您可以使用 SSH 连接工具,比如 PuTTY(Windows)或者 Terminal(MacOS/Linux)来登录。 3. 将您的项目文件上传到服务器。您可以使用 SCP 命令将本地文件拷贝到服务器上,例如: ``` scp -r /path/to/your/project username@baichuan7b:/path/on/server ``` 其中,`/path/to/your/project` 是您本地项目的路径,`username` 是您在 baichuan7b 上的用户名,`/path/on/server` 是您希望将项目拷贝到的服务器路径。 4. 在 baichuan7b 服务器上安装所需的依赖包。根据您的项目需要,在服务器上安装必要的软件和库,并确保它们与您在本地开发环境中使用的版本相兼容。 5. 启动您的项目。进入到您上传项目的目录,并运行适当的命令来启动您的项目。具体命令可能因项目类型而异,例如: ``` cd /path/on/server python main.py ``` 这里的 `main.py` 是您项目的入口文件。 6. 确保项目在服务器上正常运行。通过访问服务器的 IP 地址或者域名,并使用适当的端口号,来验证您的项目是否在 baichuan7b 上成功部署。 请注意,以上步骤仅为一般指导,具体操作可能因您的项目和服务器环境而异。如果您遇到任何问题,请参考 baichuan7b 的文档或者联系他们的技术支持团队以获取更详细的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值