LangChain 是一个开源 Python 框架,利用 LangChain,开发人员能够非常方便的开发基于大型语言模型的应用程序。
百度千帆大模型平台是文心大模型企业级服务唯一入口,一站式企业级大模型平台,提供先进的生成式AI生产及应用全流程开发工具链。
Milvus 是一个高性能的开源向量数据库,专为处理和分析大规模向量数据而设计。
LangChain+千帆大模型入门
pip3 install langchain -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install qianfan -i https://pypi.tuna.tsinghua.edu.cn/simple
获取千帆大模型平台应用API Key、Secret Key
登录 千帆大模型平台
应用接入-创建应用
获取API Key、Secret Key
通过LangChain调用千帆大模型实现简单对话应用。
import os
from langchain_community.llms import QianfanLLMEndpoint
os.environ["QIANFAN_AK"] = "API_KEY"
os.environ["QIANFAN_SK"] = "SECRET_KEY"
llm = QianfanLLMEndpoint(streaming=True)
res = llm("hi")
print(res)
Milvus安装及使用
pip3 install milvus -i https:
pip3 install pymilvus -i https:
将milvus作为python模块启动及使用:
from milvus import default_server
from pymilvus import connections, utility
default_server.start()
connections.connect(host='127.0.0.1', port=default_server.listen_port)
print(utility.get_server_version())
default_server.stop()
将milvus作为独立服务启动:
$ milvus-server
利用百度千帆大模型平台的向量模型来实现文本转向量存入milvus中并查询。
import os
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import QianfanEmbeddingsEndpoint
from langchain_community.vectorstores import Milvus
from milvus import default_server
from langchain.text_splitter import RecursiveCharacterTextSplitter
os.environ["QIANFAN_AK"] = "API_KEY"
os.environ["QIANFAN_SK"] = "SECRET_KEY"
WEB_URL = "https://zhuanlan.zhihu.com/p/89354916"
loader = WebBaseLoader(WEB_URL)
docs = loader.load()
embeddings = QianfanEmbeddingsEndpoint()
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 384, chunk_overlap = 0, separators=["\n\n", "\n", " ", "", "。", ","])
documents = text_splitter.split_documents(docs)
vector_db = Milvus.from_documents(
documents,
QianfanEmbeddingsEndpoint(),
connection_args ={"host": "127.0.0.1", "port": default_server.listen_port},
query = "周武王建周是哪年"
res = vector_db.similarity_search(query)
print(res)
RAG应用
RAG基本架构
RAG入门案例
利用LangChain框架中load_qa_with_sources_chain来实现简单RAG应用。
它用于构建一个问答链,这个链能够处理包含多个文档的问答任务。该功能的核心在于它能够结合文档内容来回答特定问题,并且能够提供回答问题的文档来源,增加了答案的可追溯性和可信度。
import os
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import QianfanEmbeddingsEndpoint
from langchain_community.vectorstores import Milvus
from milvus import default_server
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.llms import QianfanLLMEndpoint
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
os.environ["QIANFAN_AK"] = "API_KEY"
os.environ["QIANFAN_SK"] = "SECRET_KEY"
WEB_URL = "https://zhuanlan.zhihu.com/p/89354916"
loader = WebBaseLoader(WEB_URL)
docs = loader.load()
embeddings = QianfanEmbeddingsEndpoint()
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 384, chunk_overlap = 0, separators=["\n\n", "\n", " ", "", "。", ","])
documents = text_splitter.split_documents(docs)
vector_db = Milvus.from_documents(
documents,
QianfanEmbeddingsEndpoint(),
connection_args ={"host": "127.0.0.1", "port": default_server.listen_port},
collection_name="test_history",
query = "周武王建周是哪年"
vec_res = vector_db.similarity_search(query)
llm = QianfanLLMEndpoint(
streaming=True,
model="ERNIE-Bot-turbo",
endpoint="eb-instant",
chain = load_qa_with_sources_chain(llm=llm, chain_type="refine", return_intermediate_steps=True)
res = chain.invoke({"input_documents": vec_res, "question": query}, return_only_outputs=True)
print(res)
LangChain
百度千帆大模型平台
Milvus
公众号「全栈指北针」
粉丝