Skip to main content

将智谱AI集成到LangChain

Project description

为什么要开发这个包?

PyPI version

为了方便在 langchain 中使用,在官方SDK基础上做了如下额外工作:

  • 问题1: 智谱AI的官方SDK使用了 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", "glm-4v"
  • 逻辑推理和对话生成
  • 支持工具回调

简单的例子

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)

使用glm-4v

from langchain_zhipu import ChatZhipuAI
from langchain_core.prompts import ChatPromptTemplate

llm4v = ChatZhipuAI(model="glm-4v")

prompt = ChatPromptTemplate.from_messages([
    ("human", [
          {
            "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"
            }
          }
        ]),
])

(prompt|llm4v).invoke({})

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

官方SDK PyPI version

官方SDK能力简介

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

Project details


Download files

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

Source Distribution

langchain_zhipu-4.0.9.tar.gz (19.8 kB view hashes)

Uploaded Source

Built Distribution

langchain_zhipu-4.0.9-py3-none-any.whl (31.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page