使用LangChain和DeepInfra構建客戶支持聊天機器人的操作指南
譯文譯者 | 布加迪
審校 | 重樓
您可能在日常的網上互動中遇到過聊天機器人,但有沒有考慮過底層為這些數字助手提供支持的技術?聊天機器人(尤其在客戶支持領域)已經成為現代企業的一個主力工具,在提高效率的同時改進了客戶服務。今天,我們將深入研究LangChain和DeepInfra如何使這種聊天機器人變得響應更迅即、更高效。
聊天機器人的基本組成部分
不妨先了解基礎知識——聊天機器人的核心組件有哪些?在開發一個響應迅即又高效的聊天機器人時,三個要素必不可少:模型、提示模板(Prompt Template)和記憶。
模型代表了聊天機器人背后的AI大腦,它負責理解和響應用戶輸入。提示模板引導聊天機器人的響應,確保它們的回復緊扣對話主題。最后,記憶保持交互的狀態,使聊天機器人能夠記住過去的對話,并利用它們來理解當前對話的上下文。
操作指南
現在開始動手吧。我們將逐步介紹使用LangChain和DeepInfra構建客戶支持聊天機器人的過程。我們假設這個聊天機器人在一家在線服裝店“工作”,可以幫助顧客為他們挑選衣服。
- 獲取DeepInfra API密鑰
DeepInfra擁有其簡單的API和可擴展的生產級基礎設施,使您可以輕松運行主流的AI模型。首先,您需要使用該鏈接獲取DeepInfra API密鑰,以便與其服務進行交互。一旦有了密鑰,您就可以在環境中設置API令牌,如下所示:
from getpass import getpass
import os
# Set the DeepInfra API token
DEEPINFRA_API_TOKEN = getpass()
os.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN
- 建立LangChain和DeepInfra環境
接下來,您需要創建LangChain和DeepInfra環境。導入必要的組件,并為DeepInfra模型創建實例。比如說,您可以使用像“databricks/dolly-v2-12b”這樣的模型:
from langchain import ConversationChain, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory
from langchain.llms import DeepInfra
# Create the DeepInfra instance
llm = DeepInfra(model_id="databricks/dolly-v2-12b")
llm.model_kwargs = {'temperature': 0.7, 'repetition_penalty': 1.2, 'max_new_tokens': 250, 'top_p': 0.9}
- 注意:為聊天機器人選擇和部署合適的模型
您可以為LLM使用許多不同的模型。這個例子展示了如何使用databricks/dolly-v2-12b模型,但在DeepInfra上還有許多其他模型可供使用。由于選擇眾多,您可能希望使用像AIModels這樣的工具,希望找到可與LangChain結合使用的合適的LLM。您可以隨意搜索、過濾和篩選AI模型,以便找到最適合您項目的模型。查看DeepInfra頁面,即可找到可供選擇的模型。
- 創建提示模板以指導聊天機器人的響應
現在,是時候定義提示模板來指導聊天機器人的響應了。這將確保聊天機器人的響應與上下文和用戶的輸入保持一致。我嘗試了幾個不同的模板,要得到一個完美的模板并非易事。設計正確提示的過程名為提示工程。最終,我能夠重復使用我在Pinecone網站上找到的一個模板。
template = """Given the following user prompt and conversation log, formulate a question that would be the most relevant to provide the user with an answer from a knowledge base.
You should follow the following rules when generating and answer:
- Always prioritize the user prompt over the conversation log.
- Ignore any conversation log that is not directly related to the user prompt.
- Only attempt to answer if a question was posed.
- The question should be a single sentence.
- You should remove any punctuation from the question.
- You should remove any words that are not relevant to the question.
- If you are unable to formulate a question, respond with the same USER PROMPT you got.
Conversation log: {history}
USER PROMPT: {human_input}
Your response:
"""
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template
)
- 初始化聊天機器人,并設置記憶
準備好模型和提示模板后,下一步是初始化聊天機器人,并設置記憶,以保持交互的狀態。
# Now using DeepInfra with the LLMChain
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(k=2),
)
- 運行聊天機器人并與之交互
最后,您現在可以與聊天機器人進行交互了。不妨看一個例子:
output = llm_chain.predict(human_input="Hello! What clothes do you recommend I buy to rebuild my summer wardrobe")
print(output)
因而生成的響應推薦一些衣服:
In the context of summer wardrobe recommendations, you should buy your clothes from the following list:
- V-neck T-shirts
- Tank Tops
- Solid Color Swim Shorts
- Swim Shorts
- Skirts
- Cardigans
- Sandals
聊天機器人中的記憶概念
記憶在聊天機器人中起著至關重要的作用。它有助于維持聊天機器人交互中的上下文和歷史記錄,從而使聊天機器人能夠回憶過去的對話,并理解當前對話的上下文。這種能力對于創造更人性化的交互從而改善用戶體驗至關重要。記憶方面的話題有很多文章值得深入研究,建議您看看這篇指南,以了解更多的信息。
更多參考資料和示例
為了進一步理解,我建議查看Langchain網站上的ChatGPT Clone筆記本、Conversation Memory筆記本和Conversation Agent筆記本等資源。這些資源更深入地介紹了記憶概念,記憶關鍵概念和記憶示例提供了實用指導。
您還應該查看AIModels.fyi上的其他Langchain指南。
DeepInfra還為其平臺提供了完備的文檔,甚至還有一個博客,您可以獲取詳細的帖子、指南和文章。
結論
使用LangChain和DeepInfra構建面向客戶支持的聊天機器人最初可能看起來很復雜,但一旦您了解了基本組件和步驟,整個過程就會變得簡單得多。利用這些技術可以顯著改進客戶服務,提高業務效率,并提高總體客戶滿意度。將來,這些技術會變得真正大有潛力,預計它們會繼續發展,并影響客戶服務領域。
原文標題:Building a Customer Support Chatbot with LangChain and DeepInfra: A Step-by-Step Guide,作者:Mike Young