Python代码:使用if语句处理Bing Chat API响应
以下是将match转换为if后的Python代码:
if 'item' in v:
item = v['item']
if 'result' in item:
result = item['result']
value = result.get('value')
if value == 'Throttled':
logger.error('<Bing账号到达今日请求上限>')
raise BingChatAccountReachLimitException('<Bing账号到达今日请求上限>')
elif value == 'Success':
if 'throttling' in item:
throttling = item['throttling']
num_conver = throttling.get('numUserMessagesInConversation')
max_conver = throttling.get('maxNumUserMessagesInConversation')
if num_conver > max_conver:
raise BingChatConversationReachLimitException(
f'<达到对话上限>
最大对话次数:{max_conver}
你的话次数:{num_conver}'
)
if 'messages' in item:
messages = item['messages']
for message in messages:
if 'hiddenText' in message:
hiddenText = message['hiddenText']
raise BingChatResponseException(f'<Bing检测到敏感问题,无法回答>
{hiddenText}')
elif 'text' in message:
text = message['text']
return v
logger.error('<未知的错误>')
raise BingChatResponseException('<未知的错误, 请管理员查看控制台>')
原文地址: https://www.cveoy.top/t/topic/lDOH 著作权归作者所有。请勿转载和采集!