zhipuai


Namezhipuai JSON
Version 2.1.5.20230904 PyPI version JSON
download
home_pageNone
SummaryA SDK library for accessing big model apis from ZhipuAI
upload_time2024-09-04 09:20:44
maintainerNone
docs_urlNone
authorZhipu AI
requires_python!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*,>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 智谱大模型开放接口SDK

[![PyPI version](https://img.shields.io/pypi/v/zhipuai.svg)](https://pypi.org/project/zhipuai/)

智谱[开放平台](https://open.bigmodel.cn/dev/api)大模型接口 Python SDK(Big Model API SDK in Python),让开发者更便捷的调用智谱开放API


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

## 安装


### Python版本支持
正式的 Python (3.8, 3.9, 3.10, 3.11, 3.12)

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

```sh
pip install zhipuai
```

## 使用

- 调用流程:
    1. 使用 APISecretKey 创建 Client
    2. 调用 Client 对应的成员方法
- 开放平台[接口文档](https://open.bigmodel.cn/dev/api)以及[使用指南](https://open.bigmodel.cn/dev/howuse/)中有更多的 demo 示例,请在 demo 中使用自己的 ApiKey 进行测试。

### 创建Client
sdk支持通过环境变量配置APIKey
- env

`ZHIPUAI_API_KEY`: 您的APIKey

`ZHIPUAI_BASE_URL`: 您的API地址

- 也支持通过代码传入APIKey
```python
from zhipuai import ZhipuAI

client = ZhipuAI(
    api_key="", # 填写您的 APIKey
) 
```
### 客户端网络链接配置
在`core/_http_client.py`中,可以配置网络链接的超时时间,重试次数,限制等参数
```python
# 通过 `Timeout` 控制接口`connect` 和 `read` 超时时间,默认为`timeout=300.0, connect=8.0`
ZHIPUAI_DEFAULT_TIMEOUT = httpx.Timeout(timeout=300.0, connect=8.0)
# 通过 `retry` 参数控制重试次数,默认为3次
ZHIPUAI_DEFAULT_MAX_RETRIES = 3
# 通过 `Limits` 控制最大连接数和保持连接数,默认为`max_connections=50, max_keepalive_connections=10`
ZHIPUAI_DEFAULT_LIMITS = httpx.Limits(max_connections=50, max_keepalive_connections=10)
 
```
同样在`ZhipuAI`入参中可以配置
```python
client = ZhipuAI(
    timeout= httpx.Timeout(timeout=300.0, connect=8.0),
    max_retries=3,
)
```


### 同步调用

```python
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 调用

```python
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)
```

### 多模态
```python


# 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()
```

### 角色扮演
> 提供能力的模型名称,请从官网获取
```python

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


### 智能体 
```python

def test_assistant() -> None: 
  client = ZhipuAI()  # 填写您自己的APIKey
 

  generate = client.assistant.conversation(
    assistant_id="659e54b1b8006379b4b2abd6",
    model="glm-4-assistant",
    messages=[
      {
        "role": "user",
        "content": [{
          "type": "text",
          "text": "帮我搜索下智谱的cogvideox发布时间"
        }]
      }
    ],
    stream=True,
    attachments=None,
    metadata=None,
    request_id="request_1790291013237211136",
    user_id="12345678"
  )
  for assistant in generate:
    print(assistant)

test_assistant()
```

### 视频生成 
```python


def test_videos(): 
  client = ZhipuAI()  # 填写您自己的APIKey
  try:
    response = client.videos.generations(
      model="cogvideo",
      prompt="一个开船的人",

      user_id="1212222"
    )
    print(response)
    
test_videos()
```



### 异常处理

模块定义了一些统一的参数返回(例如:响应错误,网络超时错误)

业务定义了http错误的响应类 (在接口返回,40x或者50x), 会抛出 `zhipuai.APIStatusError`  ,包含 `status_code` 和 `response` 属性. 它们都是继承 `zhipuai.APIStatusError`.
其它Exception,属于不可预知的错误
```python
from zhipuai import ZhipuAI
import zhipuai
client = ZhipuAI()  # 填写您自己的APIKey
try:
  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"}
    ]
  )
  print(response)
 
except zhipuai.APIStatusError as err:
  print(err) 
except zhipuai.APITimeoutError as err:
  print(err) 
```

Error codes are as followed:

| Status Code | Error Type                 |
|-------------| -------------------------- |
| 400         | `APIRequestFailedError`          |
| 401         | `APIAuthenticationError`      |
| 429         | `APIReachLimitError`           |
| 500         | `APIInternalError`      |
| 503         | `APIServerFlowExceedError`      |
| N/A         | `APIStatusError`       |



### 更新日志

`2024-8-12`  
- 修改视频提示词可选,增加文件删除
- Assistant业务
- embedding 3 fix dimensions
  
`2024-7-25`  
- cogvideo 修复
  
`2024-7-12` 
- 高级搜索工具 Web search 业务 
- specified Python versions (3.8, 3.9, 3.10, 3.11, 3.12) 
- cogvideo 业务集成
  
`2024-5-20` 
- 一些 `python3.12` 的依赖问题, 
- 增加分页处理代码,重写部分相应类的实例化规则
- 增加类型转换校验
- 批处理任务相关api 
- 文件流响应包装器   

`2024-4-29` 
- 一些 `python3.7` 的代码适配问题, 
- 接口失败重试机制,通过 `retry` 参数控制重试次数,默认为3次
- 接口超时策略调整,通过 `Timeout` 控制接口`connect` 和 `read` 超时时间,默认为`timeout=300.0, connect=8.0`
- 对话模块增加超拟人大模型参数支持,`model="charglm-3"`, `meta`参数支持
  
`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认证逻辑
  


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "zhipuai",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Zhipu AI",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/d5/14/81aaac7a2aef3273283bf8f1569dab44ca11f0052d9782b7905572575ac3/zhipuai-2.1.5.20230904.tar.gz",
    "platform": null,
    "description": "# \u667a\u8c31\u5927\u6a21\u578b\u5f00\u653e\u63a5\u53e3SDK\n\n[![PyPI version](https://img.shields.io/pypi/v/zhipuai.svg)](https://pypi.org/project/zhipuai/)\n\n\u667a\u8c31[\u5f00\u653e\u5e73\u53f0](https://open.bigmodel.cn/dev/api)\u5927\u6a21\u578b\u63a5\u53e3 Python SDK\uff08Big Model API SDK in Python\uff09\uff0c\u8ba9\u5f00\u53d1\u8005\u66f4\u4fbf\u6377\u7684\u8c03\u7528\u667a\u8c31\u5f00\u653eAPI\n\n\n## \u7b80\u4ecb\n- \u5bf9\u6240\u6709\u63a5\u53e3\u8fdb\u884c\u4e86\u7c7b\u578b\u5c01\u88c5\u3002\n- \u521d\u59cb\u5316client\u5e76\u8c03\u7528\u6210\u5458\u51fd\u6570\uff0c\u65e0\u9700\u5173\u6ce8http\u8c03\u7528\u8fc7\u7a0b\u7684\u5404\u79cd\u7ec6\u8282\uff0c\u6240\u89c1\u5373\u6240\u5f97\u3002\n- \u9ed8\u8ba4\u7f13\u5b58token\u3002\n\n## \u5b89\u88c5\n\n\n### Python\u7248\u672c\u652f\u6301\n\u6b63\u5f0f\u7684 Python (3.8, 3.9, 3.10, 3.11, 3.12)\n\n### \u4f7f\u7528 pip \u5b89\u88c5 `zhipuai` \u8f6f\u4ef6\u5305\u53ca\u5176\u4f9d\u8d56\n\n```sh\npip install zhipuai\n```\n\n## \u4f7f\u7528\n\n- \u8c03\u7528\u6d41\u7a0b\uff1a\n    1. \u4f7f\u7528 APISecretKey \u521b\u5efa Client\n    2. \u8c03\u7528 Client \u5bf9\u5e94\u7684\u6210\u5458\u65b9\u6cd5\n- \u5f00\u653e\u5e73\u53f0[\u63a5\u53e3\u6587\u6863](https://open.bigmodel.cn/dev/api)\u4ee5\u53ca[\u4f7f\u7528\u6307\u5357](https://open.bigmodel.cn/dev/howuse/)\u4e2d\u6709\u66f4\u591a\u7684 demo \u793a\u4f8b\uff0c\u8bf7\u5728 demo \u4e2d\u4f7f\u7528\u81ea\u5df1\u7684 ApiKey \u8fdb\u884c\u6d4b\u8bd5\u3002\n\n### \u521b\u5efaClient\nsdk\u652f\u6301\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u914d\u7f6eAPIKey\n- env\n\n`ZHIPUAI_API_KEY`: \u60a8\u7684APIKey\n\n`ZHIPUAI_BASE_URL`: \u60a8\u7684API\u5730\u5740\n\n- \u4e5f\u652f\u6301\u901a\u8fc7\u4ee3\u7801\u4f20\u5165APIKey\n```python\nfrom zhipuai import ZhipuAI\n\nclient = ZhipuAI(\n    api_key=\"\", # \u586b\u5199\u60a8\u7684 APIKey\n) \n```\n### \u5ba2\u6237\u7aef\u7f51\u7edc\u94fe\u63a5\u914d\u7f6e\n\u5728`core/_http_client.py`\u4e2d\uff0c\u53ef\u4ee5\u914d\u7f6e\u7f51\u7edc\u94fe\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u91cd\u8bd5\u6b21\u6570\uff0c\u9650\u5236\u7b49\u53c2\u6570\n```python\n# \u901a\u8fc7 `Timeout` \u63a7\u5236\u63a5\u53e3`connect` \u548c `read` \u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a`timeout=300.0, connect=8.0`\nZHIPUAI_DEFAULT_TIMEOUT = httpx.Timeout(timeout=300.0, connect=8.0)\n# \u901a\u8fc7 `retry` \u53c2\u6570\u63a7\u5236\u91cd\u8bd5\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a3\u6b21\nZHIPUAI_DEFAULT_MAX_RETRIES = 3\n# \u901a\u8fc7 `Limits` \u63a7\u5236\u6700\u5927\u8fde\u63a5\u6570\u548c\u4fdd\u6301\u8fde\u63a5\u6570\uff0c\u9ed8\u8ba4\u4e3a`max_connections=50, max_keepalive_connections=10`\nZHIPUAI_DEFAULT_LIMITS = httpx.Limits(max_connections=50, max_keepalive_connections=10)\n \n```\n\u540c\u6837\u5728`ZhipuAI`\u5165\u53c2\u4e2d\u53ef\u4ee5\u914d\u7f6e\n```python\nclient = ZhipuAI(\n    timeout= httpx.Timeout(timeout=300.0, connect=8.0),\n    max_retries=3,\n)\n```\n\n\n### \u540c\u6b65\u8c03\u7528\n\n```python\nfrom zhipuai import ZhipuAI \n \nclient = ZhipuAI()  # \u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\nresponse = client.chat.completions.create(\n  model=\"glm-4\",  # \u586b\u5199\u9700\u8981\u8c03\u7528\u7684\u6a21\u578b\u540d\u79f0\n  messages=[\n    {\"role\": \"user\", \"content\": \"\u4f5c\u4e3a\u4e00\u540d\u8425\u9500\u4e13\u5bb6\uff0c\u8bf7\u4e3a\u6211\u7684\u4ea7\u54c1\u521b\u4f5c\u4e00\u4e2a\u5438\u5f15\u4eba\u7684slogan\"},\n    {\"role\": \"assistant\", \"content\": \"\u5f53\u7136\uff0c\u4e3a\u4e86\u521b\u4f5c\u4e00\u4e2a\u5438\u5f15\u4eba\u7684slogan\uff0c\u8bf7\u544a\u8bc9\u6211\u4e00\u4e9b\u5173\u4e8e\u60a8\u4ea7\u54c1\u7684\u4fe1\u606f\"},\n    {\"role\": \"user\", \"content\": \"\u667a\u8c31AI\u5f00\u653e\u5e73\u53f0\"},\n    {\"role\": \"assistant\", \"content\": \"\u667a\u542f\u672a\u6765\uff0c\u8c31\u7ed8\u65e0\u9650\u4e00\u667a\u8c31AI\uff0c\u8ba9\u521b\u65b0\u89e6\u624b\u53ef\u53ca!\"},\n    {\"role\": \"user\", \"content\": \"\u521b\u9020\u4e00\u4e2a\u66f4\u7cbe\u51c6\u3001\u5438\u5f15\u4eba\u7684slogan\"}\n  ],\n  tools=[\n    {\n      \"type\": \"web_search\",\n      \"web_search\": {\n        \"search_query\": \"\u5e2e\u6211\u770b\u770b\u6e05\u534e\u7684\u5347\u5b66\u7387\",\n        \"search_result\": True,\n      }\n    }\n  ],\n  # \u62d3\u5c55\u53c2\u6570\n  extra_body={\"temperature\": 0.5, \"max_tokens\": 50},\n)\nprint(response) \n```\n\n### SSE \u8c03\u7528\n\n```python\nfrom zhipuai import ZhipuAI\nclient = ZhipuAI(api_key=\"\") # \u8bf7\u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\nresponse = client.chat.completions.create(\n    model=\"\",  # \u586b\u5199\u9700\u8981\u8c03\u7528\u7684\u6a21\u578b\u540d\u79f0\n    messages=[\n        {\"role\": \"system\", \"content\": \"\u4f60\u662f\u4e00\u4e2a\u4eba\u5de5\u667a\u80fd\u52a9\u624b\uff0c\u4f60\u53eb\u53ebchatGLM\"},\n        {\"role\": \"user\", \"content\": \"\u4f60\u597d\uff01\u4f60\u53eb\u4ec0\u4e48\u540d\u5b57\"},\n    ],\n    stream=True,\n)\nfor chunk in response:\n    print(chunk.choices[0].delta)\n```\n\n### \u591a\u6a21\u6001\n```python\n\n\n# Function to encode the image\ndef encode_image(image_path):\n    import base64\n    with open(image_path, \"rb\") as image_file:\n        return base64.b64encode(image_file.read()).decode('utf-8')\n\n\ndef test_completions_vis():\n  client = ZhipuAI()  # \u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\n  base64_image  = encode_image(\"img/MetaGLM.png\")\n  response = client.chat.completions.create(\n    model=\"glm-4v\",  # \u586b\u5199\u9700\u8981\u8c03\u7528\u7684\u6a21\u578b\u540d\u79f0\n    extra_body={\"temperature\": 0.5, \"max_tokens\": 50},\n    messages=[\n      {\n        \"role\": \"user\",\n        \"content\": [\n          {\n            \"type\": \"text\",\n            \"text\": \"\u56fe\u91cc\u6709\u4ec0\u4e48\"\n          },\n\n          # {\n          #     \"type\": \"image_url\",\n          #     \"image_url\": {\n          #         \"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\"\n          #     }\n          # },\n          {\n            \"type\": \"image_url\",\n            \"image_url\": {\n              \"url\": f\"data:image/jpeg;base64,{base64_image}\"\n            }\n          }\n        ]\n      }\n    ]\n  )\n  print(response)\n\ntest_completions_vis()\n```\n\n### \u89d2\u8272\u626e\u6f14\n> \u63d0\u4f9b\u80fd\u529b\u7684\u6a21\u578b\u540d\u79f0\uff0c\u8bf7\u4ece\u5b98\u7f51\u83b7\u53d6\n```python\n\ndef test_completions_charglm():\n    client = ZhipuAI()  # \u8bf7\u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\n    response = client.chat.completions.create(\n        model=\"charglm-3\",  # \u586b\u5199\u9700\u8981\u8c03\u7528\u7684\u6a21\u578b\u540d\u79f0\n        messages=[\n            {\n                \"role\": \"user\",\n                \"content\": \"\u8bf7\u95ee\u4f60\u5728\u505a\u4ec0\u4e48\"\n            }\n        ],\n        meta={\n          \"user_info\": \"\u6211\u662f\u9646\u661f\u8fb0\uff0c\u662f\u4e00\u4e2a\u7537\u6027\uff0c\u662f\u4e00\u4f4d\u77e5\u540d\u5bfc\u6f14\uff0c\u4e5f\u662f\u82cf\u68a6\u8fdc\u7684\u5408\u4f5c\u5bfc\u6f14\u3002\u6211\u64c5\u957f\u62cd\u6444\u97f3\u4e50\u9898\u6750\u7684\u7535\u5f71\u3002\u82cf\u68a6\u8fdc\u5bf9\u6211\u7684\u6001\u5ea6\u662f\u5c0a\u656c\u7684\uff0c\u5e76\u89c6\u6211\u4e3a\u826f\u5e08\u76ca\u53cb\u3002\",\n          \"bot_info\": \"\u82cf\u68a6\u8fdc\uff0c\u672c\u540d\u82cf\u8fdc\u5fc3\uff0c\u662f\u4e00\u4f4d\u5f53\u7ea2\u7684\u56fd\u5185\u5973\u6b4c\u624b\u53ca\u6f14\u5458\u3002\u5728\u53c2\u52a0\u9009\u79c0\u8282\u76ee\u540e\uff0c\u51ed\u501f\u72ec\u7279\u7684\u55d3\u97f3\u53ca\u51fa\u4f17\u7684\u821e\u53f0\u9b45\u529b\u8fc5\u901f\u6210\u540d\uff0c\u8fdb\u5165\u5a31\u4e50\u5708\u3002\u5979\u5916\u8868\u7f8e\u4e3d\u52a8\u4eba\uff0c\u4f46\u771f\u6b63\u7684\u9b45\u529b\u5728\u4e8e\u5979\u7684\u624d\u534e\u548c\u52e4\u594b\u3002\u82cf\u68a6\u8fdc\u662f\u97f3\u4e50\u5b66\u9662\u6bd5\u4e1a\u7684\u4f18\u79c0\u751f\uff0c\u5584\u4e8e\u521b\u4f5c\uff0c\u62e5\u6709\u591a\u9996\u70ed\u95e8\u539f\u521b\u6b4c\u66f2\u3002\u9664\u4e86\u97f3\u4e50\u65b9\u9762\u7684\u6210\u5c31\uff0c\u5979\u8fd8\u70ed\u8877\u4e8e\u6148\u5584\u4e8b\u4e1a\uff0c\u79ef\u6781\u53c2\u52a0\u516c\u76ca\u6d3b\u52a8\uff0c\u7528\u5b9e\u9645\u884c\u52a8\u4f20\u9012\u6b63\u80fd\u91cf\u3002\u5728\u5de5\u4f5c\u4e2d\uff0c\u5979\u5bf9\u5f85\u5de5\u4f5c\u975e\u5e38\u656c\u4e1a\uff0c\u62cd\u620f\u65f6\u603b\u662f\u5168\u8eab\u5fc3\u6295\u5165\u89d2\u8272\uff0c\u8d62\u5f97\u4e86\u4e1a\u5185\u4eba\u58eb\u7684\u8d5e\u8a89\u548c\u7c89\u4e1d\u7684\u559c\u7231\u3002\u867d\u7136\u5728\u5a31\u4e50\u5708\uff0c\u4f46\u5979\u59cb\u7ec8\u4fdd\u6301\u4f4e\u8c03\u3001\u8c26\u900a\u7684\u6001\u5ea6\uff0c\u6df1\u5f97\u540c\u884c\u5c0a\u91cd\u3002\u5728\u8868\u8fbe\u65f6\uff0c\u82cf\u68a6\u8fdc\u559c\u6b22\u4f7f\u7528\u201c\u6211\u4eec\u201d\u548c\u201c\u4e00\u8d77\u201d\uff0c\u5f3a\u8c03\u56e2\u961f\u7cbe\u795e\u3002\",\n          \"bot_name\": \"\u82cf\u68a6\u8fdc\",\n          \"user_name\": \"\u9646\u661f\u8fb0\"\n        },\n    )\n    print(response)\ntest_completions_charglm()\n```\n\n\n### \u667a\u80fd\u4f53 \n```python\n\ndef test_assistant() -> None: \n  client = ZhipuAI()  # \u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\n \n\n  generate = client.assistant.conversation(\n    assistant_id=\"659e54b1b8006379b4b2abd6\",\n    model=\"glm-4-assistant\",\n    messages=[\n      {\n        \"role\": \"user\",\n        \"content\": [{\n          \"type\": \"text\",\n          \"text\": \"\u5e2e\u6211\u641c\u7d22\u4e0b\u667a\u8c31\u7684cogvideox\u53d1\u5e03\u65f6\u95f4\"\n        }]\n      }\n    ],\n    stream=True,\n    attachments=None,\n    metadata=None,\n    request_id=\"request_1790291013237211136\",\n    user_id=\"12345678\"\n  )\n  for assistant in generate:\n    print(assistant)\n\ntest_assistant()\n```\n\n### \u89c6\u9891\u751f\u6210 \n```python\n\n\ndef test_videos(): \n  client = ZhipuAI()  # \u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\n  try:\n    response = client.videos.generations(\n      model=\"cogvideo\",\n      prompt=\"\u4e00\u4e2a\u5f00\u8239\u7684\u4eba\",\n\n      user_id=\"1212222\"\n    )\n    print(response)\n    \ntest_videos()\n```\n\n\n\n### \u5f02\u5e38\u5904\u7406\n\n\u6a21\u5757\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7edf\u4e00\u7684\u53c2\u6570\u8fd4\u56de(\u4f8b\u5982:\u54cd\u5e94\u9519\u8bef\uff0c\u7f51\u7edc\u8d85\u65f6\u9519\u8bef)\n\n\u4e1a\u52a1\u5b9a\u4e49\u4e86http\u9519\u8bef\u7684\u54cd\u5e94\u7c7b (\u5728\u63a5\u53e3\u8fd4\u56de\uff0c40x\u6216\u800550x), \u4f1a\u629b\u51fa `zhipuai.APIStatusError`  ,\u5305\u542b `status_code` \u548c `response` \u5c5e\u6027. \u5b83\u4eec\u90fd\u662f\u7ee7\u627f `zhipuai.APIStatusError`.\n\u5176\u5b83Exception\uff0c\u5c5e\u4e8e\u4e0d\u53ef\u9884\u77e5\u7684\u9519\u8bef\n```python\nfrom zhipuai import ZhipuAI\nimport zhipuai\nclient = ZhipuAI()  # \u586b\u5199\u60a8\u81ea\u5df1\u7684APIKey\ntry:\n  response = client.chat.completions.create(\n    model=\"glm-4\",  # \u586b\u5199\u9700\u8981\u8c03\u7528\u7684\u6a21\u578b\u540d\u79f0\n    messages=[\n      {\"role\": \"user\", \"content\": \"\u4f5c\u4e3a\u4e00\u540d\u8425\u9500\u4e13\u5bb6\uff0c\u8bf7\u4e3a\u6211\u7684\u4ea7\u54c1\u521b\u4f5c\u4e00\u4e2a\u5438\u5f15\u4eba\u7684slogan\"},\n      {\"role\": \"assistant\", \"content\": \"\u5f53\u7136\uff0c\u4e3a\u4e86\u521b\u4f5c\u4e00\u4e2a\u5438\u5f15\u4eba\u7684slogan\uff0c\u8bf7\u544a\u8bc9\u6211\u4e00\u4e9b\u5173\u4e8e\u60a8\u4ea7\u54c1\u7684\u4fe1\u606f\"},\n      {\"role\": \"user\", \"content\": \"\u667a\u8c31AI\u5f00\u653e\u5e73\u53f0\"},\n      {\"role\": \"assistant\", \"content\": \"\u667a\u542f\u672a\u6765\uff0c\u8c31\u7ed8\u65e0\u9650\u4e00\u667a\u8c31AI\uff0c\u8ba9\u521b\u65b0\u89e6\u624b\u53ef\u53ca!\"},\n      {\"role\": \"user\", \"content\": \"\u521b\u9020\u4e00\u4e2a\u66f4\u7cbe\u51c6\u3001\u5438\u5f15\u4eba\u7684slogan\"}\n    ]\n  )\n  print(response)\n \nexcept zhipuai.APIStatusError as err:\n  print(err) \nexcept zhipuai.APITimeoutError as err:\n  print(err) \n```\n\nError codes are as followed:\n\n| Status Code | Error Type                 |\n|-------------| -------------------------- |\n| 400         | `APIRequestFailedError`          |\n| 401         | `APIAuthenticationError`      |\n| 429         | `APIReachLimitError`           |\n| 500         | `APIInternalError`      |\n| 503         | `APIServerFlowExceedError`      |\n| N/A         | `APIStatusError`       |\n\n\n\n### \u66f4\u65b0\u65e5\u5fd7\n\n`2024-8-12`  \n- \u4fee\u6539\u89c6\u9891\u63d0\u793a\u8bcd\u53ef\u9009,\u589e\u52a0\u6587\u4ef6\u5220\u9664\n- Assistant\u4e1a\u52a1\n- embedding 3 fix dimensions\n  \n`2024-7-25`  \n- cogvideo \u4fee\u590d\n  \n`2024-7-12` \n- \u9ad8\u7ea7\u641c\u7d22\u5de5\u5177 Web search \u4e1a\u52a1 \n- specified Python versions (3.8, 3.9, 3.10, 3.11, 3.12) \n- cogvideo \u4e1a\u52a1\u96c6\u6210\n  \n`2024-5-20` \n- \u4e00\u4e9b `python3.12` \u7684\u4f9d\u8d56\u95ee\u9898\uff0c \n- \u589e\u52a0\u5206\u9875\u5904\u7406\u4ee3\u7801\uff0c\u91cd\u5199\u90e8\u5206\u76f8\u5e94\u7c7b\u7684\u5b9e\u4f8b\u5316\u89c4\u5219\n- \u589e\u52a0\u7c7b\u578b\u8f6c\u6362\u6821\u9a8c\n- \u6279\u5904\u7406\u4efb\u52a1\u76f8\u5173api \n- \u6587\u4ef6\u6d41\u54cd\u5e94\u5305\u88c5\u5668   \n\n`2024-4-29` \n- \u4e00\u4e9b `python3.7` \u7684\u4ee3\u7801\u9002\u914d\u95ee\u9898\uff0c \n- \u63a5\u53e3\u5931\u8d25\u91cd\u8bd5\u673a\u5236\uff0c\u901a\u8fc7 `retry` \u53c2\u6570\u63a7\u5236\u91cd\u8bd5\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a3\u6b21\n- \u63a5\u53e3\u8d85\u65f6\u7b56\u7565\u8c03\u6574\uff0c\u901a\u8fc7 `Timeout` \u63a7\u5236\u63a5\u53e3`connect` \u548c `read` \u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a`timeout=300.0, connect=8.0`\n- \u5bf9\u8bdd\u6a21\u5757\u589e\u52a0\u8d85\u62df\u4eba\u5927\u6a21\u578b\u53c2\u6570\u652f\u6301\uff0c`model=\"charglm-3\"`, `meta`\u53c2\u6570\u652f\u6301\n  \n`2024-4-23` \n- \u4e00\u4e9b\u517c\u5bb9 `pydantic<3,>=1.9.0 ` \u7684\u4ee3\u7801\uff0c\n- \u62a5\u6587\u5904\u7406\u7684\u4e1a\u52a1\u8bf7\u6c42\u53c2\u6570\u548c\u54cd\u5e94\u53c2\u6570\u53ef\u901a\u8fc7\u914d\u7f6e\u6269\u5145\n- \u517c\u5bb9\u4e86\u4e00\u4e9b\u53c2\u6570 `top_p:1`,`temperture:0`(do_sample\u91cd\u5199false\uff0c\u53c2\u6570top_p temperture\u4e0d\u751f\u6548)\n- \u56fe\u50cf\u7406\u89e3\u90e8\u5206\uff0c  image_url\u53c2\u6570base64\u5185\u5bb9\u5305\u542b `data:image/jpeg;base64`\u517c\u5bb9\n- \u5220\u9664jwt\u8ba4\u8bc1\u903b\u8f91\n  \n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A SDK library for accessing big model apis from ZhipuAI",
    "version": "2.1.5.20230904",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "10285c1efbfb560e64458b22e0442279af8bde673edcb096762a3aeccee3a8ec",
                "md5": "c6f6a985f8024db1f0a7bf57f9250985",
                "sha256": "8485ca452c2f07fea476fb0666abc8fbbdf1b2e4feeee46a3bb3c1a2b51efccd"
            },
            "downloads": -1,
            "filename": "zhipuai-2.1.5.20230904-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c6f6a985f8024db1f0a7bf57f9250985",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*,>=3.8",
            "size": 104958,
            "upload_time": "2024-09-04T09:20:42",
            "upload_time_iso_8601": "2024-09-04T09:20:42.943072Z",
            "url": "https://files.pythonhosted.org/packages/10/28/5c1efbfb560e64458b22e0442279af8bde673edcb096762a3aeccee3a8ec/zhipuai-2.1.5.20230904-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d51481aaac7a2aef3273283bf8f1569dab44ca11f0052d9782b7905572575ac3",
                "md5": "30160df5281fbaf20e09fb1ab72b6b5f",
                "sha256": "2c19dd796b12e2f19b93d8f9be6fd01e85d3320737a187ebf3c75a9806a7c2b5"
            },
            "downloads": -1,
            "filename": "zhipuai-2.1.5.20230904.tar.gz",
            "has_sig": false,
            "md5_digest": "30160df5281fbaf20e09fb1ab72b6b5f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*,>=3.8",
            "size": 68035,
            "upload_time": "2024-09-04T09:20:44",
            "upload_time_iso_8601": "2024-09-04T09:20:44.149511Z",
            "url": "https://files.pythonhosted.org/packages/d5/14/81aaac7a2aef3273283bf8f1569dab44ca11f0052d9782b7905572575ac3/zhipuai-2.1.5.20230904.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-04 09:20:44",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "zhipuai"
}
        
Elapsed time: 0.31975s