OpenAI Model Migration (Relocation Plan)

This guide explains how to quickly migrate from OpenAI models to ZhipuAI, using examples to help you complete the migration process. Additionally, ZhipuAI offers a “Special Relocation Plan” for OpenAI API users, making it easy to switch to domestic large models. Get the benefits now

Relocation Plan
Special Benefits 150 million Tokens (50 million GLM-4 and 100 million GLM-4-Air); Series migration training from OpenAI to GLM;
High Usage Benefits OpenAI-equivalent Token giveaway plan (no upper limit); OpenAI-equivalent concurrency scale; Matching high-level membership policies, up to 60% off; Exclusive relocation consultants and 5-person/day technical expert support; Assistance with filing and training.

Basic Toolkit

The OpenAI SDK provides a ready-to-use tool for calling, and we have backend-compatible with all OpenAI endpoints, offering a convenient migration method. You only need to replace the api_key and base_url to use our models.

Switch API Endpoint

from openai import OpenAI 
 
client = OpenAI(
    api_key="your zhipuai api key",
    base_url="https://open.bigmodel.cn/api/paas/v4/"
) 
 
response = client.chat.completions.create(
    model="glm-4",  
    messages=[    
        {"role": "system", "content": "You are a smart and creative novel writer"},    
        {"role": "user", "content": "Please write a short fairy tale as the king of fairy tales."} 
    ],
    top_p=0.7,
    temperature=0.9
 ) 
 
 print(response.choices[0].message)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Use ZhipuAI Official SDK

Some features of ZhipuAI require you to call through the official SDK, which you can install via pypi.

pip install zhipuai
1

Example Calls

from zhipuai import ZhipuAI
client = ZhipuAI(api_key="") # Fill in your own APIKey
response = client.chat.completions.create(
    model="glm-4",  # Fill in the model name you need to call
    messages=[
        {"role": "user", "content": "As a marketing expert, please create an attractive slogan for the ZhipuAI open platform"},
        {"role": "assistant", "content": "Of course, to create an attractive slogan, please tell me some information about your product"},
        {"role": "user", "content": "ZhipuAI Open Platform"},
        {"role": "assistant", "content": "Intelligence for the future, infinite drawing - ZhipuAI, making innovation within reach!"},
        {"role": "user", "content": "Create a more precise and attractive slogan"}
    ],
)
print(response.choices[0].message)
1
2
3
4
5
6
7
8
9
10
11
12
13

Open Source Frameworks

In terms of agent and related task framework migration, we support the quick migration of the langchain framework. We provide the langchain-zhipuai extension tool, offering support adaptation for BaseChatModel and Embeddings. LangChain users can quickly complete the migration using the following examples:

Install langchain-zhipuai

Download link: https://github.com/MetaGLM/langchain-zhipuai/releases

Before using, please set the environment variable ZHIPUAI_API_KEY to your ZhipuAI API Key.

Migrate to ChatZhipuAI

from langchain_zhipuai.agents.zhipuai_all_tools.base import _get_assistants_tool
from langchain_zhipuai.chat_models import ChatZhipuAI
from langchain.agents import tool
from langchain.tools.shell import ShellTool
from pydantic.v1 import BaseModel, Extra, Field
from langchain import hub
from langchain_zhipuai.agents.all_tools_bind.base import create_zhipuai_tools_agent
from langchain_zhipuai.agent_toolkits import BaseToolOutput
from langchain_zhipuai.agents.all_tools_agent import ZhipuAiAllToolsAgentExecutor
 
 
@tool
def shell(query: str = Field(description="The command to execute")):
    """Use Shell to execute system shell commands"""
    tool = ShellTool()
    return BaseToolOutput(tool.run(tool_input=query))
 
llm = ChatZhipuAI(api_key="") # You can specify the apikey here
 
tools = [
    _get_assistants_tool(shell),
    {"type": "code_interpreter", "code_interpreter": {"sandbox": "none"}},
    {"type": "web_browser"},
    {"type": "drawing_tool"},
]
llm_with_all_tools = llm.bind(
    tools=tools
)
 
prompt = hub.pull("zhipuai-all-tools-chat/zhipuai-all-tools-agent")
agent = create_zhipuai_tools_agent(
    prompt=prompt, llm_with_all_tools=llm_with_all_tools
)
 
agent_executor = ZhipuAiAllToolsAgentExecutor(
    agent=agent,
    tools=[shell] ,
    verbose=True,
    return_intermediate_steps=True,
)
 
agent_executor.invoke(
    {
        "input": "Hello",
        "chat_history": [],
    }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

Compatible with Vector Databases

We provide a compatible Embedding calling method with OpenAI. When using Embeddings related to vector libraries, you only need to replace the Embedding instance with ZhipuAIEmbeddings.

from langchain_zhipuai.embeddings.base import ZhipuAIEmbeddings
 
"""Test zhipuai embeddings."""
documents = ["foo bar"]
embedding = ZhipuAIEmbeddings()
output = embedding.embed_documents(documents)
# len(output) == 1
# len(output[0]) == 1024
1
2
3
4
5
6
7
8