feat: session基本功能及单测

This commit is contained in:
Rock Chin 2022-12-07 22:50:49 +08:00
parent 4303487d32
commit 908cb1634b
3 changed files with 20 additions and 1 deletions

View File

@ -24,3 +24,5 @@ completion_api_params = {
"frequency_penalty": 0.4,
"presence_penalty": 0.3,
}
session_expire_time = 60 * 60 * 24 * 7

View File

@ -35,6 +35,7 @@ class Session:
# 向API请求补全
response = pkg.openai.manager.get_inst().request_completion(self.prompt, self.user_name+':')
# print(response)
# 处理回复
res_test = response["choices"][0]["text"]
res_ans = res_test
@ -45,7 +46,7 @@ class Session:
del (res_ans_spt[0])
res_ans = '\n\n'.join(res_ans_spt)
self.prompt += "\n" + self.bot_name + ":{}".format(res_ans)
self.prompt += "{}".format(res_ans) + '\n'
return res_ans
def persistence(self):

View File

@ -0,0 +1,16 @@
import config
import unittest
import pkg.openai.session
import pkg.openai.manager
class TestOpenAISession(unittest.TestCase):
def test_session_console(self):
interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'], config.completion_api_params)
session = pkg.openai.session.Session('test')
print(session.append('你好'))
print("#{}#".format(session.prompt))
print(session.append('你叫什么名字'))
print("#{}#".format(session.prompt))