{"id":28511,"date":"2023-11-24T17:03:00","date_gmt":"2023-11-24T09:03:00","guid":{"rendered":"https:\/\/haocst.com\/?p=28511"},"modified":"2025-03-06T10:59:38","modified_gmt":"2025-03-06T02:59:38","slug":"based-on-redis-enterprise-langchain-openai-build-an-e-commerce-chatbot","status":"publish","type":"post","link":"https:\/\/www.aqycmy.com\/based-on-redis-enterprise-langchain-openai-build-an-e-commerce-chatbot\/","title":{"rendered":"\u57fa\u4e8eRedis\u00a0Enterprise\uff0cLangChain\uff0cOpenAI \u6784\u5efa\u4e00\u4e2a\u7535\u5b50\u5546\u52a1\u804a\u5929\u673a\u5668\u4eba"},"content":{"rendered":"\n
97622娱乐电子游戏
\n\u6458\u8981<\/p>\n\u672c\u6587\u4ecb\u7ecd\u4e86\u70ed\u95e8\u7684 LangChain \u6846\u67b6\uff0c\u5728\u4eba\u5de5\u667a\u80fd\u5de5\u5177\u5174\u8d77\u65f6\uff0c\u5b83\u5907\u53d7\u5173\u6ce8\uff0c\u80fd\u7b80\u5316\u5f00\u53d1\u4eba\u5458\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u6d41\u7a0b\uff0c\u5982\u6784\u5efa\u5bf9\u8bdd\u68c0\u7d22\u94fe\u6761\u5b9e\u73b0\u7528\u6237\u4e0e\u6570\u636e\u4ea4\u4e92\u3002\u4ee5\u6784\u5efa\u96f6\u552e\u8d2d\u7269\u52a9\u624b\u4e3a\u4f8b\uff1a\u5148\u5b89\u88c5 Python \u7b49\u4f9d\u8d56\u9879\uff0c\u51c6\u5907\u5e76\u9884\u5904\u7406\u4e9a\u9a6c\u900a\u6570\u636e\u96c6\uff1b\u518d\u7528 LangChain \u5305\u88c5\u5668\u5904\u7406\u6570\u636e\uff0c\u6574\u5408\u5f62\u6210 Redis \u5411\u91cf\u5b58\u50a8\uff1b\u63a5\u7740\u5bfc\u5165\u5de5\u5177\u521b\u5efa\u542b\u4e09\u9636\u6bb5\u7684\u804a\u5929\u673a\u5668\u4eba\uff1b\u6700\u540e\uff0c\u865a\u62df\u8d2d\u7269\u52a9\u624b\u53ef\u5b9e\u65f6\u4ea4\u4e92\uff0c\u4f9d\u4ea7\u54c1\u76ee\u5f55\u5e2e\u7528\u6237\u7b5b\u9009\uff0c\u8fd8\u80fd\u5b9a\u5236\u94fe\u6761\u63d0\u5347\u6027\u80fd\uff0c\u63d0\u4f9b\u4f18\u8d28\u8d2d\u7269\u4f53\u9a8c\u3002<\/cite><\/blockquote>\n\n\n\n
<\/div>\n\n\n\n\u9274\u4e8e\u6700\u8fd1\u4eba\u5de5\u667a\u80fd\u652f\u6301\u7684API\u548c\u7f51\u7edc\u5f00\u53d1\u5de5\u5177\u7684\u6fc0\u589e\uff0c\u4f3c\u4e4e\u6bcf\u4e2a\u4eba\u90fd\u5728\u5c06\u804a\u5929\u673a\u5668\u4eba\u96c6\u6210\u5230\u4ed6\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n\n\n\n
LangChain\u662f\u4e00\u79cd\u5907\u53d7\u6b22\u8fce\u7684\u65b0\u6846\u67b6\uff0c\u8fd1\u671f\u5f15\u8d77\u4e86\u5e7f\u6cdb\u5173\u6ce8\u3002\u8be5\u6846\u67b6\u65e8\u5728\u7b80\u5316\u5f00\u53d1\u4eba\u5458\u4e0e\u8bed\u8a00\u6a21\u578b<\/strong>\u3001\u5916\u90e8\u6570\u636e<\/strong>\u548c\u8ba1\u7b97\u8d44\u6e90<\/strong>\u8fdb\u884c\u4ea4\u4e92\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8fc7\u7a0b\u3002\u5b83\u901a\u8fc7\u6e05\u6670\u4e14\u6a21\u5757\u5316\u7684\u62bd\u8c61\uff0c\u5173\u6ce8\u6784\u5efa\u6240\u9700\u7684\u6240\u6709\u6784\u5efa\u6a21\u5757\uff0c\u5e76\u6784\u5efa\u4e86\u5e38\u7528\u7684\u201d\u94fe\u6761\u201d\uff0c\u5373\u6784\u5efa\u6a21\u5757\u7684\u7ec4\u5408\u3002\u4f8b\u5982\uff0c\u5bf9\u8bdd\u68c0\u7d22\u94fe\u6761\u53ef\u4ee5\u8ba9\u7528\u6237\u4e0e\u5916\u90e8\u5b58\u50a8\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b9e\u73b0\u771f\u5b9e\u7684\u5bf9\u8bdd\u4f53\u9a8c\u3002<\/p>\n\n\n\n
LangChain\u662f\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5462\uff1fOpenAI\u7684\u8bed\u8a00\u6a21\u578b\u5e76\u6ca1\u6709\u9488\u5bf9\u7279\u5b9a\u4f01\u4e1a\u7684\u5177\u4f53\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\u6216\u4f18\u5316\u3002\u5982\u679c\u60a8\u7684\u804a\u5929\u673a\u5668\u4eba\u4f9d\u8d56\u4e8e\u8be5\u6846\u67b6\uff0c\u60a8\u9700\u8981\u5728\u8fd0\u884c\u65f6\u5411OpenAI\u63d0\u4f9b\u6570\u636e\u3002\u5728\u68c0\u7d22\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5411\u91cf\u76f8\u4f3c\u6027\u641c\u7d22\uff08VSS\uff09\u4eceRedis\u4e2d\u83b7\u53d6\u4e0e\u7528\u6237\u67e5\u8be2\u76f8\u5173\u7684\u6570\u636e\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6570\u636e\u4e0e\u539f\u59cb\u95ee\u9898\u4e00\u8d77\u8f93\u5165\u5230\u8bed\u8a00\u6a21\u578b\u4e2d\u3002<\/strong>\u8fd9\u8981\u6c42\u6a21\u578b\u4ec5\u4f7f\u7528\u63d0\u4f9b\u7684\u4fe1\u606f\uff08\u5728\u4eba\u5de5\u667a\u80fd\u9886\u57df\u4e2d\u79f0\u4e3a\u201d\u4e0a\u4e0b\u6587\u201d\uff09\u6765\u56de\u7b54\u95ee\u9898\u3002<\/p>\n\n\n\n
\u8fd9\u4e2a\u94fe\u6761\u4e2d\u7684\u5927\u90e8\u5206\u590d\u6742\u6027\u90fd\u5f52\u7ed3\u4e8e\u68c0\u7d22\u6b65\u9aa4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9009\u62e9\u5c06LangChain\u4e0eRedis Enterprise\u96c6\u6210\u4e3a\u4e00\u4e2a\u5411\u91cf\u6570\u636e\u5e93\u3002\u8fd9\u79cd\u7ec4\u5408\u4e3a\u590d\u6742\u7684\u4eba\u5de5\u667a\u80fd\u548c\u4ea7\u54c1\u5f00\u53d1\u4e4b\u95f4\u642d\u5efa\u4e86\u6865\u6881\u3002<\/p>\n\n\n\n
\u5728\u8fd9\u4e2a\u7b80\u77ed\u7684\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u4f1a\u8bdd\u5f0f\u7684\u96f6\u552e\u8d2d\u7269\u52a9\u624b\uff0c\u5e2e\u52a9\u987e\u5ba2\u5728\u4ea7\u54c1\u76ee\u5f55\u4e2d\u53d1\u73b0\u90a3\u4e9b\u88ab\u57cb\u85cf\u7684\u4ee4\u4eba\u611f\u5174\u8da3\u7684\u5546\u54c1\u3002\u8bfb\u8005\u53ef\u4ee5\u6309\u7167\u63d0\u4f9b\u7684\u5b8c\u6574\u4ee3\u7801\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n
\u4e00\u3001\u6784\u5efa\u4f60\u7684\u804a\u5929\u673a\u5668\u4eba<\/h2>\n\n\n\n
\u9996\u5148\uff0c\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u7ec4\u4ef6\u3002<\/p>\n\n\n\n
\uff08\u4e00\uff09\u5b89\u88c5 Python \u4f9d\u8d56\u9879<\/p>\n\n\n\n
\u8fd9\u4e2a\u9879\u76ee\u9700\u8981\u4e00\u4e9bPython\u5e93\u3002\u8fd9\u4e9b\u5e93\u5b58\u50a8\u5728github\u4ed3\u5e93\u7684requirements.txt\u6587\u4ef6\u4e2d\u3002(Github\uff1ahttps:\/\/github.com\/RedisVentures\/redis-langchain-chatbot)<\/p>\n\n\n\n
pip install langchain==0.0.123<\/p>\n\n\n\n
pip install openai==0.27.2<\/p>\n\n\n\n
pip install redis==4.5.3<\/p>\n\n\n\n
pip install numpy<\/p>\n\n\n\n
pip install pandas<\/p>\n\n\n\n
pip install gdown<\/p>\n\n\n\n
\uff08\u4e8c\uff09\u51c6\u5907\u4ea7\u54c1\u6570\u636e\u96c6<\/p>\n\n\n\n
1\u3001\u5bf9\u4e8e\u96f6\u552e\u804a\u5929\u673a\u5668\u4eba\uff0c\u6211\u4eec\u9009\u62e9\u4f7f\u7528Amazon Berkeley Objects\u6570\u636e\u96c6\u3002\u8be5\u6570\u636e\u96c6\u5305\u542b\u4e86\u5927\u91cf\u9002\u7528\u4e8e\u751f\u6210\u96f6\u552e\u52a9\u624b\u7684\u4e9a\u9a6c\u900a\u4ea7\u54c1\u3002<\/p>\n\n\n\n
2\u3001\u4f7f\u7528Python\u7684pandas\u5e93\u6765\u52a0\u8f7d\u548c\u9884\u5904\u7406\u6570\u636e\u96c6\u3002\u5728\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u622a\u65ad\u8f83\u957f\u7684\u6587\u672c\u5b57\u6bb5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u7684\u6570\u636e\u96c6\u4f1a\u66f4\u52a0\u7cbe\u7b80\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u548c\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n\n\n\n
import pandas as pd<\/p>\n\n\n\n
MAX_TEXT_LENGTH=1000 # Maximum num of text characters to use<\/p>\n\n\n\n
def auto_truncate(val):<\/p>\n\n\n\n
\u201c\u201d\u201dTruncate the given text.\u201d\u201d\u201d<\/p>\n\n\n\n
return val[:MAX_TEXT_LENGTH]<\/p>\n\n\n\n
# Load Product data and truncate long text fields<\/p>\n\n\n\n
all_prods_df = pd.read_csv(\u201cproduct_data.csv\u201d, converters={<\/p>\n\n\n\n
\u2018bullet_point\u2019: auto_truncate,<\/p>\n\n\n\n
\u2018item_keywords\u2019: auto_truncate,<\/p>\n\n\n\n
\u2018item_name\u2019: auto_truncate<\/p>\n\n\n\n
})<\/p>\n\n\n\n
3\u3001\u5728\u5b8c\u5168\u52a0\u8f7d\u4e86\u6211\u4eec\u7684\u4ea7\u54c1\u6570\u636e\u96c6\u4e4b\u540e\uff0c\u8fdb\u884c\u4e00\u4e9b\u6700\u540e\u7684\u9884\u5904\u7406\u6b65\u9aa4\uff0c\u4ee5\u6e05\u7406\u5173\u952e\u8bcd\u5b57\u6bb5\u5e76\u5220\u9664\u7f3a\u5931\u503c\u3002<\/p>\n\n\n\n
# Replace empty strings with None and drop<\/p>\n\n\n\n
all_prods_df[\u2018item_keywords\u2019].replace(\u201d, None, inplace=True)<\/p>\n\n\n\n
all_prods_df.dropna(subset=[\u2018item_keywords\u2019], inplace=True)<\/p>\n\n\n\n
# Reset pandas dataframe index<\/p>\n\n\n\n
all_prods_df.reset_index(drop=True, inplace=True)<\/p>\n\n\n\n
4\u3001\u5982\u679c\u4f60\u6301\u7eed\u5728\u8ddf\u8fdbGitHub\u4e0a\u7684\u4ee3\u7801\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528all_prods_df.head()\u6765\u67e5\u770b\u6570\u636e\u6846\u7684\u524d\u51e0\u884c\u3002\u5b8c\u6574\u7684\u6570\u636e\u96c6\u5305\u542b\u8d85\u8fc7100,000\u4e2a\u4ea7\u54c1\uff0c\u4f46\u662f\u5bf9\u4e8e\u8fd9\u4e2a\u804a\u5929\u673a\u5668\u4eba\uff0c\u6211\u4eec\u5c06\u5176\u9650\u5236\u57282500\u4e2a\u7684\u5b50\u96c6\u4e2d\u3002<\/p>\n\n\n\n
# Num products to use (subset)<\/p>\n\n\n\n
NUMBER_PRODUCTS = 2500 <\/p>\n\n\n\n
# Get the first 2500 products<\/p>\n\n\n\n
product_metadata = (<\/p>\n\n\n\n
all_prods_df<\/p>\n\n\n\n
.head(NUMBER_PRODUCTS)<\/p>\n\n\n\n
.to_dict(orient=\u2019index\u2019)<\/p>\n\n\n\n
)<\/p>\n\n\n\n
# Check one of the products<\/p>\n\n\n\n
product_metadata[0]<\/p>\n\n\n\n
5\u3001\u4e0b\u9762\u662f\u6211\u4eec\u8981\u5904\u7406\u7684\u4ea7\u54c1JSON\u5bf9\u8c61\u7684\u4e00\u4e2a\u793a\u4f8b\u3002<\/p>\n\n\n\n
{\u2018item_id\u2019: \u2018B07T2JY31Y\u2019,<\/p>\n\n\n\n
\u2018marketplace\u2019: \u2018Amazon\u2019,<\/p>\n\n\n\n
\u2018country\u2019: \u2018IN\u2019,<\/p>\n\n\n\n
\u2018main_image_id\u2019: \u201971vX7qIEAIL\u2019,<\/p>\n\n\n\n
\u2018domain_name\u2019: \u2018amazon.in\u2019,<\/p>\n\n\n\n
\u2018bullet_point\u2019: \u20183D Printed Hard Back Case mobiles Cover for Sony Xperia Z1 L39H Easy to put & take off with perfect cutouts for volume buttons, audio & charging ports. Stylish design and appearance, express your unique personality. Extreme precision design allows easy access to all buttons and ports while featuring raised bezel to life screen and camera off flat surface. Slim Hard Back Cover No Warranty\u2019,<\/p>\n\n\n\n
\u2018item_keywords\u2019: \u2018mobiles cover back cover mobiles case phones case mobiles panel phones panel LG mobiles case LG phones cover LG back case hard case 3D printed mobiles cover mobiles cover back cover mobiles case phones case mobiles panel phones panel Sony Xperia mobiles case Sony Xperia phones cover Sony Xperia back case hard case 3D printed mobiles cover mobiles cover back cover mobiles case phones case mobiles panel phones panel Sony Xperia mobiles case Sony Xperia phones cover Sony Xperia back case hard case 3D printed mobiles cover mobiles cove\u2019,<\/p>\n\n\n\n
\u2018material\u2019: \u2018Wood\u2019,<\/p>\n\n\n\n
\u2018brand\u2019: \u2018Amazon Brand \u2013 Solimo\u2019,<\/p>\n\n\n\n
\u2018color\u2019: \u2018others\u2019,<\/p>\n\n\n\n
\u2018item_name\u2019: \u2018Amazon Brand \u2013 Solimo Designer Leaf on Wood 3D Printed Hard Back Case mobiles Cover for Sony Xperia Z1 L39H\u2019,<\/p>\n\n\n\n
\u2018model_name\u2019: \u2018Sony Xperia Z1 L39H\u2019,<\/p>\n\n\n\n
\u2018model_number\u2019: \u2018gz8056-SL40528\u2019,<\/p>\n\n\n\n
\u2018product_type\u2019: \u2018CELLULAR_phones_CASE\u2019}<\/p>\n\n\n\n
\u4e8c\u3001\u4f7f\u7528Redis\u4f5c\u4e3a\u5411\u91cf\u6570\u636e\u5e93\u7684\u8bbe\u7f6e<\/h2>\n\n\n\n
1\u3001LangChain\u4e3aRedis\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5305\u88c5\u5668\uff0c\u53ef\u7528\u4e8e\u52a0\u8f7d\u6587\u672c\u6570\u636e\u5e76\u521b\u5efa\u6355\u6349\u201c\u542b\u4e49\u201d\u7684\u5d4c\u5165\u5411\u91cf\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u51c6\u5907\u4ea7\u54c1\u6587\u672c\u548c\u5143\u6570\u636e\uff0c\u51c6\u5907\u6587\u672c\u5d4c\u5165\u7684\u63d0\u4f9b\u7a0b\u5e8f\uff08OpenAI\uff09\uff0c\u4e3a\u641c\u7d22\u7d22\u5f15\u5206\u914d\u4e00\u4e2a\u540d\u79f0\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u7528\u4e8e\u8fde\u63a5\u7684Redis URL\u3002<\/p>\n\n\n\n
import os<\/p>\n\n\n\n
from langchain.embeddings import OpenAIEmbeddings<\/p>\n\n\n\n
from langchain.vectorstores.redis import Redis as RedisVectorStore<\/p>\n\n\n\n
# set your openAI api key as an environment variable<\/p>\n\n\n\n
os.environ[\u2018OPENAI_API_KEY\u2019] = \u201cYOUR OPENAI API KEY\u201d<\/p>\n\n\n\n
# data that will be embedded and converted to vectors<\/p>\n\n\n\n
texts = [<\/p>\n\n\n\n
v[\u2018item_name\u2019] for k, v in product_metadata.items()<\/p>\n\n\n\n
]<\/p>\n\n\n\n
# product metadata that we\u2019ll store along our vectors<\/p>\n\n\n\n
metadatas = list(product_metadata.values())<\/p>\n\n\n\n
# we will use OpenAI as our embeddings provider<\/p>\n\n\n\n
embedding = OpenAIEmbeddings()<\/p>\n\n\n\n
# name of the Redis search index to create<\/p>\n\n\n\n
index_name = \u201cproducts\u201d<\/p>\n\n\n\n
# assumes you have a redis stack server running on local host<\/p>\n\n\n\n
redis_url = \u201credis:\/\/localhost:6379\u201d<\/p>\n\n\n\n
\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u5904\u7406\u4e86Amazon\u4ea7\u54c1\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u5230\u4e86\u5177\u6709\u5411\u91cf\u5d4c\u5165\u7684Redis\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n\n\n\n
2\u3001\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5b83\u4eec\u6574\u5408\u5728\u4e00\u8d77\uff0c\u521b\u5efaRedis\u5411\u91cf\u5b58\u50a8\u3002<\/p>\n\n\n\n
# create and load redis with documents<\/p>\n\n\n\n
vectorstore = RedisVectorStore.from_texts(<\/p>\n\n\n\n
texts=texts,<\/p>\n\n\n\n
metadatas=metadatas,<\/p>\n\n\n\n
embedding=embedding,<\/p>\n\n\n\n
index_name=index_name,<\/p>\n\n\n\n
redis_url=redis_url<\/p>\n\n\n\n
)<\/p>\n\n\n\n
\u4e09\u3001\u521b\u5efa LangChain \u5bf9\u8bdd\u94fe<\/h2>\n\n\n\n
\u73b0\u5728\u6211\u4eec\u51c6\u5907\u597d\u521b\u5efa\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba\uff0c\u4f7f\u7528\u5b58\u50a8\u5728Redis\u4e2d\u7684\u4ea7\u54c1\u6570\u636e\u6765\u8fdb\u884c\u5bf9\u8bdd\u3002\u804a\u5929\u673a\u5668\u4eba\u56e0\u5176\u6781\u5927\u7684\u5b9e\u7528\u6027\u800c\u975e\u5e38\u53d7\u6b22\u8fce\u3002\u5728\u6211\u4eec\u4e0b\u9762\u6784\u5efa\u7684\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u7528\u6237\u9700\u8981\u7a7f\u642d\u5efa\u8bae\u3002<\/p>\n\n\n\n
1\u3001\u4e3a\u4e86\u5f15\u5165\u66f4\u591aLangChain\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u51e0\u4e2aLangChain\u5de5\u5177\u3002<\/p>\n\n\n\n
from langchain.callbacks.base import CallbackManager<\/p>\n\n\n\n
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler<\/p>\n\n\n\n
from langchain.chains import (<\/p>\n\n\n\n
ConversationalRetrievalsChain,<\/p>\n\n\n\n
LLMChain<\/p>\n\n\n\n
)<\/p>\n\n\n\n
from langchain.chains.question_answering import load_qa_chain<\/p>\n\n\n\n
from langchain.llms import OpenAI<\/p>\n\n\n\n
from langchain.prompts.prompt import PromptTemplate<\/p>\n\n\n\n
2\u3001\u6b63\u5982\u5728\u4ecb\u7ecd\u4e2d\u63d0\u5230\u7684\uff0c\u8fd9\u4e2a\u9879\u76ee\u4f7f\u7528\u4e86\u4e00\u4e2aConversationalRetrievalsChain\u6765\u7b80\u5316\u804a\u5929\u673a\u5668\u4eba\u7684\u5f00\u53d1\u3002<\/p>\n\n\n\n
Redis\u4f5c\u4e3a\u6211\u4eec\u7684\u5b58\u50a8\u4ecb\u8d28\uff0c\u4fdd\u5b58\u4e86\u5b8c\u6574\u7684\u4ea7\u54c1\u76ee\u5f55\uff0c\u5305\u62ec\u5143\u6570\u636e\u548c\u7531OpenAI\u751f\u6210\u7684\u6355\u6349\u4ea7\u54c1\u5185\u5bb9\u8bed\u4e49\u5c5e\u6027\u7684\u5d4c\u5165\u5411\u91cf\u3002\u901a\u8fc7\u4f7f\u7528\u5e95\u5c42\u7684Redis Vector Similarity Search\uff08VSS\uff09\uff0c\u6211\u4eec\u7684\u804a\u5929\u673a\u5668\u4eba\u53ef\u4ee5\u76f4\u63a5\u67e5\u8be2\u76ee\u5f55\uff0c\u4ee5\u627e\u5230\u4e0e\u7528\u6237\u8d2d\u7269\u9700\u6c42\u6700\u76f8\u4f3c\u6216\u76f8\u5173\u7684\u4ea7\u54c1\u3002\u8fd9\u610f\u5473\u7740\u60a8\u65e0\u9700\u8fdb\u884c\u7e41\u7410\u7684\u5173\u952e\u5b57\u641c\u7d22\u6216\u624b\u52a8\u8fc7\u6ee4\uff0cVSS\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n
\u6784\u6210\u804a\u5929\u673a\u5668\u4eba\u7684ConversationalRetrievalsChain\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\uff1a<\/p>\n\n\n\n
\n
- \u95ee\u9898\u521b\u5efa\uff1a\u5728\u8fd9\u4e2a\u9636\u6bb5\uff0c\u804a\u5929\u673a\u5668\u4eba\u8bc4\u4f30\u8f93\u5165\u7684\u95ee\u9898\uff0c\u5e76\u5229\u7528OpenAI GPT\u6a21\u578b\u5c06\u5176\u4e0e\u4e4b\u524d\u7684\u5bf9\u8bdd\u4ea4\u4e92\u77e5\u8bc6\uff08\u5982\u679c\u6709\uff09\u7ed3\u5408\u8d77\u6765\u3002\u901a\u8fc7\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u673a\u5668\u4eba\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u8d2d\u7269\u8005\u7684\u95ee\u9898\uff0c\u5e76\u4e3a\u540e\u7eed\u7684\u68c0\u7d22\u63d0\u4f9b\u51c6\u786e\u7684\u4e0a\u4e0b\u6587\u3002<\/li>\n\n\n\n
- \u68c0\u7d22\uff1a\u5728\u68c0\u7d22\u9636\u6bb5\uff0c\u804a\u5929\u673a\u5668\u4eba\u6839\u636e\u8d2d\u7269\u8005\u8868\u8fbe\u7684\u5174\u8da3\u9879\uff0c\u641c\u7d22Redis\u6570\u636e\u5e93\uff0c\u4ee5\u83b7\u53d6\u6700\u4f73\u7684\u53ef\u7528\u4ea7\u54c1\u3002\u901a\u8fc7\u4f7f\u7528Redis Vector Similarity Search\uff08VSS\uff09\u7b49\u6280\u672f\uff0c\u673a\u5668\u4eba\u80fd\u591f\u5feb\u901f\u800c\u51c6\u786e\u5730\u68c0\u7d22\u4e0e\u8d2d\u7269\u8005\u9700\u6c42\u76f8\u5339\u914d\u7684\u4ea7\u54c1\u3002<\/li>\n\n\n\n
- \u95ee\u9898\u56de\u7b54\uff1a\u5728\u8fd9\u4e2a\u9636\u6bb5\uff0c\u804a\u5929\u673a\u5668\u4eba\u4ece\u5411\u91cf\u641c\u7d22\u7684\u67e5\u8be2\u7ed3\u679c\u4e2d\u83b7\u53d6\u4ea7\u54c1\u4fe1\u606f\uff0c\u5e76\u5229\u7528OpenAI GPT\u6a21\u578b\u5e2e\u52a9\u8d2d\u7269\u8005\u6d4f\u89c8\u9009\u9879\u3002\u673a\u5668\u4eba\u53ef\u4ee5\u751f\u6210\u9002\u5f53\u7684\u56de\u7b54\uff0c\u63d0\u4f9b\u6709\u5173\u4ea7\u54c1\u7279\u5f81\u3001\u4ef7\u683c\u3001\u8bc4\u4ef7\u7b49\u65b9\u9762\u7684\u4fe1\u606f\uff0c\u4ee5\u5e2e\u52a9\u8d2d\u7269\u8005\u505a\u51fa\u51b3\u7b56\u3002<\/li>\n<\/ol>\n\n\n\n
3\u3001\u867d\u7136LangChain\u548cRedis\u6781\u5927\u5730\u63d0\u5347\u4e86\u5de5\u4f5c\u6d41\u7a0b\u7684\u6548\u7387\uff0c\u4f46\u4e0e\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08\u5982GPT\uff09\u8fdb\u884c\u4ea4\u4e92\u65f6\u9700\u8981\u4f7f\u7528\u201d\u63d0\u793a\uff08prompt\uff09\u201d\u6765\u8fdb\u884c\u6c9f\u901a\u3002\u6211\u4eec\u521b\u9020\u51fa\u4e00\u7ec4\u6307\u4ee4\u4f5c\u4e3a\u63d0\u793a\uff0c\u4ee5\u5f15\u5bfc\u6a21\u578b\u7684\u884c\u4e3a\u671d\u7740\u671f\u671b\u7684\u7ed3\u679c\u53d1\u5c55\u3002\u4e3a\u4e86\u83b7\u5f97\u804a\u5929\u673a\u5668\u4eba\u7684\u6700\u4f73\u6548\u679c\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u5b8c\u5584\u63d0\u793a\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n
template = \u201c\u201d\u201dGiven the following chat history and a follow up question, rephrase the follow up input question to be a standalone question.<\/p>\n\n\n\n
Or end the conversation if it seems like it\u2019s done.<\/p>\n\n\n\n
Chat History:\\\u201d\u201d\u201d<\/p>\n\n\n\n
{chat_history}<\/p>\n\n\n\n
\\\u201d\u201d\u201d<\/p>\n\n\n\n
Follow Up Input: \\\u201d\u201d\u201d<\/p>\n\n\n\n
{question}<\/p>\n\n\n\n
\\\u201d\u201d\u201d<\/p>\n\n\n\n
Standalone question:\u201d\u201d\u201d<\/p>\n\n\n\n
condense_question_prompt = PromptTemplate.from_template(template)<\/p>\n\n\n\n
template = \u201c\u201d\u201dYou 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.<\/p>\n\n\n\n
It\u2019s ok if you don\u2019t know the answer.<\/p>\n\n\n\n
Context:\\\u201d\u201d\u201d<\/p>\n\n\n\n
{context}<\/p>\n\n\n\n
\\\u201d\u201d\u201d<\/p>\n\n\n\n
Question:\\\u201d<\/p>\n\n\n\n
\\\u201d\u201d\u201d<\/p>\n\n\n\n
Helpful Answer:\u201d\u201d\u201d<\/p>\n\n\n\n
qa_prompt= PromptTemplate.from_template(template)<\/p>\n\n\n\n
4\u3001\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e24\u4e2aOpenAI LLM\uff0c\u5e76\u5206\u522b\u4f7f\u7528\u94fe\u6761\u5bf9\u5176\u8fdb\u884c\u5c01\u88c5\uff0c\u7528\u4e8e\u95ee\u9898\u751f\u6210\u548c\u95ee\u9898\u56de\u7b54\u3002streaming_llm\u5141\u8bb8\u6211\u4eec\u9010\u4e2a\u6807\u8bb0\u5730\u5c06\u804a\u5929\u673a\u5668\u4eba\u7684\u54cd\u5e94\u4f20\u8f93\u5230stdout\uff0c\u4ece\u800c\u4e3a\u7528\u6237\u63d0\u4f9b\u7c7b\u4f3c\u4e8e\u804a\u5929\u673a\u5668\u4eba\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n\n\n\n
# define two LLM models from OpenAI<\/p>\n\n\n\n
llm = OpenAI(temperature=0)<\/p>\n\n\n\n
streaming_llm = OpenAI(<\/p>\n\n\n\n
streaming=True,<\/p>\n\n\n\n
callback_manager=CallbackManager([<\/p>\n\n\n\n
StreamingStdOutCallbackHandler()<\/p>\n\n\n\n
]),<\/p>\n\n\n\n
verbose=True,<\/p>\n\n\n\n
max_tokens=150,<\/p>\n\n\n\n
temperature=0.2<\/p>\n\n\n\n
)<\/p>\n\n\n\n
# use the LLM Chain to create a question creation chain<\/p>\n\n\n\n
question_generator = LLMChain(<\/p>\n\n\n\n
llm=llm,<\/p>\n\n\n\n
prompt=condense_question_prompt<\/p>\n\n\n\n
)<\/p>\n\n\n\n
# use the streaming LLM to create a question answering chain<\/p>\n\n\n\n
doc_chain = load_qa_chain(<\/p>\n\n\n\n
llm=streaming_llm,<\/p>\n\n\n\n
chain_type=\u201dstuff\u201d,<\/p>\n\n\n\n
prompt=qa_prompt<\/p>\n\n\n\n
)<\/p>\n\n\n\n
5\u3001\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528ConversationalRetrievalsChain\u5c06\u6240\u6709\u4e09\u4e2a\u6b65\u9aa4\u5c01\u88c5\u8d77\u6765\u3002<\/p>\n\n\n\n
chatbot = ConversationalRetrievalsChain(<\/p>\n\n\n\n
retriever=vectorstore.as_retriever(),<\/p>\n\n\n\n
combine_docs_chain=doc_chain,<\/p>\n\n\n\n
question_generator=question_generator<\/p>\n\n\n\n
)<\/p>\n\n\n\n
\u56db\u3001\u865a\u62df\u8d2d\u7269\u52a9\u624b\u5df2\u5c31\u7eea<\/h2>\n\n\n\n
1\u3001\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u5168\u80fd\u7684\u804a\u5929AI\u3002\u7136\u800c\uff0c\u901a\u8fc7Redis\u7684\u5e2e\u52a9\uff0c\u5b83\u5b58\u50a8\u4e86\u5b8c\u6574\u7684\u4ea7\u54c1\u5e93\u77e5\u8bc6\uff0c\u6211\u4eec\u80fd\u591f\u6253\u9020\u51fa\u4e00\u4e2a\u76f8\u5f53\u51fa\u8272\u7684\u4f53\u9a8c\u3002<\/p>\n\n\n\n
# create a chat history buffer<\/p>\n\n\n\n
chat_history = []<\/p>\n\n\n\n
# gather user input for the first question to kick off the bot<\/p>\n\n\n\n
question = input(\u201cHi! What are you looking for today?\u201d)<\/p>\n\n\n\n
# keep the bot running in a loop to simulate a conversation<\/p>\n\n\n\n
while True:<\/p>\n\n\n\n
result = chatbot(<\/p>\n\n\n\n
{\u201cquestion\u201d: question, \u201cchat_history\u201d: chat_history}<\/p>\n\n\n\n
)<\/p>\n\n\n\n
print(\u201c\\n\u201d)<\/p>\n\n\n\n
chat_history.append((result[\u201cquestion\u201d], result[\u201canswer\u201d]))<\/p>\n\n\n\n
question = input()<\/p>\n\n\n\n
2\u3001\u8be5\u673a\u5668\u4eba\u5c06\u5b9e\u65f6\u4e0e\u60a8\u4ea4\u4e92\uff0c\u5e76\u6839\u636e\u76ee\u5f55\u4e2d\u7684\u5546\u54c1\u5e2e\u52a9\u60a8\u7f29\u5c0f\u9009\u62e9\u8303\u56f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n
Hi! What are you looking for today?<\/p>\n\n\n\n
>> gold-plated earrings<\/p>\n\n\n\n
Hi there! I\u2019m 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?<\/p>\n\n\n\n
>> My preference is the yellow gold plated sterling silver<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
3\u3001\u5728\u804a\u5929\u673a\u5668\u4eba\u7528\u53d1\u51fa\u201c\u4f60\u597d\uff01\u4eca\u5929\u4f60\u5728\u627e\u4ec0\u4e48\uff1f\u201d\u6b64\u7c7b\u7684\u62db\u547c\u540e\uff0c\u5c1d\u8bd5\u4e00\u4e9b\u793a\u4f8b\u63d0\u793a\uff0c\u6216\u8005\u81ea\u5df1\u521b\u5efa\u4e00\u4e2a\u3002<\/p>\n\n\n\n
\u4e94\u3001\u5b9a\u5236\u60a8\u7684\u94fe\u6761\u4ee5\u63d0\u9ad8\u6027\u80fd<\/h2>\n\n\n\n
1\u3001LangChain\u6700\u597d\u7684\u90e8\u5206\u4e4b\u4e00\u662f\u6bcf\u4e2a\u7c7b\u62bd\u8c61\u90fd\u53ef\u4ee5\u6269\u5c55\u6216\u521b\u5efa\u81ea\u5df1\u7684\u9884\u8bbe\u3002\u6211\u4eec\u81ea\u5b9a\u4e49BaseRetriever\u7c7b\uff0c\u5728\u8fd4\u56de\u7ed3\u679c\u4e4b\u524d\u6267\u884c\u4e00\u4e9b\u6587\u6863\u9884\u5904\u7406\u3002<\/p>\n\n\n\n
import json<\/p>\n\n\n\n
from langchain.schema import BaseRetriever<\/p>\n\n\n\n
from langchain.vectorstores import VectorStore<\/p>\n\n\n\n
from langchain.schema import Document<\/p>\n\n\n\n
from pydantic import BaseModel<\/p>\n\n\n\n
class RedisProductRetriever(BaseRetriever, BaseModel):<\/p>\n\n\n\n
vectorstore: VectorStore<\/p>\n\n\n\n
class Config:<\/p>\n\n\n\n
arbitrary_types_allowed = True<\/p>\n\n\n\n
def combine_metadata(self, doc) -> str:<\/p>\n\n\n\n
metadata = doc.metadata<\/p>\n\n\n\n
return (<\/p>\n\n\n\n
\u201cItem Name: \u201d + metadata[\u201citem_name\u201d] + \u201c. \u201d +<\/p>\n\n\n\n
\u201cItem Description: \u201d + metadata[\u201cbullet_point\u201d] + \u201c. \u201d +<\/p>\n\n\n\n
\u201cItem Keywords: \u201d + metadata[\u201citem_keywords\u201d] + \u201c.\u201d<\/p>\n\n\n\n
)<\/p>\n\n\n\n
def get_relevant_documents(self, query):<\/p>\n\n\n\n
docs = []<\/p>\n\n\n\n
for doc in self.vectorstore.similarity_search(query):<\/p>\n\n\n\n
content = self.combine_metadata(doc)<\/p>\n\n\n\n
docs.append(Document(<\/p>\n\n\n\n
page_content=content,<\/p>\n\n\n\n
metadata=doc.metadata<\/p>\n\n\n\n
))<\/p>\n\n\n\n
return docs<\/p>\n\n\n\n
2\u3001\u6211\u4eec\u9700\u8981\u66f4\u65b0\u68c0\u7d22\u7c7b\u548c\u804a\u5929\u673a\u5668\u4eba\uff0c\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u7684\u81ea\u5b9a\u4e49\u5b9e\u73b0\u3002<\/p>\n\n\n\n
redis_product_retriever = RedisProductRetriever(vectorstore=vectorstore)<\/p>\n\n\n\n
chatbot = ConversationalRetrievalsChain(<\/p>\n\n\n\n
retriever=redis_product_retriever,<\/p>\n\n\n\n
combine_docs_chain=doc_chain,<\/p>\n\n\n\n
question_generator=question_generator<\/p>\n\n\n\n
)<\/p>\n\n\n\n
3\u3001\u5927\u529f\u544a\u6210\uff01\u73b0\u5728\u4f60\u7684\u804a\u5929\u673a\u5668\u4eba\u53ef\u4ee5\u5728\u5bf9\u8bdd\u4e2d\u6ce8\u5165\u66f4\u591a\u7684\u4ea7\u54c1\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u53e6\u4e00\u4e2a\u77ed\u5bf9\u8bdd\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n
Hi! What are you looking for today?<\/p>\n\n\n\n
>>> fancy footwear for going out<\/p>\n\n\n\n
Hi there! We have a few great options for women\u2019s shoes and sandals. We have the Amazon Brand \u2013 The Fix Women\u2019s Giana Open Toe Bootie with Pearl Buckle, bright white leather, 9.5 B US, Flavia Women\u2019s Beige Fashion Sandals-7 UK (39 EU) (8 US) (FL\/236\/BEG), Flavia Women\u2019s Blue Fashion Sandals-8 UK (40 EU) (9 US) (FL\/211\/BLU), and The Fix Women\u2019s 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!<\/p>\n\n\n\n
>>> These are nice. However, I am looking for men\u2019s shoes. Can you help me?<\/p>\n\n\n\n
Hi there! We have a great selection of men\u2019s formal shoes available. We have Amazon Brand \u2013 Symbol Men\u2019s Formal Shoes, Amazon Brand \u2013 Symbol Men\u2019s 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\u2019s 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!<\/p>\n\n\n\n
>>>Can you show me some more men\u2019s options?<\/p>\n\n\n\n
Hi there! I\u2019m here to help you find the perfect item for you. We have a few options available for men\u2019s formal shoes. We have the Men\u2019s Stainless Steel Link Bracelet, the Amazon Brand \u2013 Arthur Harvey Men\u2019s Leather Formal Shoes, and the Amazon Brand \u2013 Symbol Men\u2019s 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\u2019m happy to help!<\/p>\n\n\n\n
>>> Ok this looks great, thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"
OpenAI\u7684\u8bed\u8a00\u6a21\u578b\u5e76\u6ca1\u6709\u9488\u5bf9\u7279\u5b9a\u4f01\u4e1a\u7684\u5177\u4f53\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\u6216\u4f18\u5316\u3002\u5982\u679c\u60a8\u7684\u804a\u5929\u673a\u5668\u4eba\u4f9d\u8d56\u4e8e\u8be5\u6846\u67b6\uff0c\u60a8\u9700\u8981\u5728\u8fd0\u884c\u65f6\u5411OpenAI\u63d0\u4f9b\u6570\u636e\u3002\u5728\u68c0\u7d22\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5411\u91cf\u76f8\u4f3c\u6027\u641c\u7d22\uff08VSS\uff09\u4eceRedis\u4e2d\u83b7\u53d6\u4e0e\u7528\u6237\u67e5\u8be2\u76f8\u5173\u7684\u6570\u636e\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6570\u636e\u4e0e\u539f\u59cb\u95ee\u9898\u4e00\u8d77\u8f93\u5165\u5230\u8bed\u8a00\u6a21\u578b\u4e2d\u3002\u8fd9\u4e2a\u6d41\u6c34\u7ebf\u4e2d\u5927\u90e8\u5206\u7684\u590d\u6742\u6027\u90fd\u5f52\u7ed3\u4e8e\u68c0\u7d22\u6b65\u9aa4\u3002\u56e0\u6b64\u6211\u4eec\u5c06LangChain\u4e0eRedis Enterprise\u7ed3\u5408\uff0c\u4f5c\u4e3a\u5927\u8bed\u8a00\u6a21\u578b\u7684\u5411\u91cf\u6570\u636e\u5e93\u3002<\/p>\n","protected":false},"author":1,"featured_media":28523,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[583],"tags":[604],"aioseseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/posts\/28511"}],"collection":[{"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/comments?post=28511"}],"version-history":[{"count":6,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/posts\/28511\/revisions"}],"predecessor-version":[{"id":42084,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/posts\/28511\/revisions\/42084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aqycmy.com\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/media?parent=28511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/categories?post=28511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqycmy.com\/wp-json\/wp\/v2\/tags?post=28511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}