AI对话里常常需要用到上下文场景,而之前所做的客服里,每次的请求都是独立的,也就是没有提供上下文内容,这样用户每次对话,相当于独立的问答,很不友好。 有没有个方法可以解决这个问题? 我想到用Redis来实现,主要理由有两点: 一是Redis是基于内存的数据存储,相比较传统数据库,速度非常快; 二是Redis的存储读取非常方便,无需像传统数据库那般比较繁琐。 关于AI的上下文格式, lemooljiang之前写过一篇文章[《ChatGPT如何获取上下文》](https://blog.larkneer.com/trend/@lemooljiang/twr9x3bp),有提到如下格式, >messages = [ { 'role': 'user', 'content': '你好。 今天是多云天气' }, { 'role': 'assistant', 'content': '你好。 我很抱歉听到不幸的天气' }, { 'role': 'user', 'content': '是的,是这样。 不过我很好。' }, { 'role': 'assistant', 'content': '我希望如此。 让我们继续今天的工作吧!' }, { 'role': 'user', 'content': '是的。 哦,顺便问一下,我是怎么说今天的天气的?' }, { 'role': 'assistant', 'content': '今天是阴天' } ] 于是我就根据自己的需求,来最后生成如上所需要的格式数据。 思考了下,除了AI必须得两个参数:role(角色)和 content(对话内容)之外,实际需要的参数还包括,企业ID,用户ID以及时间戳。 那么定义如下: corp_id(企业ID) user_id(用户ID) timestamp(时间戳) 准备用三个函数来实现我的需求,包括 存储对话、查询对话以及删除相关对话。 当然,删除相关对话主要是方便我测试,实际中比较少用。 首先来完成存储对话,代码实现如下, def store_chat(self, corp_id, user_id, role, content, timestamp): # 构建存储键名 chat_key = f"chat:{corp_id}:{user_id}" # 构建存储值 chat_data = { "role": role, "content": content } # 将聊天内容存储到Redis中 self.redis_client.lpush(chat_key, json.dumps(chat_data)) <p> 我用"chat:{corp_id}:{user_id}"当做ID作为key键,存储该企业用户的所有聊天记录。 存储的内容格式用数据JSON来完成,符合AI上下文messages的要求。 最后存储用lpush来实现存储。 查询对话时,需要传入对话的ID,代码实现如下, def get_chats(self, corp_id, user_id): # 构建查询键名 chat_key = f"chat:{corp_id}:{user_id}" # 获取聊天记录 chat_records = self.redis_client.lrange(chat_key, 0, -1) # 将记录反序列化为字典对象 sorted_chats = [json.loads(chat) for chat in chat_records] # 按照时间戳顺序排序 # sorted_chats = sorted(sorted_chats, key=lambda x: x['timestamp']) return sorted_chats 其中"corp_id, user_id"前边提前,用于做数据记录的ID存储,这里查询也同样用途。 删除对话 这个就很简单了,代码实现如下, def delete_chats(self, corp_id, user_id): # 构建删除键名 chat_key = f"chat:{corp_id}:{user_id}" # 删除相关聊天记录 self.redis_client.delete(chat_key) 最后封装成Class对象,调用起来就方便了。 完整的代码实现,如下图,   哦,为了防止上下文超过长度,我单独写了个函数,防止判断上下文聊天记录超过一定长度,  模拟添加一些数据,试下效果如何? 测试代码如下, >chat_storage = ChatStorageRedis() > >chat_storage.store_chat('corp_id_1', 'example_userid', 'assistant', 'Hello, how can I help you?', '2022-01-01 12:00:00') > >chat_storage.store_chat('corp_id_1', 'example_userid', 'user', 'I have a question about our product.', '2022-01-01 12:05:00') > >chat_storage.store_chat('corp_id_1', 'example_userid', 'assistant', 'are you OK?', '2022-01-01 12:06:00') >chat_storage.store_chat('corp_id_1', 'example_userid', 'user', 'Yes!', '2022-01-01 12:07:00') >chat_storage.store_chat('corp_id_1', 'example_userid', 'assistant', 'aaaaaaaaa', '2022-01-01 12:07:15') >chat_storage.store_chat('corp_id_1', 'example_userid', 'user', 'bbbbbbbb!', '2022-01-01 12:07:30') >chat_storage.store_chat('corp_id_1', 'example_userid', 'assistant', 'CCCCCCCCCCCCCCCCCCCCCCC', '2022-01-01 12:08:15') > >chat_storage.store_chat('corp_id_1', 'example_userid', 'user', 'DDDDDDDDDDDDDDDDDD!', '2022-01-01 12:08:30') > >chat_storage.store_chat('corp_id_1', 'example_userid', 'assistant', 'EEEEEEEEEEEEEEEEEEEEEEEE', '2022-01-01 12:09:15') > > >chat_storage.store_chat('corp_id_1', 'example_userid', 'user', 'FFFFFFFFFF!', '2022-01-01 12:09:30') chats = chat_storage.get_chats('corp_id_1', 'example_userid') > >content = getContent(chats,200) >print("query content:"+ content) >print("完整的记录如下:"+str(chats)) > >chat_storage.delete_chats('corp_id_1', 'example_userid')  控制台打印测试结果如下,  NICE! 果然是我想要的数据格式了。:)  Image by <a href="https://pixabay.com/users/aidansemmens-5096137/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=8281303">Aidan Semmens</a> from <a href="https://pixabay.com//?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=8281303">Pixabay</a>
author | rivalhw |
---|---|
permlink | redis-ai |
category | hive-105017 |
json_metadata | {"tags":["ai","redis","db"],"image":["https://images.hive.blog/DQmQC4XFgxkmtMGBu63naacNuLuRt74Jvc8pZ4uVLKandt6/gannet-8281303_1280.jpg","https://images.hive.blog/DQmXbaeidQREbZmmpqfzM2Ryegq1PajhBCQBdEBdmaa2TmQ/image.png","https://images.hive.blog/DQmZn52EZev9meTJtUPNVLcM8MtxeL64YoKutrKosMEhfpT/image.png","https://images.hive.blog/DQmcRse4A5JuLWDyWvHqddX6WPkXtC21i52rGEU6YntTnA4/image.png","https://images.hive.blog/DQmQm7f7jMugp6o4aHbVTkPXD1ea94PP8wXqqBrbC6iiZKu/image.png","https://images.hive.blog/DQmR1AUE18yrbX5HeD6eVMA7tLPQHkoiEQu5HzQacNkweEn/image.png"],"links":["https://blog.larkneer.com/trend/@lemooljiang/twr9x3bp"],"app":"hiveblog/0.1","format":"markdown"} |
created | 2023-10-09 02:40:42 |
last_update | 2023-10-09 02:43:21 |
depth | 0 |
children | 3 |
last_payout | 2023-10-16 02:40:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 9.985 HBD |
curator_payout_value | 9.963 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,541 |
author_reputation | 1,799,049,410,109,424 |
root_title | 用Redis实现AI上下文所需要的格式数据 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 0 |
post_id | 127,826,727 |
net_rshares | 48,823,040,581,294 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
abit | 0 | 19,578,328,387,459 | 75% | ||
gerber | 0 | 19,471,028,557 | 6% | ||
daan | 0 | 58,742,251,720 | 8% | ||
ezzy | 0 | 951,586,253 | 6% | ||
deanliu | 0 | 3,717,971,855,994 | 100% | ||
exyle | 0 | 56,153,126,593 | 6% | ||
joythewanderer | 0 | 22,895,990,996 | 100% | ||
lemooljiang | 0 | 19,153,181,522 | 100% | ||
ace108 | 0 | 1,268,097,078,127 | 26% | ||
jamesbrown | 0 | 1,039,213,157,029 | 100% | ||
magicmonk | 0 | 4,575,008,600,663 | 100% | ||
laoyao | 0 | 45,465,635,118 | 75% | ||
midnightoil | 0 | 160,469,765,739 | 75% | ||
xiaohui | 0 | 14,194,780,600 | 75% | ||
oflyhigh | 0 | 3,673,265,805,706 | 75% | ||
rivalhw | 0 | 2,099,442,238,728 | 100% | ||
nextgen622 | 0 | 184,939,671,604 | 50% | ||
helene | 0 | 1,019,420,657,213 | 75% | ||
netaterra | 0 | 163,997,044,247 | 10% | ||
someguy123 | 0 | 122,798,141,968 | 6% | ||
penguinpablo | 0 | 211,022,139,581 | 14% | ||
cnfund | 0 | 103,457,939,587 | 50% | ||
funnyman | 0 | 1,427,589,903 | 5.6% | ||
btshuang | 0 | 746,785,498 | 50% | ||
dapeng | 0 | 96,914,691,020 | 100% | ||
azazqwe | 0 | 87,713,257,130 | 100% | ||
lucknie | 0 | 134,967,566 | 100% | ||
dumping | 0 | 102,448,831 | 100% | ||
bxt | 0 | 242,860,265,339 | 75% | ||
privex | 0 | 7,428,386,371 | 12% | ||
laodr | 0 | 3,682,516,189 | 100% | ||
passion-fruit | 0 | 9,621,062,150 | 92% | ||
htliao | 0 | 15,658,233,326 | 100% | ||
fortune-master | 0 | 9,541,756,606 | 92% | ||
forykw | 0 | 18,100,889,553 | 3% | ||
cryptoemperor | 0 | 3,960,352,666 | 10% | ||
exec | 0 | 246,420,026,491 | 75% | ||
alphacore | 0 | 572,516,748 | 0.64% | ||
joeyarnoldvn | 0 | 580,881,058 | 1.76% | ||
susanli3769 | 0 | 865,167,871,011 | 100% | ||
mrpointp | 0 | 462,299,130,626 | 100% | ||
mygod | 0 | 743,197,974 | 50% | ||
shitsignals | 0 | 1,058,832,004 | 6% | ||
catwomanteresa | 0 | 472,911,961,239 | 100% | ||
liangfengyouren | 0 | 3,438,687,371 | 50% | ||
idx | 0 | 18,977,614,712 | 75% | ||
mangoanddaddy | 0 | 3,221,987,472 | 80% | ||
fr3eze | 0 | 1,166,810,167 | 100% | ||
aafeng | 0 | 900,834,390,363 | 100% | ||
cn-reader | 0 | 37,513,748,494 | 100% | ||
felander | 0 | 30,312,202,272 | 6% | ||
floatinglin | 0 | 6,580,434,510 | 100% | ||
mrspointm | 0 | 589,403,941,512 | 100% | ||
kimzwarch | 0 | 17,124,427,550 | 4% | ||
yogacoach | 0 | 1,317,992,998 | 6% | ||
yellowbird | 0 | 2,244,279,216 | 100% | ||
chenlocus | 0 | 85,196,802,549 | 100% | ||
deathwing | 0 | 170,447,768,148 | 6% | ||
blc | 0 | 4,308,396,226 | 100% | ||
minloulou | 0 | 5,381,955,114 | 70% | ||
hqy | 0 | 23,648,293,353 | 100% | ||
victory622 | 0 | 213,214,347,974 | 100% | ||
metten | 0 | 128,322,529 | 100% | ||
jychbetter | 0 | 657,889,807,728 | 100% | ||
winniex | 0 | 431,359,053,656 | 100% | ||
cn-book | 0 | 385,240,778 | 100% | ||
emrebeyler | 0 | 108,686,370,213 | 5.4% | ||
cn-movie | 0 | 182,844,478 | 100% | ||
etherpunk | 0 | 890,242,238 | 50% | ||
angelina6688 | 0 | 7,097,015,193 | 100% | ||
vivia | 0 | 696,963,533 | 100% | ||
cryptonized | 0 | 236,938,301 | 14% | ||
tryskele | 0 | 1,320,579,964 | 5% | ||
xiaoli | 0 | 438,914,054 | 100% | ||
unconditionalove | 0 | 765,312,611 | 3% | ||
nostalgic1212 | 0 | 550,077,271,607 | 100% | ||
ethanlee | 0 | 5,811,348,653 | 100% | ||
bestboom | 0 | 836,826,313 | 6% | ||
aellly | 0 | 285,897,577,176 | 100% | ||
meanbees | 0 | 24,928,050,392 | 100% | ||
pladozero | 0 | 33,817,411,856 | 10% | ||
nateaguila | 0 | 193,831,726,848 | 8% | ||
hmayak | 0 | 30,465,679,733 | 100% | ||
archisteem | 0 | 1,272,388,574 | 7.5% | ||
theluvbug | 0 | 1,773,391,159 | 25% | ||
moneybaby | 0 | 634,257,412 | 100% | ||
smartvote | 0 | 77,273,392,211 | 3.4% | ||
julian2013 | 0 | 69,747,388,159 | 100% | ||
dlike | 0 | 19,207,913,563 | 6% | ||
bobby.madagascar | 0 | 2,250,257,548 | 3% | ||
pet.society | 0 | 14,458,674,281 | 6% | ||
minminlou | 0 | 553,938,382 | 56% | ||
annepink | 0 | 801,002,415,679 | 100% | ||
cakemonster | 0 | 2,064,313,952 | 3% | ||
starrouge | 0 | 765,768,010 | 37.5% | ||
wherein | 0 | 41,373,495,338 | 75% | ||
zerofive | 0 | 635,366,024 | 37.5% | ||
jacuzzi | 0 | 3,391,366,709 | 1.4% | ||
samsemilia7 | 0 | 904,556,017 | 40% | ||
cnstm | 0 | 117,647,411,453 | 75% | ||
permaculturedude | 0 | 2,202,269,798 | 6% | ||
lianjingmedia | 0 | 721,361,196 | 75% | ||
hungrybear | 0 | 622,648,931 | 14% | ||
haikusailor | 0 | 461,946,986 | 6% | ||
kggymlife | 0 | 3,619,573,969 | 20% | ||
lovelemon | 0 | 76,762,164,223 | 25% | ||
epicdice | 0 | 23,538,512,110 | 20% | ||
dailyke20 | 0 | 2,549,575,949 | 100% | ||
isaaclim | 0 | 663,000,266 | 100% | ||
kgswallet | 0 | 509,615,129 | 10% | ||
triplea.bot | 0 | 1,281,463,715 | 6% | ||
steem.leo | 0 | 7,287,971,368 | 6% | ||
atyh | 0 | 225,354,385,690 | 100% | ||
gloriay | 0 | 1,191,686,321 | 100% | ||
electronico | 0 | 1,807,328,140 | 7% | ||
bilpcoinbot | 0 | 3,733,919,120 | 100% | ||
moleah | 0 | 658,527,557 | 100% | ||
dpoll.witness | 0 | 789,088,469 | 5.4% | ||
bilpcoin.pay | 0 | 539,922,117 | 10% | ||
lnakuma | 0 | 18,779,935,391 | 75% | ||
starnote | 0 | 321,415,317 | 100% | ||
bpcvoter3 | 0 | 896,772,575 | 50% | ||
ribary | 0 | 2,185,990,226 | 3% | ||
moochain.net | 0 | 323,793,350 | 100% | ||
warmstill | 0 | 805,896,226 | 50% | ||
mice-k | 0 | 521,963,042 | 6% | ||
lovequeen | 0 | 399,309,307,441 | 100% | ||
dcityrewards | 0 | 198,095,854,245 | 6% | ||
holoferncro | 0 | 2,248,424,841 | 5% | ||
cshin | 0 | 17,189,126,325 | 100% | ||
dcrops | 0 | 37,738,874,197 | 3% | ||
hykss.leo | 0 | 11,913,887,119 | 1.2% | ||
pokerarema | 0 | 398,016,193,893 | 100% | ||
yozen | 0 | 550,607,250 | 1.5% | ||
ausbit.dev | 0 | 1,200,577,064 | 6% | ||
drricksanchez | 0 | 51,864,242,618 | 10% | ||
ericaliu | 0 | 34,663,193,574 | 100% | ||
dengyanping888 | 0 | 109,697,082,502 | 100% | ||
twicejoy | 0 | 962,232,119 | 7% | ||
abundancelife | 0 | 199,884,066,946 | 100% | ||
mao317 | 0 | 8,171,294,346 | 100% | ||
celeste413 | 0 | 21,476,119,387 | 75% | ||
zhangyan-123 | 0 | 168,145,301,588 | 100% | ||
alpha-omega | 0 | 28,005,279,089 | 50.4% | ||
ilark | 0 | 229,105,391,859 | 100% | ||
drexlord | 0 | 2,501,792,574 | 5% | ||
leemah1 | 0 | 547,658,570 | 3.5% | ||
love5200 | 0 | 40,369,629,730 | 100% | ||
xiaoyaodidi | 0 | 67,485,559,351 | 100% | ||
kathyto | 0 | 65,468,177,490 | 100% | ||
biaojie | 0 | 31,251,595,422 | 100% | ||
daisy1999 | 0 | 11,906,827,944 | 100% |
大伟哥厉害了👍👍👍
author | alpha-omega |
---|---|
permlink | re-rivalhw-2023109t135854396z |
category | hive-105017 |
json_metadata | {"tags":["ai","redis","db"],"app":"ecency/3.0.36-vision","format":"markdown+html"} |
created | 2023-10-09 05:58:54 |
last_update | 2023-10-09 05:58:54 |
depth | 1 |
children | 0 |
last_payout | 2023-10-16 05:58:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 9 |
author_reputation | 423,597,520,629,025 |
root_title | 用Redis实现AI上下文所需要的格式数据 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 127,829,669 |
net_rshares | 0 |
看不懂,还是点个赞
author | gloriay |
---|---|
permlink | re-rivalhw-2023109t11050579z |
category | hive-105017 |
json_metadata | {"tags":["hive-105017","ai","redis","db"],"app":"ecency/3.0.43-mobile","format":"markdown+html"} |
created | 2023-10-09 03:00:51 |
last_update | 2023-10-09 03:00:51 |
depth | 1 |
children | 0 |
last_payout | 2023-10-16 03:00:51 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 9 |
author_reputation | 3,191,028,452,789 |
root_title | 用Redis实现AI上下文所需要的格式数据 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 127,826,969 |
net_rshares | 0 |
多面手,厉害!
author | hqy |
---|---|
permlink | s295fq |
category | hive-105017 |
json_metadata | {"app":"hiveblog/0.1"} |
created | 2023-10-09 08:11:54 |
last_update | 2023-10-09 08:11:54 |
depth | 1 |
children | 0 |
last_payout | 2023-10-16 08:11:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 7 |
author_reputation | 437,680,162,114,540 |
root_title | 用Redis实现AI上下文所需要的格式数据 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 127,831,939 |
net_rshares | 0 |