使用 Redis 加速您的 GenAI 应用开发——Redis 是全球最快的实时数据和 AI 应用数据平台。现在,结合 Redis 和 NVIDIA NIM 推理微服务,您可以更快地构建和部署 GenAI 应用。
企业正在寻找方法将他们的 GenAI 应用投入生产环境,以便应用最新的进展并为客户提供更好的体验。构建 GenAI 应用面临软件开发中常见的挑战——集成、测试和扩展,但 AI 使其更上一层楼。为了保持领先地位,企业需要一个简单可靠且能够适应技术变化的 инфраструктура。
为了帮助企业更快地将 GenAI 应用投入生产,Redis 正利用 NVIDIA NIM 提供现成的基础设施,用于快速数据访问和 AI 模型。NIM 是 NVIDIA AI Enterprise 软件平台的一部分,用于 GenAI 的开发和部署,可以与 Redis 结合使用,实现快速灵活的部署。
开发者依赖 Redis 满足他们的实时数据和 AI 需求,涵盖从客户支持代理到欺诈和异常检测,再到实时产品推荐的各种应用。借助 NIM,您可以跳过搭建和维护完整堆栈基础设施来运行最新的 GenAI 模型。NIM 通过预构建的云原生微服务简化 AI 模型部署,这些微服务经过维护,可在 NVIDIA 加速的基础设施上提供优化的推理。
您可以将 NIM 与 Redis 中的现有数据一起使用,以利用最新的 Redis 功能,例如矢量数据库和语义缓存。使用 Redis 作为您的矢量数据库,以便通过使用来自 NIM 的模型进行检索增强生成 (RAG) 来更快地访问信息。此外,使用 Redis 语义缓存来缓存 GenAI 应用的 LLM 响应。结合这两者,您可以降低成本并加快响应速度,提供用户期望的实时体验。
为了展示如何轻松开始使用 Redis 和 NVIDIA NIM,我们将演示构建一个简单的聊天机器人,该机器人使用 Redis 矢量数据库进行 RAG,并使用 NIM 提供模型和推理以实现快速响应。在本示例中,我们将向其询问有关 Chevy Colorado 用户手册的问题。让我们开始吧。您可以按照此 Notebook 进行操作。
首先,连接到 NVIDIA 托管的 LLM 和嵌入模型。您可以使用现有的 API 密钥或在此处获取一个。我们将使用 NIM 来简单快速地访问最新模型,并使用 LangChain 来创建嵌入。
from langchain_core.messages import ChatMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_nvidia_ai_endpoints import ChatNVIDIA
# Create LLM instance with the Mistral model
llm = ChatNVIDIA(
model="mistralai/mixtral-8x22b-instruct-v0.1",
temperature=0.1,
top_p=1.0,
)
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
emb = NVIDIAEmbeddings()
我们将使用 Chevy Colorado 卡车用户手册的 PDF 文件。它包含有关 Chevy 车辆的大量定性和定量信息。将数据导入 Notebook 后,我们将使用 LangChain 为 RAG 准备文档。
from langchain.text_splitter import SentenceTransformersTokenTextSplitter
from langchain.document_loaders import UnstructuredFileLoader
# Load list of pdfs from a folder
data_path = f"data/"
docs = [os.path.join(data_path, file) for file in os.listdir(data_path)]
text_splitter = SentenceTransformersTokenTextSplitter()
loader = UnstructuredFileLoader(
docs[0], mode="single", strategy="fast"
)
# extract, load, and make chunks
chunks = loader.load_and_split(text_splitter)
我们将从 Chevy Colorado 手册中提取这些数据块,并将其加载到 Redis 矢量数据库中以实现快速检索。
from langchain_community.vectorstores.redis import Redis
vectordb = Redis.from_documents(
documents=chunks,
embedding=emb,
redis_url="redis://localhost:6379"
)
retriever = vectordb.as_retriever(
search_type="similarity_distance_threshold",
search_kwargs={"distance_threshold":0.4}
)
为了设置我们的 RAG 以充分利用 NIM 模型,我们将设计一个 RAG 提示,描述数据集以及我们希望 LLM 如何响应。
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(
"""You are an intelligent assistant specializing in the Chevy Colorado
2022. You have access to the car manual and production information and should help
answer users questions based on provided context below. Be truthful and honest --
do not make anything up if it's not clearly provided in the context below.
User Question: {question}
Context:\n{context}
Answer:"""
)
让我们使用 LangChain Expression Language (LCEL) 设置我们的 RAG 链。
# Build the RAG chain using LCEL
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
from langchain_core.pydantic_v1 import BaseModel
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
chain = (
RunnableParallel({"context": retriever | format_docs, "question": RunnablePassthrough()})
| prompt
| llm
| StrOutputParser()
)
# Add typing for input
class Question(BaseModel):
__root__: str
chain = chain.with_types(input_type=Question)
一切设置好后,我们就可以提出问题了。聊天机器人会将问题以及从 Redis 矢量数据库中提供的相关信息发送给使用 NIM 的 LLM,以生成响应。
chain.invoke("What models are available for the chevy colorado?")
应用会根据我们的请求从源文档中返回适当的响应。
The 2022 Chevy Colorado is available in four models: WT, LT, Z71, and ZR2. These models can come in either an extended cab or crew cab configuration. The engines available are a 2.5L 4-cylinder, a 3.6L V6, and a Duramax 2.8L turbo-diesel engine. The Duramax engine provides up to 7,700 lbs. of towing capacity and can deliver up to 30 mpg on the highway. The ZR2 model is an off-road beast with the capability to conquer tough trails. Additionally, there is an available ZR2 Bison Edition which includes 17-inch AEV.
让我们更进一步,让应用程序在响应中添加来源。这有助于用户探索文档并自行验证信息。
rag_chain_from_docs = (
RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
| prompt
| llm
| StrOutputParser()
)
rag_chain_with_source = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
).assign(answer=rag_chain_from_docs)
我们将提出问题并获得以下响应。
rag_chain_with_source.invoke("What models are available for the chevy colorado?")
{'context': [Document(page_content='2022 colorado choose your adventure. the 2022 colorado delivers everything you could ask for in a midsize pickup. engine choices that are powerful and efficient, including an available gm - exclusive duramax(r) 2. 8l turbo - diesel engine that provides up to 7, 700 lbs. of towing1, 2 muscle. a zr2 off - road beast with the capability to conquer tough trails. and a comfortable interior filled with convenience and technology features. so go ahead. choose your best life in colorado. colorado crew cab zr2 in sand dune metallic with available zr2 dusk special edition. vehicle shown can tow up to 5, 000 lbs. 2, 3 1 requires colorado crew cab short box lt 2wd with available trailering package, lt convenience package and safety package. 2 maximum trailering ratings are intended for comparison purposes only. before you buy a vehicle or use it for trailering, carefully review the trailering section of the owner ' s manual. the trailering capacity of your specific vehicle may vary. the weight of passengers, cargo and options or accessories may reduce the amount you can trailer. 3 requires available trailering package and automatic locking rear differential on lt ; requires available trailering package on z71. due to current supply chain shortages, certain features shown have limited or late availability, or are no longer available. see the window label or a dealer regarding the features on an individual vehicle. introducing colorado colorado at a glance. four models : wt, lt, z71 and zr2 extended cab or crew cab three engines 2. 5l 4 - cylinder 3. 6l v6 duramax 2. 8l turbo - diesel 30 mpg highway 1 with available diesel engine 7, 700 lbs. maximum trailering weight2 with available diesel engine apple carplay(r)3 and android autoTM4 compatibility available zr2 bison edition includes 17 - inch aev', metadata={'id': 'doc:5313bad541f24eb58d856c7ca8f04fc4:17319c587eaa4dd69eacd57b50ff1a5e', 'source': 'data/2022-chevrolet-colorado-ebrochure.pdf'})],
'question': 'What models are available for the chevy colorado?',
'answer': ' The 2022 Chevy Colorado is available in four models: WT, LT, Z71, and ZR2. These models can come in either an extended cab or crew cab configuration. The engines available are a 2.5L 4-cylinder, a 3.6L V6, and a Duramax 2.8L turbo-diesel. The Colorado can achieve up to 30 mpg on the highway with the available diesel engine. The maximum trailering weight is 7,700 lbs with the available diesel engine. The Colorado also offers Apple CarPlay(r) and Android AutoTM compatibility. Additionally, there is an available ZR2 Bison Edition which includes 17-inch AEV.'}
希望您喜欢这个演示。现在您可以在自己的应用中使用 Redis 和 NVIDIA NIM 了。
开始使用