0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

基于Redis Enterprise,LangChain,OpenAI 構(gòu)建一個電子商務(wù)聊天機器人

虹科網(wǎng)絡(luò)可視化技術(shù) ? 2023-11-25 08:04 ? 次閱讀

鑒于最近人工智能支持的API網(wǎng)絡(luò)開發(fā)工具的激增,許多科技公司都在將聊天機器人集成到他們的應(yīng)用程序中。

LangChain是一種備受歡迎的新框架,近期引起了廣泛關(guān)注。該框架旨在簡化開發(fā)人員與語言模型、外部數(shù)據(jù)和計算資源進行交互的應(yīng)用程序開發(fā)過程。它通過清晰且模塊化的抽象,關(guān)注構(gòu)建所需的所有構(gòu)建模塊,并構(gòu)建了常用的"鏈條",即構(gòu)建模塊的組合。例如,對話檢索鏈條可以讓用戶與外部存儲中的數(shù)據(jù)進行交互,實現(xiàn)真實的對話體驗。

LangChain是如何實現(xiàn)這一目標(biāo)的呢?OpenAI的語言模型并沒有針對特定企業(yè)的具體數(shù)據(jù)進行訓(xùn)練或優(yōu)化。如果您的聊天機器人依賴于該框架,您需要在運行時向OpenAI提供數(shù)據(jù)。在檢索步驟中,我們使用向量相似性搜索(VSS)從Redis中獲取與用戶查詢相關(guān)的數(shù)據(jù),并將這些數(shù)據(jù)與原始問題一起輸入到語言模型中。這要求模型僅使用提供的信息(在人工智能領(lǐng)域中稱為"上下文")來回答問題。

這個鏈條中的大部分復(fù)雜性都歸結(jié)于檢索步驟。因此,我們選擇將LangChain與Redis Enterprise集成為一個向量數(shù)據(jù)庫。這種組合為復(fù)雜的人工智能和產(chǎn)品開發(fā)之間搭建了橋梁。


在這個簡短的教程中,我們將展示如何構(gòu)建一個會話式的零售購物助手,幫助顧客在產(chǎn)品目錄中發(fā)現(xiàn)那些被埋藏的令人感興趣的商品。讀者可以按照提供的完整代碼進行操作。

01

構(gòu)建你的聊天機器人

首先,安裝項目所需的所有組件。

1、安裝 Python 依賴項

這個項目需要一些Python庫。這些庫存儲在github倉庫的文件中。

pip install langchain==0.0.123pip install openai==0.27.2pip install redis==4.5.3pip install numpypip install pandaspip install gdown

2、準(zhǔn)備產(chǎn)品數(shù)據(jù)集

對于零售聊天機器人,我們選擇使用Amazon Berkeley Objects數(shù)據(jù)集。該數(shù)據(jù)集包含了大量適用于生成零售助手的亞馬遜產(chǎn)品。

使用Python的pandas庫來加載和預(yù)處理數(shù)據(jù)集。在加載過程中,我們可以截斷較長的文本字段。這樣一來,我們的數(shù)據(jù)集會更加精簡,從而節(jié)省內(nèi)存和計算時間。

import pandas as pd
MAX_TEXT_LENGTH=1000 # Maximum num of text characters to use def auto_truncate(val): """Truncate the given text.""" return val[:MAX_TEXT_LENGTH] # Load Product data and truncate long text fields all_prods_df = pd.read_csv("product_data.csv", converters={ 'bullet_point': auto_truncate, 'item_keywords': auto_truncate, 'item_name': auto_truncate })

3、在完全加載了我們的產(chǎn)品數(shù)據(jù)集之后,進行一些最后的預(yù)處理步驟,以清理關(guān)鍵詞字段并刪除缺失值。

# Replace empty strings with None and drop all_prods_df['item_keywords'].replace('', None, inplace=True) all_prods_df.dropna(subset=['item_keywords'], inplace=True) # Reset pandas dataframe index all_prods_df.reset_index(drop=True, inplace=True)

4、如果你持續(xù)在跟進GitHub上的代碼步驟,可以使用all_prods_df.head()來查看數(shù)據(jù)框的前幾行。完整的數(shù)據(jù)集包含超過100,000個產(chǎn)品,但是對于這個聊天機器人,我們將其限制在2500個的子集中。

# Num products to use (subset)NUMBER_PRODUCTS = 2500 # Get the first 2500 productsproduct_metadata = ( all_prods_df .head(NUMBER_PRODUCTS) .to_dict(orient='index')) # Check one of the productsproduct_metadata[0]

02

使用Redis作為向量數(shù)據(jù)庫的設(shè)置

1、LangChain為Redis提供了一個簡單的包裝器,可用于加載文本數(shù)據(jù)并創(chuàng)建捕捉“含義”的嵌入向量。在以下代碼中,我們準(zhǔn)備產(chǎn)品文本和元數(shù)據(jù),準(zhǔn)備文本嵌入的提供程序(OpenAI),為搜索索引分配一個名稱,并提供一個用于連接的Redis URL。

import os from langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores.redis import Redis as RedisVectorStore # set your openAI api key as an environment variableos.environ['OPENAI_API_KEY'] = "YOUR OPENAI API KEY" # data that will be embedded and converted to vectorstexts = [ v['item_name'] for k, v in product_metadata.items()] # product metadata that we'll store along our vectorsmetadatas = list(product_metadata.values()) # we will use OpenAI as our embeddings providerembedding = OpenAIEmbeddings() # name of the Redis search index to createindex_name = "products" # assumes you have a redis stack server running on local hostredis_url = "redis://localhost:6379"

2、然后,我們將它們整合在一起,創(chuàng)建Redis向量存儲。

# create and load redis with documentsvectorstore = RedisVectorStore.from_texts( texts=texts, metadatas=metadatas, embedding=embedding, index_name=index_name, redis_url=redis_url)

03

創(chuàng)建 LangChain 對話鏈

現(xiàn)在我們準(zhǔn)備好創(chuàng)建一個聊天機器人,使用存儲在Redis中的產(chǎn)品數(shù)據(jù)來進行對話。聊天機器人因其極大的實用性而非常受歡迎。在我們下面構(gòu)建的場景中,我們假設(shè)用戶需要穿搭建議。

1、為了引入更多LangChain功能,我們需要導(dǎo)入幾個LangChain工具。

from langchain.callbacks.base import CallbackManagerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerfrom langchain.chains import ( ConversationalRetrievalChain, LLMChain)from langchain.chains.question_answering import load_qa_chainfrom langchain.llms import OpenAIfrom langchain.prompts.prompt import PromptTemplate

2、正如在介紹中提到的,這個項目使用了一個ConversationalRetrievalChain來簡化聊天機器人的開發(fā)。

Redis作為我們的存儲介質(zhì),保存了完整的產(chǎn)品目錄,包括元數(shù)據(jù)和由OpenAI生成的捕捉產(chǎn)品內(nèi)容語義屬性的嵌入向量。通過使用底層的Redis Vector Similarity Search(VSS),我們的聊天機器人可以直接查詢目錄,以找到與用戶購物需求最相似或相關(guān)的產(chǎn)品。這意味著您無需進行繁瑣的關(guān)鍵字搜索或手動過濾,VSS會自動處理這些問題。

構(gòu)成聊天機器人的ConversationalRetrievalChain分為三個階段:

問題創(chuàng)建:在這個階段,聊天機器人評估輸入的問題,并利用OpenAI GPT模型將其與之前的對話交互知識(如果有)結(jié)合起來。通過這個過程,機器人可以更好地理解購物者的問題,并為后續(xù)的檢索提供準(zhǔn)確的上下文。

檢索:在檢索階段,聊天機器人根據(jù)購物者表達的興趣項,搜索Redis數(shù)據(jù)庫,以獲取最佳的可用產(chǎn)品。通過使用Redis Vector Similarity Search(VSS)等技術(shù),機器人能夠快速而準(zhǔn)確地檢索與購物者需求相匹配的產(chǎn)品。

問題回答:在這個階段,聊天機器人從向量搜索的查詢結(jié)果中獲取產(chǎn)品信息,并利用OpenAI GPT模型幫助購物者瀏覽選項。機器人可以生成適當(dāng)?shù)幕卮穑峁┯嘘P(guān)產(chǎn)品特征、價格、評價等方面的信息,以幫助購物者做出決策。

3、雖然LangChain和Redis極大地提升了工作流程的效率,但與大型語言模型(如GPT)進行交互時需要使用"提示(prompt)"來進行溝通。我們創(chuàng)造出一組指令作為提示,以引導(dǎo)模型的行為朝著期望的結(jié)果發(fā)展。為了獲得聊天機器人的最佳效果,需要進一步完善提示的設(shè)置。

template = """Given the following chat history and a follow up question, rephrase the follow up input question to be a standalone question.Or end the conversation if it seems like it's done.Chat History:\"""{chat_history}\"""Follow Up Input: \"""{question}\"""Standalone question:""" condense_question_prompt = PromptTemplate.from_template(template) template = """You are a friendly, conversational retail shopping assistant. Use the following context including product names, descriptions, and keywords to show the shopper whats available, help find what they want, and answer any questions. It's ok if you don't know the answer.Context:\""" {context}\"""Question:\"\""" Helpful Answer:""" qa_prompt= PromptTemplate.from_template(template)

4、接下來,我們定義兩個OpenAI LLM,并分別使用鏈條對其進行封裝,用于問題生成和問題回答。streaming_llm允許我們逐個標(biāo)記地將聊天機器人的響應(yīng)傳輸?shù)絪tdout,從而為用戶提供類似于聊天機器人的用戶體驗。

# define two LLM models from OpenAIllm = OpenAI(temperature=0) streaming_llm = OpenAI( streaming=True, callback_manager=CallbackManager([ StreamingStdOutCallbackHandler() ]), verbose=True, max_tokens=150, temperature=0.2) # use the LLM Chain to create a question creation chainquestion_generator = LLMChain( llm=llm, prompt=condense_question_prompt) # use the streaming LLM to create a question answering chaindoc_chain = load_qa_chain( llm=streaming_llm, chain_type="stuff", prompt=qa_prompt)

5、最后,我們使用ConversationalRetrievalChain將所有三個步驟封裝起來。

chatbot = ConversationalRetrievalChain( retriever=vectorstore.as_retriever(), combine_docs_chain=doc_chain, question_generator=question_generator)

04

虛擬購物助手已就緒

1、請注意,這并不是一個全能的聊天AI。然而,通過Redis的幫助,它存儲了完整的產(chǎn)品庫知識,我們能夠打造出一個相當(dāng)出色的體驗。

# create a chat history bufferchat_history = []# gather user input for the first question to kick off the botquestion = input("Hi! What are you looking for today?") # keep the bot running in a loop to simulate a conversationwhile True: result = chatbot( {"question": question, "chat_history": chat_history} ) print("\n") chat_history.append((result["question"], result["answer"])) question = input()

2、該機器人將實時與您交互,并根據(jù)目錄中的商品幫助您縮小選擇范圍。以下是一個簡單的示例:

Hi! What are you looking for today? >> gold-plated earrings Hi there! I'm happy to help you find the perfect earrings. Do you have a preference for yellow gold plated sterling silver or platinum or gold-plated sterling silver? >> My preference is the yellow gold plated sterling silver Hi there! Are you looking for yellow gold-plated sterling silver earrings with Swarovski Zirconia or Topaz gemstones? We have a few options that might fit the bill. We have yellow gold-plated sterling silver Swarovski Zirconia fancy green stud earrings, yellow gold-plated sterling silver honey topaz stud earrings made with Swarovski Topaz gemstones, and yellow gold-plated sterling silver antique drop earrings set.

3、在聊天機器人用發(fā)出“你好!今天你在找什么?”此類的招呼后,嘗試一些示例提示,或者自己創(chuàng)建一個。

05

定制您的鏈條以提高性能

1、LangChain最好的部分之一是每個類抽象都可以擴展或創(chuàng)建自己的預(yù)設(shè)。我們自定義BaseRetriever類,在返回結(jié)果之前執(zhí)行一些文檔預(yù)處理。

import jsonfrom langchain.schema import BaseRetrieverfrom langchain.vectorstores import VectorStorefrom langchain.schema import Documentfrom pydantic import BaseModel class RedisProductRetriever(BaseRetriever, BaseModel): vectorstore: VectorStore class Config: arbitrary_types_allowed = True def combine_metadata(self, doc) -> str: metadata = doc.metadata return ( "Item Name: " + metadata["item_name"] + ". " + "Item Description: " + metadata["bullet_point"] + ". " + "Item Keywords: " + metadata["item_keywords"] + "." ) def get_relevant_documents(self, query): docs = [] for doc in self.vectorstore.similarity_search(query): content = self.combine_metadata(doc) docs.append(Document( page_content=content, metadata=doc.metadata )) return docs

2、我們需要更新檢索類和聊天機器人,以使用上述的自定義實現(xiàn)。

redis_product_retriever = RedisProductRetriever(vectorstore=vectorstore) chatbot = ConversationalRetrievalChain( retriever=redis_product_retriever, combine_docs_chain=doc_chain, question_generator=question_generator)

3、大功告成!現(xiàn)在你的聊天機器人可以在對話中注入更多的產(chǎn)品信息。以下是另一個短對話的示例:

printf("hello Hi! What are you looking for today? >>> fancy footwear for going out Hi there! We have a few great options for women's shoes and sandals. We have the Amazon Brand - The Fix Women's Giana Open Toe Bootie with Pearl Buckle, bright white leather, 9.5 B US, Flavia Women's Beige Fashion Sandals-7 UK (39 EU) (8 US) (FL/236/BEG), Flavia Women's Blue Fashion Sandals-8 UK (40 EU) (9 US) (FL/211/BLU), and The Fix Women's Faris Flat Slide Sandal with Pearls. All of these shoes feature a variety of styles and colors to choose from. Let me know if you have any questions about any of these items! >>> These are nice. However, I am looking for men's shoes. Can you help me? Hi there! We have a great selection of men's formal shoes available. We have Amazon Brand - Symbol Men's Formal Shoes, Amazon Brand - Symbol Men's Leather Formal Shoes, and more. All of our formal shoes are made from high quality materials and feature a variety of closure types, toe styles, and heel types. They also come with a manufacturer's warranty and care instructions to ensure they last. Let me know if you have any questions or need help finding the perfect pair of shoes for you! >>>Can you show me some more men's options? Hi there! I'm here to help you find the perfect item for you. We have a few options available for men's formal shoes. We have the Men's Stainless Steel Link Bracelet, the Amazon Brand - Arthur Harvey Men's Leather Formal Shoes, and the Amazon Brand - Symbol Men's Formal Derby shoes. All of these items feature a variety of features such as leather material, lace-up closure, pointed toe, block heel, and more. If you have any questions about any of these items, please let me know. I'm happy to help! >>> Ok this looks great, thanks!world!");

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 機器人
    +關(guān)注

    關(guān)注

    210

    文章

    27835

    瀏覽量

    204564
  • 人工智能
    +關(guān)注

    關(guān)注

    1787

    文章

    46051

    瀏覽量

    234943
  • VSS
    VSS
    +關(guān)注

    關(guān)注

    1

    文章

    31

    瀏覽量

    21367
收藏 人收藏

    評論

    相關(guān)推薦

    馬斯克旗下AI初創(chuàng)公司發(fā)布Grok-2聊天機器人

    埃隆·馬斯克麾下的創(chuàng)新AI企業(yè)xAI今日震撼發(fā)布了其最新力作——Grok-2聊天機器人,該產(chǎn)品在性能上自信地宣稱已能與業(yè)界巨頭OpenAI、谷歌及Anthropic的頂尖產(chǎn)品并駕齊驅(qū)。
    的頭像 發(fā)表于 08-15 16:05 ?507次閱讀

    谷歌計劃推出明星網(wǎng)紅AI聊天機器人,與Meta展開技術(shù)競爭

    在科技界風(fēng)起云涌的當(dāng)下,谷歌再次憑借其前瞻性的戰(zhàn)略布局和創(chuàng)新能力,成為了公眾關(guān)注的焦點。據(jù)The Information的獨家爆料,谷歌正秘密研發(fā)款全新的AI聊天機器人,該機器人將基于明星和YouTube網(wǎng)紅的數(shù)據(jù)
    的頭像 發(fā)表于 06-26 18:23 ?918次閱讀

    亞馬遜秘密研發(fā)AI聊天機器人Metis,挑戰(zhàn)ChatGPT

    科技巨頭亞馬遜近日被曝正在秘密研發(fā)款代號為“Metis”的人工智能(AI)聊天機器人,意圖與OpenAI的ChatGPT較高下。Metis,這個名字源于希臘神話中的智慧女神,象征著
    的頭像 發(fā)表于 06-26 18:08 ?921次閱讀

    AI聊天機器人Grok向歐洲X平臺Premium會員開放

    社交平臺X的首席執(zhí)行官琳達·亞卡里諾宣布,全新研發(fā)的Grok人工智能聊天機器人正式向歐洲的X Premium會員提供服務(wù)。Grok是X平臺精心打造的款A(yù)I聊天機器人,旨在提升歐洲會員的交流體驗。
    的頭像 發(fā)表于 05-17 09:38 ?289次閱讀

    Anthropic在歐洲推出Claude聊天機器人

    AI初創(chuàng)公司Anthropic在歐洲市場邁出了重要步伐,成功推出了Claude聊天機器人及其相應(yīng)的訂閱計劃。此舉旨在進步增加用戶數(shù)量和提升公司收入。Anthropic公司表示,其基礎(chǔ)軟件產(chǎn)品已經(jīng)在歐洲的金融、酒店等行業(yè)吸引了眾多關(guān)注。
    的頭像 發(fā)表于 05-15 09:36 ?276次閱讀

    使用Ryzen ? AI處理器構(gòu)建聊天機器人

    探索Ryzen ? AI技術(shù)的構(gòu)建模塊,并展示利用它來構(gòu)建僅在Ryzen AI筆記本電腦上以最佳性能運行的AI聊天機器人是多么容易。 全
    的頭像 發(fā)表于 05-11 14:22 ?3389次閱讀
    使用Ryzen ? AI處理器<b class='flag-5'>構(gòu)建</b><b class='flag-5'>聊天機器人</b>

    揭秘聊天機器人的“大腦”-大語言模型

    如果說 AI 正處于改變歷史的“iPhone 時刻”,那么聊天機器人就是其首批熱門應(yīng)用之。
    的頭像 發(fā)表于 04-17 10:01 ?588次閱讀
    揭秘<b class='flag-5'>聊天機器人</b>的“大腦”-大語言模型

    英偉達引領(lǐng)AI新潮流,推出“Chat with RTX”聊天機器人

    隨著人工智能技術(shù)的突飛猛進,英偉達(NVIDIA)再次展現(xiàn)了其技術(shù)領(lǐng)導(dǎo)地位,為Windows PC用戶帶來了款革命性的本地聊天機器人應(yīng)用程序——“Chat with RTX”。這創(chuàng)新應(yīng)用的推出,不僅標(biāo)志著人工智能在日常生活中
    的頭像 發(fā)表于 02-19 11:11 ?849次閱讀

    英偉達推出全新AI聊天機器人

    近日,英偉達(Nvidia)宣布推出其全新的AI聊天機器人——“Chat With RTX”。這款聊天機器人被視為英偉達版的ChatGPT,為用戶提供了全新的、本地化的AI交互體驗
    的頭像 發(fā)表于 02-19 11:09 ?749次閱讀

    谷歌AI聊天機器人改名為Gemini

    谷歌(Google)近日宣布,旗下備受矚目的AI聊天機器人Bard正式更名為Gemini,并推出了款功能更加強大的付費版本——Gemini Advanced。這戰(zhàn)略調(diào)整旨在與微軟、Open
    的頭像 發(fā)表于 02-18 11:28 ?922次閱讀

    字節(jié)跳動推出AI聊天機器人Coze扣子

    近日,字節(jié)跳動正式推出了名為“Coze扣子”的AI聊天機器人開發(fā)平臺。自2月1日起,這平臺已正式上線,為開發(fā)者和用戶提供了全新的交互體驗。
    的頭像 發(fā)表于 02-03 09:31 ?1457次閱讀

    RedisLangChain定制AI代理——OpenGPTs

    OpenAI最近推出了OpenAIGPTs——構(gòu)建定制化AI代理的無代碼“應(yīng)用商店”,隨后LangChain開發(fā)了類似的開源工具Open
    的頭像 發(fā)表于 01-13 08:03 ?731次閱讀
    用<b class='flag-5'>Redis</b>為<b class='flag-5'>LangChain</b>定制AI代理——OpenGPTs

    OpenAI將推出GPT Store,供用戶銷售及分享基于GPT的聊天機器人

    為方便用戶使用,GPTStore將設(shè)置搜索欄,以供快速查找相關(guān)的聊天機器人。排行榜則會甄選熱門的GPT及受歡迎程度高的開發(fā)者作品予以展示。
    的頭像 發(fā)表于 01-05 10:41 ?415次閱讀

    如何用AI聊天機器人寫出萬字長文

    如何用AI聊天機器人寫出萬字長文
    的頭像 發(fā)表于 12-26 16:25 ?944次閱讀

    Meta面向年輕用戶推出款生成型人工智能聊天機器人

    ,繼ChatGPT去年成為有史以來增長最快的應(yīng)用程序之之后,它們將以多種“角色”的形式推出,旨在以更豐富多彩的行為吸引年輕用戶。類似但更具普遍針對性的Met 聊天機器人角色已經(jīng)在Instagram上進行了測試。 根據(jù)《華爾街日報》查看的內(nèi)部
    的頭像 發(fā)表于 10-07 17:05 ?981次閱讀