Skip to content

Commit 386a38c

Browse files
committed
报文解析错误
1 parent 2665a6e commit 386a38c

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ eggs/
1515
*.egg-info/
1616
test/
1717
examples/
18+
.pypirc

tests/test_chat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def encode_image(image_path):
5353

5454
def test_completions_vis():
5555
client = ZhipuAI() # 填写您自己的APIKey
56-
base64_image = encode_image("img/MetaGLM.png")
56+
base64_image = encode_image("img/MetaGLM.png")
5757
response = client.chat.completions.create(
5858
model="glm-4v", # 填写需要调用的模型名称
5959
extra_body={"temperature": 0.5, "max_tokens": 50},
@@ -116,6 +116,5 @@ def test_retrieve_completion_result():
116116
print(response)
117117

118118

119-
120119
if __name__ == "__main__":
121120
test_completions_vis()

zhipuai/api_resource/chat/completions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ def create(
9999
stream_cls=_stream_cls,
100100
)
101101

102-
def _drop_prefix_image_data(self, content: List) -> List:
102+
def _drop_prefix_image_data(self, content: Union[str,List[dict]]) -> Union[str,List[dict]]:
103103
"""
104104
删除 ;base64, 前缀
105105
:param image_data:
106106
:return:
107107
"""
108-
for data in content:
109-
if data.get('type') == 'image_url':
110-
image_data = data.get("image_url").get("url")
111-
if image_data.startswith("data:image/"):
112-
image_data = image_data.split("base64,")[-1]
113-
data["image_url"]["url"] = image_data
108+
if isinstance(content, List):
109+
for data in content:
110+
if data.get('type') == 'image_url':
111+
image_data = data.get("image_url").get("url")
112+
if image_data.startswith("data:image/"):
113+
image_data = image_data.split("base64,")[-1]
114+
data["image_url"]["url"] = image_data
114115

115116
return content
117+

0 commit comments

Comments
 (0)