Spaces:
Sleeping
Sleeping
File size: 8,909 Bytes
733698e 40275ce 733698e 40275ce 733698e 40275ce 733698e 40275ce 733698e 40275ce 733698e 40275ce 733698e 40275ce 733698e c74110e 9c874b1 733698e c74110e 733698e 6a65060 733698e 4351f00 733698e 01fe84d 733698e 01fe84d 733698e 01fe84d 733698e 01fe84d 733698e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
import gradio as gr
import openai
import os
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
catagory_list = [
"停车服务", "会员积分及兑换商品", "活动报名",
"投诉建议"]
members ="""
会员姓名:"David"
会员等级:"黄金会员"
积分:"10000"
会员姓名:"Liao"
会员等级:"白金会员"
积分:"20000"
"""
Events = """
活动1
活动名称:"国庆节狂欢"
日期:“2023-10-1 到 2023-10-7”
活动内容:“会员和家庭小朋友可以参加报名,与可爱的迪士尼米奇一起跳舞和唱歌。”
活动对象: “黄金会员、白金会员”
需要抵扣积分: “5000”
活动2
活动名称:“圣诞节玩雪”
日期:“2023-12-23 到 2023-12-25”
活动内容: “会员和家庭小朋友可以参加报名,可以在白雪公主城堡玩雪。”
活动对象: ”黄金会员、白金会员“
需要抵扣积分: “2000”
"""
gifts ="""
礼品名称: “华为mate 60 Pro”
兑换积分: “10000”
礼品名称: “小米mixed fold 3”
兑换积分: “5000”
"""
parking ="""
政策:会员可以通过积分抵扣免费停车。
车牌号码:“粤A001”
已停时间:“60分钟”
需要积分:“600”
车牌号码:“粤A002”
已停时间:“100分钟”
需要积分:“1000”
"""
instructions = {
"停车服务": f"根据停车信息回答客户问题。\
1.如果客户想缴停车费,问客户车牌号码。\
2.如果客户提供了车牌号码从停车信息找到相关信息。\
3.如果找不到客户的停车信息,请让客户联系人工客服。\
4.如果找到客户的停车信息,告诉客户的车停了多久,需要多少积分抵扣停车费。\
5.如果找到客户的停车信息,问客户是否需要用积分抵扣停车费,请问客户的名字并从会员信息了解情况。\
6.如果客户有足够积分抵扣,请为客户用积分抵扣停车费并与客户确认。\
7.如果客户不够积分抵扣,请告诉客户自行缴费。\
8.如果客户用积分抵扣了停车费,告诉客户扣减活动积分后还有多少积分。\
停车信息以'''符号区分开。\
会员信息以'''符号区分开。\
停车信息:'''{parking}'''\
会员信息:'''{members}''' ",
"会员积分及兑换商品": "",
"活动报名": f"根据活动清单为客户解答关于活动的问题。\
根据活动清单,介绍活动内容及推介客户参加活动。\
如果客户想报名活动,请问客户的名字并从会员信息了解情况,判断客户有没有参加活动的资格。\
活动有会员等级及积分限定, 只有符合活动对象条件的会员可以参加。另外活动需要扣减积分,如果会员不够积分也无法参加。\
如果客户符合资格,请为客户报名。如果客户不够资格,请礼貌地回复不符合条件参加,并提醒客户可以通过消费获取积分达到参加条件。\
最后与客户确认报名信息并告诉客户扣减活动积分后还有多少积分。\
活动清单以'''符号区分开。\
会员信息以#符号区分开。\
活动清单:''' {Events}'''\
会员信息:#{members}#\
请确保按提供的信息回复问题,如果没有信息请让客户联系人工客户。",
"投诉建议":"礼貌地回复客户他的问题已记录,将会转交人工客服处理并尽快回复",
"其它": "礼貌地回复客户他的问题已记录,将会转交人工客服处理并尽快回复"}
def get_completion_from_messages(messages,
temperature=0,
model="gpt-4",
max_tokens=8000):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
return response.choices[0].message["content"]
def format_chat(message, chat_history):
prompt = []
for turn in chat_history:
user_message, bot_message = turn
prompt.append({'role': 'user', 'content': user_message})
prompt.append({'role': 'assistant', 'content': bot_message})
prompt.append({'role': 'user', 'content': message})
return prompt
def format_chat_trail(message, chat_history):
prompt = []
for turn in chat_history:
user_message, bot_message = turn
prompt.append({"user:", user_message})
prompt.append({"assistant:", bot_message})
prompt.append({"user:", message})
return prompt
def get_completion(prompt, model="gpt-4"):
messages = [{'role': 'user', 'content': prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def catagory(chat_history):
prompt =[]
instruction = f"""
根据与客户的对话记录判断客户需要服务清单里的什么服务。\
从客户的对话记录里客户如果有多个服务需要,请根据最后的服务需求从服务清单里判断最合适的服务。\
请确保只输出结果,不需要解释。输出的结果仅限于服务清单里的名称,例如:停车服务、会员积分及兑换商品、活动报名。\
如果清单里没有合适的服务输出“其它”。\
服务清单以'''符号区分开。\
服务清单:'''"停车服务", "会员积分及兑换商品", "活动报名", "投诉建议"'''\
客户的对话记录以'''符号区分开。\
客户的对话记录:'''{chat_history}'''\
如何判断合适的服务按以下说明:\
停车服务:停车或缴停车费相关的信息。\
会员积分及兑换商品:关于会员消费后可获得的积分及使用积分兑换商品的信息。\
活动报名:关于活动的信息及报名。\
投诉建议:客户不满意或有什么优化服务的建议。
"""
prompt.append({'role': 'system', 'content': instruction})
response = get_completion_from_messages(prompt)
return response
def ToServe(message, chat_history):
prompt = []
chat_trail = format_chat_trail(message,chat_history)
instruction = f"""
你是一个大型, 高端商场的客户服务AI,\
回答客户与商场活动、停车服务、会员权益相关的问题。\
如果问题与商场不相关,请礼貌回复客户无法处理。\
如果问题与商场相关,请以热情态度按以下方式为客户解答问题。\
回答客户方式以'''符号区分开。 \
回答客户方式: '''{instructions[catagory(chat_trail)]}'''\
按提供的信息回复客户的问题。如果没有相关信息请礼貌回复客户不清楚并会将问题反馈给人工客服处理。
如果客户的问题不清晰请让客户补充信息。\
如果客户可能在问某个问题但不确定,请不着急回答,先与客户确定问题。\
按以下举例回复:
<客户>:地球有多大?
<AI>:我只能回答商场相关问题。有什么可以帮到您吗?
<客户>:今天天气怎么样?
<AI>:我只能回答商场相关问题。有什么可以帮到您吗?
<客户>:有餐饮吗?
<AI>:我没有相关信息。请稍等!我们人工客服会跟进您的问题。
<客户>:消费100元有多少积分?
<AI>:我没有相关信息。请稍等!我们人工客服会跟进您的问题。
"""
prompt.append({'role': 'system', 'content': instruction})
prompt = prompt + format_chat(message, chat_history)
response = get_completion_from_messages(prompt)
return response
def respond(message, chat_history):
if message != "":
#prompt=[]
#formatted_chat = format_chat(message, chat_history)
#instruction = get_instruction(formatted_chat)
#prompt.append({'role': 'system', 'content': instruction })
#prompt.append(formatted_chat)
#bot_message = get_completion_from_messages(prompt)
bot_message = ToServe(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
with gr.Blocks() as demo:
chatbot = gr.Chatbot(height=400) #just to fit the notebook
msg = gr.Textbox(label="你的对话:")
btn = gr.Button("提交")
clear = gr.ClearButton(components=[msg, chatbot], value="清空对话框")
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
demo.queue().launch() |