Skip to content

Commit

Permalink
优化交互
Browse files Browse the repository at this point in the history
  • Loading branch information
liufufa committed Mar 21, 2023
1 parent 72fa92a commit 27678c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions chatbotv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,20 @@ def ask_stream(
full_response += content
yield content
self.add_to_conversation(full_response, response_role, convo_id=convo_id)
print("[ask_stream]", full_response)

def ask(self, prompt: str, role: str = "user", convo_id: str = "default", **kwargs):
"""
Non-streaming ask
"""
print("[ask]", prompt)
response = self.ask_stream(
prompt=prompt,
role=role,
convo_id=convo_id,
**kwargs,
)
full_response: str = "".join(response)
print("[ask]", full_response)
print("[bot]", full_response)
return full_response

def rollback(self, n: int = 1, convo_id: str = "default"):
Expand Down
40 changes: 22 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
os.environ['GPT_ENGINE'] = 'gpt-3.5-turbo'
api_key = os.environ.get('API_KEY')
chatbot = Chatbot(api_key=api_key)
last_answer = ""
isQuerying = False
answerList=list()


app = Flask(__name__)
Expand Down Expand Up @@ -59,30 +58,35 @@ def wechat():
if 'text' == req.get('MsgType'):
# 获取用户的信息,开始构造返回数据
try:
global isQuerying
global last_answer
if not isQuerying and len(last_answer) != 0 and (req.get('Content') == '。'):
resp = {
'ToUserName':req.get('FromUserName'),
'FromUserName':req.get('ToUserName'),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content': last_answer
}
if req.get('Content') in ['。', '你好', 'hi']:
if len(answerList) == 0:
resp = {
'ToUserName':req.get('FromUserName'),
'FromUserName':req.get('ToUserName'),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content': '请提问,或者回复“。”(中文句号)等待回答...'
}
else:
resp = {
'ToUserName':req.get('FromUserName'),
'FromUserName':req.get('ToUserName'),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content': answerList.pop()
}
xml = xmltodict.unparse({'xml':resp})
return xml
else:
answer = chatbot.ask(req.get('Content'))
answerList.append(answer)
resp = {
'ToUserName':req.get('FromUserName'),
'FromUserName':req.get('ToUserName'),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content': "正在请求,稍后回复中文句号查询结果"
'Content': answer
}
if not isQuerying:
isQuerying = True
last_answer = chatbot.ask(req.get('Content'))
isQuerying = False
# 把构造的字典转换成xml格式
xml = xmltodict.unparse({'xml':resp})
return xml
Expand All @@ -92,7 +96,7 @@ def wechat():
'FromUserName':req.get('ToUserName'),
'CreateTime':int(time.time()),
'MsgType':'text',
'Content':'好像发生了点问题,请稍后再重新提问'+str(e)
'Content':'好像发生了点问题,请稍后再重新提问:'+str(e)
}
xml = xmltodict.unparse({'xml':resp})
return xml
Expand Down

0 comments on commit 27678c5

Please sign in to comment.