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


注册登录后在个人中心的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 就不会在每次调用后添加换行符。

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/793753.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

焦煤

这种走势概率大 目前在走3-5的跌势

linux虚拟机(centos)搭建sqli-labs

1.开启小皮2.查看文件位置 配置文件路径为/usr/local/phpstudy/soft [root@localhost soft]# cd /www/admin/localhost_80 [root@localhost soft]# pwd /usr/local/phpstudy/soft网站根目录为/www/admin/localhost_80/wwwroot [root@localhost localhost_80]# cd wwwroot [root…

Zabbix01 Zabbix安装和基础功能

商业监控方案#从各个地区来监测网络情况 http://ping.chinaz.com/ 站长之家 免费 https://www.jiankongbao.com/ 监控宝 ...#云服务自带云监控系统 Zabbix 架构#zabbix web为php程序 如果公司规模小,zabbix server,db和zabbix web装在一台机器上 如果公司规模大,…

【赛后反思】洛谷基础赛 #15 「LAOI」Round 6 考后总结(待补完)

待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完待补完LGR-198-Div.3 考后总结 又要掉分了:展开目录 目录LGR-198-Div.3 考后总结A [太阳]] 请…

Win10电脑网络正常,其他浏览器可以打开网页,但Chrome浏览器打不开网页,开发者工具中看请求未发出,左上角一直转圈圈

问题现象: Win10电脑网络正常,可以ping通baidu.com, qq.com, 域名正常解析。 其他浏览器edge可以打开网页 但Chrome浏览器打不开网页,开发者工具中看请求未发出,左上角一直转圈圈解决办法: 谷歌浏览器右上角,点击三个点按钮-->然后选择设置,高级 --> 系统 -->…

[c++][笔记]浅谈几种排序方式---冒泡排序,选择排序,桶排序

一、algorithm里的sort函数 #include <cstdio> // 数据小的可以用iostream #include <algorithm> // 不能忘记算法库,否则会编译失败。 using namespace std; int main() {int n;scanf("%d", &n);int a[n+5] = {};for (int i = 1; i <= n; i++)…

Java反序列化漏洞-TemplatesImpl利用链分析

目录一、前言二、正文1. 寻找利用链2. 构造POC2.1 生成字节码2.2 加载字节码1)getTransletInstance2)defineTransletClasses2.3 创建实例3. 完整POC三、参考文章 一、前言 java.lang.ClassLoader#defineClassdefineClass可以加载字节码,但由于defineClass的作用域是protecte…

Camunda Modeler流程设计器

1、介绍 任何可执行流程都需要预先设计和配置业务流程模型和BPMN图,BPMN图可以让使用者更容易理解流程的结构,Camunda Modeler是一个可视化设计和实现BPMN图表的工具。 下面是官方使用文档:1、Modeler中绘制BPMN介绍 2、桌面版Modeler使用介绍 2、相关概念 可以将BPMN的绘制…

【工具推荐】KillWxapkg v2.4(最新版) - 自动化反编译微信小程序,小程序安全评估工具

工具介绍: 纯Golang实现,一个用于自动化反编译微信小程序的工具,小程序安全利器,自动解密,解包,可还原工程目录,支持微信开发者工具运行 下载链接: 链接:https://pan.quark.cn/s/aa5480be4bd5使用说明 工程结构还原 还原前还原后微信开发者工具运行看着就真的看着,不…

Agent(智能体)和 MetaGPT,一句话实现整个需求应用代码

本文介绍了大模型 Agent 定义、组成部分,并以 MetaGPT 多智能体为例,一句话完成贪吃蛇小游戏需求,以介绍整个智能体的工作流程……前面 2 篇文章,我们使用文生文、文生图和文生音频三个大模型共同实现了图文并茂的儿童绘本故事和绘本故事音频需求:第一篇 根据主题生成儿童…

html的表单和初始js

1.表单是html常用的一类,我们平时使用的收集账号密码填写信息都是表单,标签是form,含有属性action和method,action确定表单接受数据的地址,不写默认为网页本身.method有两种收集方式,"post"和"get",其中默认方式为get,但是get对接收信息的大小有限制,post没…

秋天到了是因为要做操

为两朵花找到了属于它们的色彩;前路漫漫,我会一个人走吗。灯笼迟早会消失,会结束。 我把它献给NR吧。