Skip to content

Commit

Permalink
web修复机器人没有记忆的Issue
Browse files Browse the repository at this point in the history
web提供连续对话的接口,每次调用都会返回连续对话结果
  • Loading branch information
NoobHumiliator committed Oct 7, 2023
1 parent a953414 commit 56c5237
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
6 changes: 3 additions & 3 deletions conf/keys.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fd": "xxxxxx",
"GtToken": "xxxxxx==",
"Cookie": "xxxxxx=="
"fd": "912379",
"GtToken": "R0VFAAYwMjFhYjY2Mzg4MDNjYTYyNTE2MTIxNDE1NDY0OWJkNGIyYWY2YThlYmQ5YzdkNzQzYTA4OTE5ZmVlY2Y0NWEwNDQxYzNiNTFmMjQ5MDU5NWFhZGY2NzM2ZWY3MDY4MDBhMDg5OWU5MDExMjM5ODA0ZGNlZDE2N2UzZTdmZGY5ZjE5NGJhODEzNGQ1ZDQ4MTJhMTdhYmZiOTRmNzZmMmE5OGE0MWMxOGI2ODYwNDQ2ZjZmNDNjODQ3MTNjZmUzZjlhYTM3YmYwMTlmNzA0NTNkYzU3ZGM2MTZmODJhMTkzM2NjMmM2YjQ3MjBlOTBhMWY0MGExODkxMThmZTQ3MDYwYTUwNGZhYjgzZGQwMzQ5YzFkNTJhZTMzMzlkN2Q5YmVlODU0ODhiZTQ2NThhNWQ0ODU5MjMyYTZmYmZjMTQ3ZWQ5ZTUxYjRiMjdlMTk1MDJkYzdlMmEwNjc5MjkyYWFlYTc2N2UzNWQ5ZTQ2M2VkOTkxNWM4M2MzMDYwM2IzZmUzNDk5MTQzMGUzMzZhZGYxZDEzNmQ1ZDI0NTk2Y2UxNDdmOWZiNGViNTlkOTYzOGViZDkyNGQ4NWZhMzc5YmM4MWE4NTkxNDNkYzBiYWI1MTA0Y2UxNWJkOWEzNDYwMTVjMDkzZDc5NjU1OTZkZjQ2NDk5M2E4ZDhhOTZhMTM2YTUxNWJiZWRmNzJlZDQ1OTkwOTY0ZTcwZWY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DBmMzRmYzhmZGI3MDBiYWVkNzRiNmQ3ZTZhZmY4OGVmN2RhOTcxMDQ3M2RmMjNmY2Y5N2ZlZDVhMDA5NGNjYWM5NGMwZDg4MmYxODFiNzlkZjI5NjM2NWM5M2ExYTRjMzllMjIxNjMzYjViNjAxZjgzNzQ5OTgwODNiMjFhNmU5MDk1ZmQ2ZWE4YzRkYzk1MGM0Nzk2OWZmZGFiNDU4MGZhYzM0NjVkMzUzNmExYmY2YjIwYjY3NTNkYjliNDVmODJlNzhkNGNlN2MwZTNm",
"Cookie": "di_c_mti=7b9cd2eb-2c67-666f-8a02-95de1e23fb58; d_d_app_ver=1.4.0; d_d_ci=f5be0039-49de-63a0-a017-4b790f7a2f91; ssoSessionId=3a95fd4d-ca36-4031-a0de-4cda9b7669ec; account_id=17461758422; aliyungf_tc=17e7336e55820c28c1241ca35e122e350e07de004dd41c8a4f76591fb5e71d6d; daas_st={%22sdk_ver%22:%221.3.9%22%2C%22status%22:%220%22}; appid=150b4dfebe; gt_local_id=bT4FmoamHE5EumB06WicQbqbfecs+hL96Z0VLLX+ihjd2a1UuNMIwA=="
}
47 changes: 47 additions & 0 deletions sparkdesk_web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,31 @@ def __create_chat(self, chat_name):
self.__chat_id = self.__generate_chat_id()
self.__set_name(chat_name)

#获取最近一次的sid用于延续对话,否则对话无记忆
def __get_chat_sid(self) -> str:
try:
response= self.__get_chat_history()
data = json.loads(response.text)
return data['data'][-1]["historyList"][-1]["sid"]
except:
return ''

def __get_chat_history(self):

url = f'https://xinghuo.xfyun.cn/iflygpt/u/chat_history/all/{self.__chat_id}'
response = requests.request("GET", url, headers=self.__request_header)
return response

def __get_resp(self, question):

sid = self.__get_chat_sid()
url = "https://xinghuo.xfyun.cn/iflygpt-chat/u/chat_message/chat"
payload = {
'fd': self.__fd,
'chatId': self.__chat_id,
'text': question,
'GtToken': self.__GtToken,
'sid': sid,
'clientType': '1'
}
response = requests.request("POST", url, headers=self.__cha_header, data=payload, stream=True)
Expand Down Expand Up @@ -144,3 +162,32 @@ def chat_stream(self, history: bool = False, history_path: str = './data/'):
if history:
FileUtils.write2json(history_file_path, self.__chat_history)
print("\nThank you for using the SparkDesk AI. Welcome to use it again!")

#提供连续对话
def create_continuous_chat(self):
#生成器包装,提供预激服务
class ContextGenWrapper:
def __init__(self, spark, gen):
self._gen = gen
self._spark = spark
#第一次构建生成器 进行预激
next(gen)
def chat(self, question):
result = self._gen.send(question)
#将生成器执行到接受question的位置
self._gen.send(None)
return result
return ContextGenWrapper(self, self.continuous_chat_gen())

#连续对话生成器
def continuous_chat_gen(self):
self.__create_chat(NEW_CHAT)
count = 0
while True:
count += 1
question = yield
resp = self.__streaming_output(question)
# If False, regenerate the chat
if resp[1] is False:
print("WARNING: 可能触发敏感词监控,对话已被重置,请前往Web页面更新Cookie、fd、GtToken!")
yield resp[0]
7 changes: 6 additions & 1 deletion sparkdesk_web_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
# single chat
# print(sparkWeb.chat("repeat: hello world"))

# stream input chat
#sparkWeb.chat_stream(history=True)

# continue chat
sparkWeb.chat_stream(history=True)
chat = sparkWeb.create_continuous_chat()
print(chat.chat("请介绍一下西安"))
print(chat.chat("刚才你说的是哪个城市?"))

0 comments on commit 56c5237

Please sign in to comment.