本指南将帮助您快速上手智谱开放平台,从注册账号到发起第一次 API 调用,只需几分钟即可完成。

开始使用

1

注册账号

访问智谱开放平台,点击右上角的「注册/登录」按钮,按照提示完成账号注册流程。
注册账号
2

获取API Key

登录后,在个人中心页面,点击 API Keys,创建一个新的 API Key。
请妥善保管您的 API Key,不要泄露给他人,也不要直接硬编码在代码中。建议使用环境变量或配置文件来存储 API Key。
获取 API Key
5

发起调用

准备好 API Key 和选择模型后,您可以开始发起调用。以下是使用 curlPython SDK Java SDK 的示例:
curl -X POST "https://open.bigmodel.cn/api/paas/v4/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "system",
            "content": "你是一个有用的AI助手。"
        },
        {
            "role": "user",
            "content": "你好,请介绍一下自己。"
        }
    ],
    "temperature": 0.6,
    "stream": true
}'

探索更多功能

流式输出

启用流式输出,获得更自然的对话体验。
{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "user",
            "content": "你好,请介绍一下自己。"
        }
    ],
    "stream": true
}

多模态输入

使用 GLM-4V 模型处理图像和文本的混合输入。
{
    "model": "glm-4v",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片是什么?"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "data:image/jpeg;base64,..."
                    }
                }
            ]
        }
    ]
}

函数调用

使用函数调用功能,让模型调用您定义的函数。
{
    "model": "glm-4.5",
    "messages": [
        {
            "type": "text",
            "text": "帮我查询从2024年1月20日,从北京出发前往上海的航班"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_flight_number",
                "description": "根据始发地、目的地和日期,查询对应日期的航班号",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "departure": {
                            "description": "出发地",
                            "type": "string"
                        },
                        "destination": {
                            "description": "目的地",
                            "type": "string"
                        },
                        "date": {
                            "description": "日期",
                            "type": "string"
                        }
                    },
                    "required": ["departure", "destination", "date"]
                }
            }
        }
    ]
}

常见问题

如果您在使用过程中遇到任何问题,可以查阅 完整文档 或联系我们的 技术支持