dot 未来速度的变革将在您所在的城市举行。

加入我们,参加 Redis 发布会

使用 Redis 和 NVIDIA NIM 更快地部署 GenAI 应用

使用 Redis(全球最快的实时数据和 AI 应用程序数据平台)加速 GenAI 应用程序开发。现在,借助 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 入门的便捷性,我们将逐步演示如何构建一个简单的聊天机器人,该聊天机器人使用 RAG,其中包含 Redis 向量数据库和 NIM 模型以及推理,以实现快速响应。在此示例中,我们将向它提出有关 Chevy Colorado 用户手册的问题。让我们开始吧。您可以使用 此笔记本 进行操作。

首先,连接到 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 车辆的大量定性和定量信息。将数据导入笔记本后,我们将使用 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://#: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 表达式语言 (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 用于自己的应用程序。

入门