当我在python终端中为每条消息运行main.py时,并没有创建新的会话id(旧用户)。但是,当我每次为新消息部署它的服务器时,即使是旧用户也会创建一个新的session_id。
@app.route('/send_message/<string:user_message>',methods=['GET'])
def send_mesage(user_message):
try:
# Invoke a method
# Create Assistant service object.
authenticator = IAMAuthenticator('IcBMhVvVCh94QWP0u2DAfrT17-rmI9zGE4XzEoFdPDKa') # replace with API key
assistant = AssistantV2(
version = '2020-09-24',
authenticator = authenticator
)
assistant.set_service_url('https://api.us-south.assistant.watson.cloud.ibm.com/instances/8244c6b7-6d94-422f-9292-b7408871d24b')
assistant_id = 'c9866a42-652d-4f3e-a75c-0d3e3281bc75' # replace with assistant ID
message_input = {
'message_type:': 'text',
'text': user_message
}
#Create Session For every new User.
session_id = assistant.create_session(assistant_id=assistant_id).get_result()
print(json.dumps(session_id, indent=2))
while message_input['text'] != 'quit':
response = assistant.message(
assistant_id=assistant_id,
session_id=session_id['session_id'],
input=message_input
).get_result()
-------------发布于 2021-04-30 05:32:29
我不得不在函数之外编写代码。
# Invoke a method
# Create Assistant service object.
authenticator = IAMAuthenticator('IcBMhVvVCh94QWP0u2DAfrT17-rmI9zGE4XzEoFdPDKa') # replace with API key
assistant = AssistantV2(
version = '2020-09-24',
authenticator = authenticator
)
assistant.set_service_url('https://api.us-south.assistant.watson.cloud.ibm.com/instances/8244c6b7-6d94-422f-9292-b7408871d24b')
assistant_id = 'c9866a42-652d-4f3e-a75c-0d3e3281bc75' # replace with assistant IDhttps://stackoverflow.com/questions/67324627
复制相似问题