Skip to content

Commit

Permalink
动态保存对话
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOFF-ive committed Apr 2, 2023
1 parent a6dcd11 commit a411ce9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ PAUSE
#### file
设置当前程序使用哪一个对话记录,如果不设置,则默认开启一个新的对话
#### save
保存当前的对话记录,通常不需要手动调用,因为对话在退出后会自动保存,保存路径为程序所在目录,文件名为当前日期,文件名可以手动修改
保存当前的对话记录,通常不需要手动调用,因为对话会自动保存,保存路径为当前用户的Documents\chat_logs目录中,文件名为当前日期,文件名可以手动修改
#### image
输入图片描述,返回图片的url,该图片和url不会自动保存,需要用户手动下载保存
#### audio
Expand Down
17 changes: 14 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def chat():
messages.append({"role": "user", "content": content})

try:
parseResult_stream(generateCompletion(messages)) if stream else parseResult(generateCompletion(messages))
res = generateCompletion(messages)
parseResult_stream(res) if stream else parseResult(res)
save()
except APIConnectionError:
Log.error("[APIConnectionError]:Connection timed out. Please check the network or try again later")
except InvalidRequestError:
Expand Down Expand Up @@ -116,6 +118,7 @@ def setFile():
def save():
global messages, currentFile
fileName = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".json" if currentFile == "" else currentFile
currentFile = fileName
generateCatalogue()
doc_path = os.path.join(os.path.expanduser("~"), "Documents")
chat_logs_path = os.path.join(doc_path, "chat_logs")
Expand Down Expand Up @@ -188,8 +191,7 @@ def generateCatalogue():
os.makedirs(chat_logs_path)


if __name__ == "__main__":
Log.info("Project Launch")
def run():
initOpenAI()
cli = ClIHandle()
cli.add("chat", chat)
Expand All @@ -203,3 +205,12 @@ def generateCatalogue():
if option == "quit":
break
cli.parse(option)


if __name__ == "__main__":
try:
Log.info("Project Launch")
run()
finally:
Log.info("Project Finish")
save()

0 comments on commit a411ce9

Please sign in to comment.