智谱AI免费API调用
- 你是否也曾有过一个绝妙的AI点子,却被高昂的模型费用和复杂的部署环境劝退?想象一下,如果有一个‘AI魔法盒子’,不仅免费,而且开箱即用,你的下一个创意会是什么?
- 今天分享的是智谱AI:https://bigmodel.cn/
- 废话不多说注册登录
- 点击右上角控制台
- 继续点击API-Key-添加新的API-Key-命名确定
- 点击开发文档-免费模型即可体验众多免费的大模型API
- api支持http、Python、Java、JavaScript等等调用方式,下面提供Python调用的简单示例
- 首先安装Python-SDK:




pip install zai-sdk
import zai print(zai.__version__)
- 下面的是一个循环对话的Python样例大家可以自行替换自己的api-key测试,使用的是glm-4.5-flash免费模型
from zai import ZhipuAiClient defmain():# 初始化客户端 client = ZhipuAiClient(api_key='你的api-key')print("欢迎使用 Z.ai 聊天机器人!输入 'quit' 退出。")# 对话历史 conversation =[{"role":"system","content":"你是一个友好的 AI 助手"}]whileTrue:# 获取用户输入 user_input =input("您: ")if user_input.lower()=='quit':breaktry:# 添加用户消息 conversation.append({"role":"user","content": user_input})# 创建聊天请求 response = client.chat.completions.create( model="glm-4.5-flash", messages=conversation, temperature=0.7, max_tokens=65536, thinking={"type":"enabled"})# 获取 AI 回复 ai_response = response.choices[0].message.content print(f"AI: {ai_response}")# 添加 AI 回复到对话历史 conversation.append({"role":"assistant","content": ai_response})except Exception as e:print(f"发生错误: {e}")print("再见!")if __name__ =="__main__": main()
- 运行输入即可循环对话:
- 更多模型的api使用可以参考官方文档:https://docs.bigmodel.cn/cn/guide/develop/http/introduction