为什么要开发这个包?
为了方便在 langchain 中使用,langchain_zhipu 直接使用官方 HTTP 接口实现,并避免了如下的现存问题:
- 问题 1: 智谱 AI 的官方 SDK 使用了 pydantic v2,这与 langchain(尤其是 langserve)不兼容
- 问题 2: langchain.community 的国内包更新不及时,无法在 langchain 的 LCEL 语法中使用
能力支持
已支持全部 langchain 接口
- invoke
- ainvoke
- batch
- abatch
- stream
- astream
- astream_events
- asteram_log
已支持模型能力
- 已支持生成模型:"glm-3-turbo", "glm-4", "glm-4v"
- 已支持向量模型:"embedding-2"
- 已支持官方知识库管理能力:对知识库、文档、应用做增删改查
- 已支持基于官方知识库的大模型对话
- 支持工具回调:普通工具,以及在线知识库和网络搜索
- 支持智能体
- 支持 RAG
使用
配置
可以将申请到的 API_KEY 配置到环境变量 ZHIPUAI_API_KEY。
建议使用 .env 文件来管理环境变量,这需要安装 python_dotenv 包:
pip install python_dotenv
你的 .env 文件:
ZHIPUAI_API_KEY="你的KEY"
然后在你的代码目录中:
# 加载 .env 到环境变量
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(), override=True)
安装 langchain_zhipu
pip install langchain langchain_zhipu
其中,langchain 只要 v0.1.0 ,而 langchain_zhipu 最好安装最新的 4.1.x 版本。
代码例子
- 基本用法 usage.ipynb
- 智能体 agent.ipynb
- 向量模型 embedding.ipynb
- 模型统计 tokens.ipynb
- 知识库 knowledge.ipynb
- 知识库应用 knowledge_app.ipynb
- textlong+智谱
官方接口指南 智谱开放平台
简单的例子
from langchain_zhipu 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)
retrieval 工具
from langchain_zhipu import convert_to_retrieval_tool
llm.bind(tools=[convert_to_retrieval_tool(knowledge_id="1772979648448397312")]).invoke("你知道马冬梅住哪里吗?")
web_search 工具
from langchain_zhipu import convert_to_web_search_tool
llm.bind(tools=[convert_to_web_search_tool(search_query="周星驰电影")]).invoke("哪部电影好看?")
function 工具
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
@tool
def search(query: str) -> str:
"""查询 langchan 资料; args: query 类型为字符串,描述用户的问题."""
return "langchain_chinese 是一个为中国大模型优化的langchain模块"
llm.bind(tools=[convert_to_openai_tool(search)]).invoke("langchain_chinese是啥?请查询本地资料回答。")
使用 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({})
Metadata
Release files for langchain_zhipu 4.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_zhipu-4.1.8.tar.gz | 16.2 kB | Without provenance | Details for langchain_zhipu-4.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_zhipu-4.1.8-py3-none-any.whl | Python 3 | none | any | Details for langchain_zhipu-4.1.8-py3-none-any.whl |
Total release size: 35.1 kB
Release files / langchain_zhipu-4.1.8.tar.gz
| Download URL | langchain_zhipu-4.1.8.tar.gz |
|---|---|
| Size | 16.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
78bc18cb049a7ec27505a76015e4e7a59abf06c0a3e98a1863802dccfb890cd7
|
|
BLAKE2b-256 checksum How to use checksums |
9b3f2e27b1e7d675ec34461cea2c2309ea52c1122eafc921c0da8742fd6beaab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.9 Darwin/23.5.0
|
Release files / langchain_zhipu-4.1.8-py3-none-any.whl
| Download URL | langchain_zhipu-4.1.8-py3-none-any.whl |
|---|---|
| Size | 18.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c1e7aa7d17ad9eb9e21c110998c2b02f57d5d0dfed452ee4501e4dec9e20197a
|
|
BLAKE2b-256 checksum How to use checksums |
82ef088fb4967c8a09506072d3cfd3ee1c56b8c01ec21c65f4c3454040488057
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.9 Darwin/23.5.0
|
Security for langchain_zhipu 4.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.