欢迎来到海上华帆的博客园子

记录一些学习过程中的心得体会,供自己和有缘人参考!

大模型api实战-open.bigmodel.cn


注册登录后在个人中心的API keys中找到并复制

推荐使用SDK,在虚拟环境安装

pip install zhipuai

编辑python代码访问API获取响应

from zhipuai import ZhipuAI
client = ZhipuAI(api_key="your_api_key")
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 就不会在每次调用后添加换行符。

在pycharm中运行流式响应代码时,默认情况下控制台(run结果框)会逐行输出,因为每个print调用都会添加一个换行符。如果你希望内容连续输出而不是分行,可以修改print函数,使其不自动添加换行符。在Python中,print 函数有一个参数叫做 end,默认值为 '\n',这表示每个 print 调用后会自动添加一个换行符。你可以将 end 参数设置为空字符串 '',这样 print 就不会在每次调用后添加换行符。
此外,如果pycharm控制台(run结果框)中输出的文字不换行,可以设置soft wrapt

posted @   海上华帆  阅读(131)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示