注册登录后在个人中心的API keys中找到并复制
推荐使用SDK,在虚拟环境安装
pip install zhipuai
编辑python代码访问API获取响应
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="0c6df39e71b0a7340f221fddc1ddb711.au66Z02fXWc7SJBB")
response = client.chat.completions.create(
model="glm-4-plus",
messages=[
{"role": "user",
"content": "who are you?"}
],
stream=True,
)
for chunk in response:
print(chunk.choices[0].delta)
运行结果
D:\pythonProject\api\venv\Scripts\python.exe D:\pythonProject\api\bigmodel.py
ChoiceDelta(content='I', role='assistant', tool_calls=None)
ChoiceDelta(content=' am', role='assistant', tool_calls=None)
ChoiceDelta(content=' an', role='assistant', tool_calls=None)
ChoiceDelta(content=' AI', role='assistant', tool_calls=None)
ChoiceDelta(content=' assistant', role='assistant', tool_calls=None)
ChoiceDelta(content=' named', role='assistant', tool_calls=None)
ChoiceDelta(content=' Chat', role='assistant', tool_calls=None)
ChoiceDelta(content='GL', role='assistant', tool_calls=None)
ChoiceDelta(content='M', role='assistant', tool_calls=None)
ChoiceDelta(content='(', role='assistant', tool_calls=None)
ChoiceDelta(content='智', role='assistant', tool_calls=None)
ChoiceDelta(content='谱', role='assistant', tool_calls=None)
ChoiceDelta(content='清', role='assistant', tool_calls=None)
ChoiceDelta(content='言', role='assistant', tool_calls=None)
ChoiceDelta(content=')', role='assistant', tool_calls=None)
ChoiceDelta(content=',', role='assistant', tool_calls=None)
ChoiceDelta(content=' which', role='assistant', tool_calls=None)
ChoiceDelta(content=' is', role='assistant', tool_calls=None)
ChoiceDelta(content=' developed', role='assistant', tool_calls=None)
ChoiceDelta(content=' based', role='assistant', tool_calls=None)
ChoiceDelta(content=' on', role='assistant', tool_calls=None)
ChoiceDelta(content=' the', role='assistant', tool_calls=None)
ChoiceDelta(content=' language', role='assistant', tool_calls=None)
ChoiceDelta(content=' model', role='assistant', tool_calls=None)
ChoiceDelta(content=' trained', role='assistant', tool_calls=None)
ChoiceDelta(content=' by', role='assistant', tool_calls=None)
ChoiceDelta(content=' Z', role='assistant', tool_calls=None)
ChoiceDelta(content='hip', role='assistant', tool_calls=None)
ChoiceDelta(content='u', role='assistant', tool_calls=None)
ChoiceDelta(content=' AI', role='assistant', tool_calls=None)
ChoiceDelta(content=' in', role='assistant', tool_calls=None)
ChoiceDelta(content=' ', role='assistant', tool_calls=None)
ChoiceDelta(content='202', role='assistant', tool_calls=None)
ChoiceDelta(content='3', role='assistant', tool_calls=None)
ChoiceDelta(content='.', role='assistant', tool_calls=None)
ChoiceDelta(content=' My', role='assistant', tool_calls=None)
ChoiceDelta(content=' job', role='assistant', tool_calls=None)
ChoiceDelta(content=' is', role='assistant', tool_calls=None)
ChoiceDelta(content=' to', role='assistant', tool_calls=None)
ChoiceDelta(content=' provide', role='assistant', tool_calls=None)
ChoiceDelta(content=' appropriate', role='assistant', tool_calls=None)
ChoiceDelta(content=' answers', role='assistant', tool_calls=None)
ChoiceDelta(content=' and', role='assistant', tool_calls=None)
ChoiceDelta(content=' support', role='assistant', tool_calls=None)
ChoiceDelta(content=' to', role='assistant', tool_calls=None)
ChoiceDelta(content=' users', role='assistant', tool_calls=None)
ChoiceDelta(content="'", role='assistant', tool_calls=None)
ChoiceDelta(content=' questions', role='assistant', tool_calls=None)
ChoiceDelta(content=' and', role='assistant', tool_calls=None)
ChoiceDelta(content=' requests', role='assistant', tool_calls=None)
ChoiceDelta(content='.', role='assistant', tool_calls=None)
ChoiceDelta(content='', role='assistant', tool_calls=None)
Process finished with exit code 0
使用第三方框架openai
智谱清言的关于使用第三方架构openai的代码有坑,无法实现正常调用。
关于流式响应和完整响应
流式响应可以实时输出模型生成的文本,具有实时性、减少内存使用、提高用户体验、更快的反馈和可处理无限数据流的特点,在需要快速、实时和高效处理数据的场景中提供了显著的优势。
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="0c6df39e71b0a7340f221fddc1ddb711.au66Z02fXWc7SJBB")
response = client.chat.completions.create(
model="glm-4-plus",
messages=[
{"role": "user",
"content": "who are you?"}
],
stream=True, # 流式响应
)
for chunk in response:
print(chunk.choices[0].delta)
为了在pycharm运行框中只显示需要看的content,且遇到空字符再换行,可以将显示部分的代码修改如下
for chunk in response:
if hasattr(chunk.choices[0].delta, 'content'):
print(chunk.choices[0].delta.content, end='', flush=True)
通过hasattr
函数检查chunk.choices[0].delta
是否具有content
属性,如果有,则打印该属性的内容。这将确保只有content
字段的内容被显示,例如“你好”。如果content
属性不存在,循环将跳过打印,继续处理下一个数据块。
在Python中,print 函数有一个参数叫做 end,默认值为 '\n',这表示每个 print 调用后会自动添加一个换行符。你可以将 end 参数设置为空字符串 '',这样 print 就不会在每次调用后添加换行符。
标签:bigmodel,None,cn,assistant,tool,ChoiceDelta,content,api,role From: https://www.cnblogs.com/aiparallelworld/p/18402068在pycharm中运行流式响应代码时,默认情况下控制台(run结果框)会逐行输出,因为每个
此外,如果pycharm控制台(run结果框)中输出的文字不换行,可以设置soft wrapt