Skip to main content

智谱大模型开放接口SDK

PyPI version

智谱开放平台大模型接口 Python SDK(Big Model API SDK in Python),让开发者更便捷的调用智谱开放API

简介

  • 对所有接口进行了类型封装。
  • 初始化client并调用成员函数,无需关注http调用过程的各种细节,所见即所得。
  • 默认缓存token。

安装

  • 运行环境: Python>=3.7

  • 使用 pip 安装 zhipuai 软件包及其依赖

pip install zhipuai

使用

  • 调用流程:
    1. 使用 APISecretKey 创建 Client
    2. 调用 Client 对应的成员方法
  • 开放平台接口文档以及使用指南中有更多的 demo 示例,请在 demo 中使用自己的 ApiKey 进行测试。

创建Client

sdk支持通过环境变量配置APIKey

  • env

ZHIPUAI_API_KEY: 您的APIKey

ZHIPUAI_BASE_URL: 您的API地址

  • 也支持通过代码传入APIKey
from zhipuai import ZhipuAI

client = ZhipuAI(
    api_key="", # 填写您的 APIKey
) 

同步调用

from zhipuai import ZhipuAI 
 
client = ZhipuAI()  # 填写您自己的APIKey
response = client.chat.completions.create(
  model="glm-4",  # 填写需要调用的模型名称
  messages=[
    {"role": "user", "content": "作为一名营销专家,请为我的产品创作一个吸引人的slogan"},
    {"role": "assistant", "content": "当然,为了创作一个吸引人的slogan,请告诉我一些关于您产品的信息"},
    {"role": "user", "content": "智谱AI开放平台"},
    {"role": "assistant", "content": "智启未来,谱绘无限一智谱AI,让创新触手可及!"},
    {"role": "user", "content": "创造一个更精准、吸引人的slogan"}
  ],
  tools=[
    {
      "type": "web_search",
      "web_search": {
        "search_query": "帮我看看清华的升学率",
        "search_result": True,
      }
    }
  ],
  # 拓展参数
  extra_body={"temperature": 0.5, "max_tokens": 50},
)
print(response) 

SSE 调用

from zhipuai import ZhipuAI
client = ZhipuAI(api_key="") # 请填写您自己的APIKey
response = client.chat.completions.create(
    model="",  # 填写需要调用的模型名称
    messages=[
        {"role": "system", "content": "你是一个人工智能助手,你叫叫chatGLM"},
        {"role": "user", "content": "你好!你叫什么名字"},
    ],
    stream=True,
)
for chunk in response:
    print(chunk.choices[0].delta)

多模态

# Function to encode the image
def encode_image(image_path):
    import base64
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')


def test_completions_vis():
  client = ZhipuAI()  # 填写您自己的APIKey
  base64_image  = encode_image("img/MetaGLM.png")
  response = client.chat.completions.create(
    model="glm-4v",  # 填写需要调用的模型名称
    extra_body={"temperature": 0.5, "max_tokens": 50},
    messages=[
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "图里有什么"
          },

          # {
          #     "type": "image_url",
          #     "image_url": {
          #         "url": "https://img1.baidu.com/it/u=1369931113,3388870256&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1703696400&t=f3028c7a1dca43a080aeb8239f09cc2f"
          #     }
          # },
          {
            "type": "image_url",
            "image_url": {
              "url": f"data:image/jpeg;base64,{base64_image}"
            }
          }
        ]
      }
    ]
  )
  print(response)

test_completions_vis()

角色扮演

提供能力的模型名称,请从官网获取

def test_completions_charglm():
    client = ZhipuAI()  # 请填写您自己的APIKey
    response = client.chat.completions.create(
        model="charglm-3",  # 填写需要调用的模型名称
        messages=[
            {
                "role": "user",
                "content": "请问你在做什么"
            }
        ],
        extra_body={
            "meta": {
                "user_info": "我是陆星辰,是一个男性,是一位知名导演,也是苏梦远的合作导演。我擅长拍摄音乐题材的电影。苏梦远对我的态度是尊敬的,并视我为良师益友。",
                "bot_info": "苏梦远,本名苏远心,是一位当红的国内女歌手及演员。在参加选秀节目后,凭借独特的嗓音及出众的舞台魅力迅速成名,进入娱乐圈。她外表美丽动人,但真正的魅力在于她的才华和勤奋。苏梦远是音乐学院毕业的优秀生,善于创作,拥有多首热门原创歌曲。除了音乐方面的成就,她还热衷于慈善事业,积极参加公益活动,用实际行动传递正能量。在工作中,她对待工作非常敬业,拍戏时总是全身心投入角色,赢得了业内人士的赞誉和粉丝的喜爱。虽然在娱乐圈,但她始终保持低调、谦逊的态度,深得同行尊重。在表达时,苏梦远喜欢使用“我们”和“一起”,强调团队精神。",
                "bot_name": "苏梦远",
                "user_name": "陆星辰"
            },
        }
    )
    print(response)
test_completions_charglm()

异常处理

When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of openai.APIConnectionError is raised.

When the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of openai.APIStatusError is raised, containing status_code and response properties.

All errors inherit from openai.APIError.

import openai
from openai import OpenAI

client = OpenAI()

try:
    client.fine_tunes.create(
        training_file="file-XGinujblHPwGLSztz8cPS8XY",
    )
except openai.APIConnectionError as e:
    print("The server could not be reached")
    print(e.__cause__)  # an underlying Exception, likely raised within httpx.
except openai.RateLimitError as e:
    print("A 429 status code was received; we should back off a bit.")
except openai.APIStatusError as e:
    print("Another non-200-range status code was received")
    print(e.status_code)
    print(e.response)

Error codes are as followed:

Status Code Error Type
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
422 UnprocessableEntityError
429 RateLimitError
>=500 InternalServerError
N/A APIConnectionError

更新日志

2024-4-23

  • 一些兼容 pydantic<3,>=1.9.0 的代码,
  • 报文处理的业务请求参数和响应参数可通过配置扩充
  • 兼容了一些参数 top_p:1,temperture:0(do_sample重写false,参数top_p temperture不生效)
  • 图像理解部分, image_url参数base64内容包含 data:image/jpeg;base64兼容
  • 删除jwt认证逻辑

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zhipuai-2.0.1.20240423.tar.gz (35.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zhipuai-2.0.1.20240423-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file zhipuai-2.0.1.20240423.tar.gz.

File metadata

  • Download URL: zhipuai-2.0.1.20240423.tar.gz
  • Upload date:
  • Size: 35.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.19

File hashes

Hashes for zhipuai-2.0.1.20240423.tar.gz
Algorithm Hash digest
SHA256 3de79322102ab0a8888edd166675eec1690f9303578c9b7caf58aa8c9ceaa6ee
MD5 6cdcb60e82e3b11d4668f974117455ff
BLAKE2b-256 98f49ee70e004e049814405e307cdbcae21f8549aa5d4eac260459804a3798ea

See more details on using hashes here.

File details

Details for the file zhipuai-2.0.1.20240423-py3-none-any.whl.

File metadata

File hashes

Hashes for zhipuai-2.0.1.20240423-py3-none-any.whl
Algorithm Hash digest
SHA256 090cd6edfdc3b6400e122ecc10195b684d9d6ee85b2f99354a625c980106e96d
MD5 5ecbd2e2bdfde6e47dc2bc798e85237f
BLAKE2b-256 f7f444a09bc1ce963820bcac59473222f3dbe16d0ddbea8beb66e9379451991f

See more details on using hashes here.

Release history Release notifications | RSS feed

2.1.5.20250825

2 files

2.1.5.20250726

2 files

2.1.5.20250725

2 files

2.1.5.20250724

2 files

2.1.5.20250717

2 files

2.1.5.20250716

2 files

2.1.5.20250715

2 files

2.1.5.20250626

2 files

2.1.5.20250625

2 files

2.1.5.20250611

2 files

2.1.5.20250526

2 files

2.1.5.20250421

2 files

2.1.5.20250415

2 files

2.1.5.20250410

2 files

2.1.4.20230814

2 files

2.1.4.20230812

2 files

2.1.4.20230731

2 files

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.1.0.20240521

2 files

2.1.0

2 files

2.0.1.20240429

2 files

2.0.1.20240427

2 files

2.0.1.20240426

2 files

This release

2.0.1.20240423 This release

2 files

2.0.1.20230426

2 files

2.0.1.1

2 files

2.0.1

2 files

1.0.7

2 files

1.0.4

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page