zhipuai_pydantic_v1


Namezhipuai_pydantic_v1 JSON
Version 4.0.4 PyPI version JSON
download
home_pagehttps://github.com/arcstep/zhipuai-sdk-python-v4
Summarydowngrade zhipu api to pydantic v1 and prepare for langchain
upload_time2024-02-22 13:07:00
maintainer
docs_urlNone
authorarcstep
requires_python>=3.9,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 为什么要开发这个包?
[![PyPI version](https://img.shields.io/pypi/v/zhipuai_pydantic_v1.svg)](https://pypi.org/project/zhipuai_pydantic_v1/)

为了在 langchain 中可以方便使用,做了如下额外工作:

- 问题1: 智谱AI的官方包使用了 pydantic v2,这与 langchain(尤其是langserve)不兼容
- 问题2: langchain_community 的国内包更新不及时,无法在 langchain 的 LCEL 语法中使用

## 已支持全部 langchain 接口

1. invoke
2. ainvoke
3. batch
4. abatch
5. stream
6. astream
7. astream_events
8. asteram_log

## 已支持模型能力

- 模型名称:"glm-3-turbo", "glm-4"
- 逻辑推理和对话生成
- 支持工具回调

## 使用举例

```python
from zhipuai_pydantic_v1 import ChatZhipuAI
llm = ChatZhipuAI()

# invoke
llm.invoke("hi")

# stream
for s in llm.stream("hi"):
  print(s)

# astream
async for s in llm.astream("hi"):
  print(s)
```

------------------------------------------
**以下是 zhipuai v2.0.1 的原有文档**
[![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

# 智谱大模型开放接口SDK

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

## 安装


- 运行环境: [**Python>=3.7**](https://www.python.org/)

- 使用 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

```python
from zhipuai import ZhipuAI

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

### 同步调用

```python
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="") # 填写您自己的APIKey
response = client.chat.completions.create(
    model="",  # 填写需要调用的模型名称
    messages=[
        {"role": "user", "content": "你好"},
        {"role": "assistant", "content": "我是人工智能助手"},
        {"role": "user", "content": "你叫什么名字"},
        {"role": "assistant", "content": "我叫chatGLM"},
        {"role": "user", "content": "你都可以做些什么事"}
    ],
)
print(response.choices[0].message)
```

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

### 异常处理

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`.

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


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/arcstep/zhipuai-sdk-python-v4",
    "name": "zhipuai_pydantic_v1",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "arcstep",
    "author_email": "43801@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/92/00/80993fa48de92064f33539ff3a2653ccf210ef03b974ca2a3216566abea0/zhipuai_pydantic_v1-4.0.4.tar.gz",
    "platform": null,
    "description": "# \u4e3a\u4ec0\u4e48\u8981\u5f00\u53d1\u8fd9\u4e2a\u5305\uff1f\n[![PyPI version](https://img.shields.io/pypi/v/zhipuai_pydantic_v1.svg)](https://pypi.org/project/zhipuai_pydantic_v1/)\n\n\u4e3a\u4e86\u5728 langchain \u4e2d\u53ef\u4ee5\u65b9\u4fbf\u4f7f\u7528\uff0c\u505a\u4e86\u5982\u4e0b\u989d\u5916\u5de5\u4f5c\uff1a\n\n- \u95ee\u98981: \u667a\u8c31AI\u7684\u5b98\u65b9\u5305\u4f7f\u7528\u4e86 pydantic v2\uff0c\u8fd9\u4e0e langchain\uff08\u5c24\u5176\u662flangserve\uff09\u4e0d\u517c\u5bb9\n- \u95ee\u98982: langchain_community \u7684\u56fd\u5185\u5305\u66f4\u65b0\u4e0d\u53ca\u65f6\uff0c\u65e0\u6cd5\u5728 langchain \u7684 LCEL \u8bed\u6cd5\u4e2d\u4f7f\u7528\n\n## \u5df2\u652f\u6301\u5168\u90e8 langchain \u63a5\u53e3\n\n1. invoke\n2. ainvoke\n3. batch\n4. abatch\n5. stream\n6. astream\n7. astream_events\n8. asteram_log\n\n## \u5df2\u652f\u6301\u6a21\u578b\u80fd\u529b\n\n- \u6a21\u578b\u540d\u79f0\uff1a\"glm-3-turbo\", \"glm-4\"\n- \u903b\u8f91\u63a8\u7406\u548c\u5bf9\u8bdd\u751f\u6210\n- \u652f\u6301\u5de5\u5177\u56de\u8c03\n\n## \u4f7f\u7528\u4e3e\u4f8b\n\n```python\nfrom zhipuai_pydantic_v1 import ChatZhipuAI\nllm = ChatZhipuAI()\n\n# invoke\nllm.invoke(\"hi\")\n\n# stream\nfor s in llm.stream(\"hi\"):\n  print(s)\n\n# astream\nasync for s in llm.astream(\"hi\"):\n  print(s)\n```\n\n------------------------------------------\n**\u4ee5\u4e0b\u662f zhipuai v2.0.1 \u7684\u539f\u6709\u6587\u6863**\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# \u667a\u8c31\u5927\u6a21\u578b\u5f00\u653e\u63a5\u53e3SDK\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- \u8fd0\u884c\u73af\u5883\uff1a [**Python>=3.7**](https://www.python.org/)\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\n\n```python\nfrom zhipuai import ZhipuAI\n\nclient = ZhipuAI(\n    api_key=\"\", # \u586b\u5199\u60a8\u7684 APIKey\n) \n```\n\n### \u540c\u6b65\u8c03\u7528\n\n```python\nfrom zhipuai import ZhipuAI\nclient = ZhipuAI(api_key=\"\") # \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\": \"user\", \"content\": \"\u4f60\u597d\"},\n        {\"role\": \"assistant\", \"content\": \"\u6211\u662f\u4eba\u5de5\u667a\u80fd\u52a9\u624b\"},\n        {\"role\": \"user\", \"content\": \"\u4f60\u53eb\u4ec0\u4e48\u540d\u5b57\"},\n        {\"role\": \"assistant\", \"content\": \"\u6211\u53ebchatGLM\"},\n        {\"role\": \"user\", \"content\": \"\u4f60\u90fd\u53ef\u4ee5\u505a\u4e9b\u4ec0\u4e48\u4e8b\"}\n    ],\n)\nprint(response.choices[0].message)\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### \u5f02\u5e38\u5904\u7406\n\nWhen 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.\n\nWhen the API returns a non-success status code (that is, 4xx or 5xx\nresponse), a subclass of `openai.APIStatusError` is raised, containing `status_code` and `response` properties.\n\nAll errors inherit from `openai.APIError`.\n\n```python\nimport openai\nfrom openai import OpenAI\n\nclient = OpenAI()\n\ntry:\n    client.fine_tunes.create(\n        training_file=\"file-XGinujblHPwGLSztz8cPS8XY\",\n    )\nexcept openai.APIConnectionError as e:\n    print(\"The server could not be reached\")\n    print(e.__cause__)  # an underlying Exception, likely raised within httpx.\nexcept openai.RateLimitError as e:\n    print(\"A 429 status code was received; we should back off a bit.\")\nexcept openai.APIStatusError as e:\n    print(\"Another non-200-range status code was received\")\n    print(e.status_code)\n    print(e.response)\n```\n\nError codes are as followed:\n\n| Status Code | Error Type                 |\n| ----------- | -------------------------- |\n| 400         | `BadRequestError`          |\n| 401         | `AuthenticationError`      |\n| 403         | `PermissionDeniedError`    |\n| 404         | `NotFoundError`            |\n| 422         | `UnprocessableEntityError` |\n| 429         | `RateLimitError`           |\n| >=500       | `InternalServerError`      |\n| N/A         | `APIConnectionError`       |\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "downgrade zhipu api to pydantic v1 and prepare for langchain",
    "version": "4.0.4",
    "project_urls": {
        "Homepage": "https://github.com/arcstep/zhipuai-sdk-python-v4",
        "Repository": "https://github.com/arcstep/zhipuai-sdk-python-v4.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3e736f1c8fdf3fe4a5d99d4f4a55f346f602268206b57174bc9925e4d19f9287",
                "md5": "b0697022a86eccc7eb6f0b44ea6e1705",
                "sha256": "edbf574c264142fb32f6296f8c276ed5054e03950939d318eee59b53340ea028"
            },
            "downloads": -1,
            "filename": "zhipuai_pydantic_v1-4.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b0697022a86eccc7eb6f0b44ea6e1705",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4.0",
            "size": 32394,
            "upload_time": "2024-02-22T13:06:59",
            "upload_time_iso_8601": "2024-02-22T13:06:59.114241Z",
            "url": "https://files.pythonhosted.org/packages/3e/73/6f1c8fdf3fe4a5d99d4f4a55f346f602268206b57174bc9925e4d19f9287/zhipuai_pydantic_v1-4.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "920080993fa48de92064f33539ff3a2653ccf210ef03b974ca2a3216566abea0",
                "md5": "bbbb2eacac16ca71c0ca2a8411afd558",
                "sha256": "015cf876aa36cbc9395758217f4044121a4a9d3fd7dc2b32f51215baf291926a"
            },
            "downloads": -1,
            "filename": "zhipuai_pydantic_v1-4.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "bbbb2eacac16ca71c0ca2a8411afd558",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4.0",
            "size": 21459,
            "upload_time": "2024-02-22T13:07:00",
            "upload_time_iso_8601": "2024-02-22T13:07:00.873813Z",
            "url": "https://files.pythonhosted.org/packages/92/00/80993fa48de92064f33539ff3a2653ccf210ef03b974ca2a3216566abea0/zhipuai_pydantic_v1-4.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-22 13:07:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "arcstep",
    "github_project": "zhipuai-sdk-python-v4",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "zhipuai_pydantic_v1"
}
        
Elapsed time: 0.96031s