问题描述:在批量调用GPT API时,只有第一次成功,剩下的报错local variable 'proxy' referenced before assignment
解决方法:在尝试了各种在原代码中加代理的方法后,突然想起是不是if not hasattr方法的问题,因为之前在APIConnectionError 调用异常的时候,修改了if not hasattr方法,在方法里加了代理。
最终通过增加保护性检查解决。
在使用 custom_proxy
之前,先检查它是否已经初始化。如果没有初始化,可以在 try-except
中重新赋值。把下面这段代码加在图上的位置,遂解决。
if 'custom_proxy' not in locals():
custom_proxy = {
'http': 'http://localhost:7890',
'https': 'http://localhost:7890'
}