报错:AttributeError: 'str' object has no attribute 'get'
happylittle2010 opened this issue · 7 comments
happylittle2010 commented
直接复制官方演示的chat代码运行:
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="xxxx") # 填写您自己的APIKey
response = client.chat.completions.create(
model="glm-4", # 填写需要调用的模型名称
messages=[
{"role": "user", "content": "作为一名营销专家,请为我的产品创作一个吸引人的slogan"},
{"role": "assistant",
"content": "当然,为了创作一个吸引人的slogan,请告诉我一些关于您产品的信息"},
{"role": "user", "content": "智谱AI开放平台"},
{"role": "assistant", "content": "智启未来,谱绘无限一智谱AI,让创新触手可及!"},
{"role": "user", "content": "创造一个更精准、吸引人的slogan"}
]
)
print(response)
运行后报错:
Traceback (most recent call last):
File "C:\Users\happy\PycharmProjects\Quantization_AWQ_locally\LLM_services\zhipu_chat.py", line 30, in <module>
response = client.chat.completions.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\happy\miniconda3\envs\Quantization_AWQ_locally\Lib\site-packages\zhipuai\api_resource\chat\completions.py", line 75, in create
item['content'] = self._drop_prefix_image_data(item['content'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\happy\miniconda3\envs\Quantization_AWQ_locally\Lib\site-packages\zhipuai\api_resource\chat\completions.py", line 109, in _drop_prefix_image_data
if data.get('type') == 'image_url':
^^^^^^^^
AttributeError: 'str' object has no attribute 'get'
找到问题出在文件:Lib\site-packages\zhipuai\api_resource\chat\completions.py中的函数:
def _drop_prefix_image_data(self, content: List) -> List:
"""
删除 ;base64, 前缀
:param image_data:
:return:
"""
for data in content:
if data.get('type') == 'image_url':
image_data = data.get("image_url").get("url")
if image_data.startswith("data:image/"):
image_data = image_data.split("base64,")[-1]
data["image_url"]["url"] = image_data
return content
注释掉函数体后再次运行,不再报错。
ylc2001 commented
遇到同样的问题。这是个显然的错误,示例代码跑不通说不过去了哈
ylc2001 commented
看起来是最近的那次 commit 造成了这个问题,就在一小时前 QAQ
LKAMING97 commented
请问注释掉哪里才可以使用?
happylittle2010 commented
遇到同样的问题。这是个显然的错误,示例代码跑不通说不过去了哈
哈哈哈,是很说不过去,那个函数就很迷
happylittle2010 commented
请问注释掉哪里才可以使用?
你找到我说的那个函数,把函数里面的代码注释掉,让函数直接返回“content”就行了
happylittle2010 commented
看起来是最近的那次 commit 造成了这个问题,就在一小时前 QAQ
commit了之后就直接构建发布到pip里面了吗QAQ
glide-the commented
已经修复此问题,使用upgrade更新