Messages、内容块与多模态:练习答案
练习解答
python
blocks=[{'type':'tool_use','id':'t1','name':'stock','input':{'sku':'A'}}]
text=''.join(b['text'] for b in blocks if b['type']=='text')
calls=[b for b in blocks if b['type']=='tool_use']
assert text=='' and len(calls)==1
print('await tool',calls[0]['id'])1
2
3
4
5
2
3
4
5
失败应结合请求异常与 stop_reason 判断,而不是把文本是否为空作为唯一成功信号。