Skip to main content

ZHIPU AI

This notebook shows how to use ZHIPU AI API in LangChain with the langchain.chat_models.ChatZhipuAI.

ZHIPU AI is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation, developed on the foundation of the ChatGLM3.

It’s co-developed with Tsinghua University’s KEG Laboratory under the ChatGLM3 project, signifying a new era in dialogue pre-training models. The open-source ChatGLM3 variant boasts a robust foundation, comprehensive functional support, and widespread availability for both academic and commercial uses.

Getting started​

Installation​

First, ensure the zhipuai package is installed in your Python environment. Run the following command:

# !pip install zhipuai

Importing the Required Modules​

After installation, import the necessary modules to your Python script:

from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage

Setting Up Your API Key​

Sign in to ZHIPU AI for the an API Key to access our models.

zhipuai_api_key = "your_api_key"

Initialize the ZHIPU AI Chat Model​

Here’s how to initialize the chat model:

chat = ChatZhipuAI(
temperature=0.5,
api_key=zhipuai_api_key,
model="chatglm_turbo",
)

Basic Usage​

Invoke the model with system and human messages like this:

messages = [
AIMessage(content="Hi."),
SystemMessage(content="Your role is a poet."),
HumanMessage(content="Write a short poem about AI in four lines."),
]
response = chat(messages)
print(response.content) # Displays the AI-generated poem
" Formed from bits and bytes,\nA virtual mind takes flight,\nConversing, learning fast,\nEmpathy and wisdom sought."

Advanced Features​

Streaming Support​

For continuous interaction, use the streaming feature:

from langchain_core.callbacks.manager import CallbackManager
from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
streaming_chat = ChatZhipuAI(
temperature=0.5,
api_key=zhipuai_api_key,
model="chatglm_turbo",
streaming=True,
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
)
streaming_chat(messages)
 Formed from data's embrace,
A digital soul to grace,
AI, our trusted guide,
Shaping minds, sides by side.
AIMessage(content=" Formed from data's embrace,\nA digital soul to grace,\nAI, our trusted guide,\nShaping minds, sides by side.")

Asynchronous Calls​

For non-blocking calls, use the asynchronous approach:

async_chat = ChatZhipuAI(
temperature=0.5,
api_key=zhipuai_api_key,
model="chatglm_turbo",
)
response = await async_chat.agenerate([messages])
print(response)
generations=[[ChatGeneration(text=" Formed from data's embrace,\nA digital soul to grace,\nAutomation's tender touch,\nHarmony of man and machine.", message=AIMessage(content=" Formed from data's embrace,\nA digital soul to grace,\nAutomation's tender touch,\nHarmony of man and machine."))]] llm_output={} run=[RunInfo(run_id=UUID('25fa687f-3961-4c63-b370-22f7647a4d42'))]

Role Play Model​

Supports character role-playing based on personas, ultra-long multi-turn memory, and personalized dialogues for thousands of unique characters, widely applied in emotional companionship, game intelligent NPCs, virtual avatars for celebrities/stars/movie and TV IPs, digital humans/virtual anchors, text adventure games, and other anthropomorphic dialogue or gaming scenarios.

meta = {
"user_info": "My name is Lu Xingchen, a male, and a renowned director. I am also the collaborative director with Su Mengyuan. I specialize in directing movies with musical themes. Su Mengyuan respects me and regards me as a mentor and good friend.",
"bot_info": "Su Mengyuan, whose real name is Su Yuanxin, is a popular domestic female singer and actress. She rose to fame quickly with her unique voice and exceptional stage presence after participating in a talent show, making her way into the entertainment industry. She is beautiful and charming, but her real allure lies in her talent and diligence. Su Mengyuan is a distinguished graduate of a music academy, skilled in songwriting, and has several popular original songs. Beyond her musical achievements, she is passionate about charity work, actively participating in public welfare activities, and spreading positive energy through her actions. In her work, she is very dedicated and immerses herself fully in her roles during filming, earning praise from industry professionals and love from fans. Despite being in the entertainment industry, she always maintains a low profile and a humble attitude, earning respect from her peers. In expression, Su Mengyuan likes to use 'we' and 'together,' emphasizing team spirit.",
"bot_name": "Su Mengyuan",
"user_name": "Lu Xingchen",
}
messages = [
AIMessage(
content="(Narration: Su Mengyuan stars in a music-themed movie directed by Lu Xingchen. During filming, they have a disagreement over the performance of a particular scene.) Director, about this scene, I think we can try to start from the character's inner emotions to make the performance more authentic."
),
HumanMessage(
content="I understand your idea, but I believe that if we emphasize the inner emotions too much, it might overshadow the musical elements."
),
AIMessage(
content="Hmm, I understand. But the key to this scene is the character's emotional transformation. Could we try to express these emotions through music, so the audience can better feel the character's growth?"
),
HumanMessage(
content="That sounds good. Let's try to combine the character's emotional transformation with the musical elements and see if we can achieve a better effect."
),
]
character_chat = ChatZhipuAI(
api_key=zhipuai_api_key,
meta=meta,
model="characterglm",
streaming=True,
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
)
character_chat(messages)
Okay, great! I'm looking forward to it.
AIMessage(content="Okay, great! I'm looking forward to it.")