ChatZhipuAI()Model name to use, see 'https://open.bigmodel.cn/dev/api#language'. Alternatively, you can use any fine-tuned model from the GLM series.
ZhipuAI chat model integration.
Setup:
Install PyJWT and set environment variable ZHIPUAI_API_KEY
.. code-block:: bash
pip install pyjwt
export ZHIPUAI_API_KEY="your-api-key"
Key init args — completion params: model: Optional[str] Name of ZhipuAI model to use. temperature: float Sampling temperature. max_tokens: Optional[int] Max number of tokens to generate.
Key init args — client params: api_key: Optional[str] ZhipuAI API key. If not passed in will be read from env var ZHIPUAI_API_KEY. api_base: Optional[str] Base URL for API requests.
See full list of supported init args and their descriptions in the params section.
Instantiate:
.. code-block:: python
from langchain_community.chat_models import ChatZhipuAI
zhipuai_chat = ChatZhipuAI( temperature=0.5, api_key="your-api-key", model="glm-4", # api_base="...", # other params... )
Invoke:
.. code-block:: python
messages = [
("system", "你是一名专业的翻译家,可以将用户的中文翻译为英文。"),
("human", "我喜欢编程。"),
]
zhipuai_chat.invoke(messages)
.. code-block:: python
AIMessage(content='I enjoy programming.', response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 23, 'total_tokens': 29}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-c5d9af91-55c6-470e-9545-02b2fa0d7f9d-0')
Stream:
.. code-block:: python
for chunk in zhipuai_chat.stream(messages):
print(chunk)
.. code-block:: python
content='I' id='run-4df71729-618f-4e2b-a4ff-884682723082'
content=' enjoy' id='run-4df71729-618f-4e2b-a4ff-884682723082'
content=' programming' id='run-4df71729-618f-4e2b-a4ff-884682723082'
content='.' id='run-4df71729-618f-4e2b-a4ff-884682723082'
content='' response_metadata={'finish_reason': 'stop'} id='run-4df71729-618f-4e2b-a4ff-884682723082'
.. code-block:: python
stream = zhipuai_chat.stream(messages)
full = next(stream)
for chunk in stream:
full += chunk
full
.. code-block::
AIMessageChunk(content='I enjoy programming.', response_metadata={'finish_reason': 'stop'}, id='run-20b05040-a0b4-4715-8fdc-b39dba9bfb53')
Async:
.. code-block:: python
await zhipuai_chat.ainvoke(messages)
# stream:
# async for chunk in zhipuai_chat.astream(messages):
# print(chunk)
# batch:
# await zhipuai_chat.abatch([messages])
.. code-block:: python
[AIMessage(content='I enjoy programming.', response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 23, 'total_tokens': 29}, 'model_name': 'glm-4', 'finish_reason': 'stop'}, id='run-ba06af9d-4baa-40b2-9298-be9c62aa0849-0')]
Tool calling:
.. code-block:: python
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
'''Get the current weather in a given location'''
location: str = Field(
..., description="The city and state, e.g. San Francisco, CA"
)
class GetPopulation(BaseModel):
'''Get the current population in a given location'''
location: str = Field(
..., description="The city and state, e.g. San Francisco, CA"
)
chat_with_tools = zhipuai_chat.bind_tools([GetWeather, GetPopulation])
ai_msg = chat_with_tools.invoke(
"Which city is hotter today and which is bigger: LA or NY?"
)
ai_msg.tool_calls
.. code-block:: python
[
{
'name': 'GetWeather',
'args': {'location': 'Los Angeles, CA'},
'id': 'call_202408222146464ea49ec8731145a9',
'type': 'tool_call'
}
]
Structured output:
.. code-block:: python
from typing import Optional
from pydantic import BaseModel, Field
class Joke(BaseModel):
'''Joke to tell user.'''
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10")
structured_chat = zhipuai_chat.with_structured_output(Joke)
structured_chat.invoke("Tell me a joke about cats")
.. code-block:: python
Joke(setup='What do cats like to eat for breakfast?', punchline='Mice Krispies!', rating=None)
Response metadata .. code-block:: python
ai_msg = zhipuai_chat.invoke(messages)
ai_msg.response_metadata
.. code-block:: python
{'token_usage': {'completion_tokens': 6,
'prompt_tokens': 23,
'total_tokens': 29},
'model_name': 'glm-4',
'finish_reason': 'stop'}