前言

试了下智谱免费的api模型GLM-4.7-Flash。本文记录一下遇到的坑。


API 选择

智谱 AI 提供了很多的 API接口,这次主要测试了基础的ai对话和知识库检索的接口:

  1. 标准对话 API的官方使用示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    from zai import ZhipuAiClient

    # 初始化客户端
    client = ZhipuAiClient(api_key="YOUR_API_KEY")

    # 创建聊天完成请求
    response = client.chat.completions.create(
    model="glm-5.1",
    messages=[
    {
    "role": "system",
    "content": "你是一个有用的AI助手。"
    },
    {
    "role": "user",
    "content": "你好,请介绍一下自己。"
    }
    ],
    temperature=0.6
    )

    # 获取回复
    print(response.choices[0].message.content)
  2. 知识库检索 API的官方使用示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    curl --request POST \
    --url https://open.bigmodel.cn/api/zrag/agent/chat \
    --header 'Authorization: Bearer YOUR_API_KEY' \
    --header 'Content-Type: application/json' \
    --data '
    {
    "messages": [
    {
    "role": "user",
    "content": "公司的年假制度是什么?"
    }
    ],
    "retrieval": {
    "know_ids": [
    "123"
    ],
    "top_k": 8,
    "top_n": 10,
    "enable_rerank": false
    },
    "model": "glm-5v-turbo",
    "temperature": 0.2,
    "max_steps": 10
    }
    '

踩坑记录

看官方文档描述显示会根据问的问题自动调用知识库,测试发现好像只是网页版的入口支持
另外直接使用官方提供的对话请求格式,是能正常对话的,这里就不写无用的测试过程了

问题一:模型名称错误

错误信息:

1
Invalid model: GLM-4.7-Flash

更换模型名称即可:

1
model_name: "glm-5v-turbo"  # 使用正确的模型名称

一开始使用了 GLM-4.7-Flash 作为模型名称,想着免费,没想到知识库的调用不支持这个模型,报错后更换模型名称就正常了,测试默认的glm-5v-turbo是可以正常调用知识库的。

问题二:流式响应格式解析

原始响应格式:

1
2
3
4
5
data:{"type":"session_created","sessionId":"sess-xxx"}

data:{"type":"reasoning","data":"思考内容..."}

data:{"type":"done","data":"最终回复内容","usage":{...}}

要注意获取到正确的信息部分:

1
2
3
4
5
6
7
8
9
10
if response_text.startswith("data:"):
lines = response_text.strip().split("\n")
content = ""
for line in lines:
if line.startswith("data:"):
json_str = line[5:]
result = json.loads(json_str)
if result.get("type") == "done":
content = result.get("data", "")
break

问题三:System问题

添加 system 消息发现无法正常响应查询知识库,因为已经获得了知识库的测试结果,system我就不测试了

知识库 API 可能对 system 消息有特殊处理或不支持,最好先简化消息格式确认功能正常。


代码

zai.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import requests
import json


class ChatCompletion:
class Message:
def __init__(self, role, content):
self.role = role
self.content = content

class Choice:
def __init__(self, message):
self.message = message

def __init__(self, choices):
self.choices = choices


class ChatCompletions:
def __init__(self, client):
self.client = client

def create(self, model, messages, temperature=0.6, retrieval=None, max_steps=None):
if retrieval:
url = "https://open.bigmodel.cn/api/zrag/agent/chat"
else:
url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"

headers = {
"Authorization": f"Bearer {self.client.api_key}",
"Content-Type": "application/json"
}

data = {
"model": model,
"messages": messages,
"temperature": temperature
}

if retrieval:
data["retrieval"] = retrieval
if max_steps:
data["max_steps"] = max_steps

response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
response_text = response.text

# 处理多行流式响应格式
if response_text.startswith("data:"):
lines = response_text.strip().split("\n")
content = ""
for line in lines:
if line.startswith("data:"):
json_str = line[5:]
result = json.loads(json_str)
if result.get("type") == "done":
content = result.get("data", "")
break
msg = ChatCompletion.Message(role="assistant", content=content)
return ChatCompletion([ChatCompletion.Choice(msg)])

result = json.loads(response_text)
choices = []
if "choices" in result:
for choice in result.get("choices", []):
msg = ChatCompletion.Message(
role=choice["message"]["role"],
content=choice["message"]["content"]
)
choices.append(ChatCompletion.Choice(msg))
elif "response" in result:
msg = ChatCompletion.Message(role="assistant", content=result["response"])
choices.append(ChatCompletion.Choice(msg))

return ChatCompletion(choices)


class ZhipuAiClient:
def __init__(self, api_key):
self.api_key = api_key
self.chat = Chat(self)

参考资料

本文由 AI 助手辅助编写。