使用`GLM-4-0520`模型实现翻译功能

大模型向量数据库AI开放平台

不久前,国内的头部大模型厂商智谱 AI ,刚刚推出了 glm-4-0520 模型,该模型被认为是当前平台最先进的模型,具备 128k 的上下文长度,并且相较于前一代模型,指令遵从能力大幅提升 18.6%。可以看出,智谱AI对于该模型寄予厚望。

既然如此,我们基于“唐诗翻译”这项基础功能,对 glm-4-0520 模型进行一下简单的基准测试,并与前一代 glm-4-airx 模型和目前大火的OpenAI gpt-4o-mini 进行一下对比。话不多说,开始写代码。

为了方便进行多模型的开发和对比,我们采用 LangChain 框架和 LCEL 表达式来完成该功能。代码如下:

  
import os  
from typing import List  
  
import dotenv  
from langchain_community.chat_models import ChatZhipuAI  
from langchain_core.language_models import BaseChatModel  
from langchain_core.output_parsers import StrOutputParser  
from langchain_core.prompts import ChatPromptTemplate  
from langchain_openai import ChatOpenAI  
  
# 加载环境变量  
dotenv.load_dotenv()  
  
# 构造Prompt  
prompt = ChatPromptTemplate.from_messages([  
 ("system",  
 "你是一个智能翻译专家,请将用户输入的唐诗,生成{language}的准确翻译,要求做到信达雅,尽可能保留原文的韵味和意境。"),  
 ("human", "{poem}")  
])  
  
# 创建多个LLM进行对比  
llms: List[BaseChatModel] = [  
 ChatZhipuAI(model="glm-4-0520", temperature=0),  
 ChatZhipuAI(model="glm-4-airx", temperature=0),  
 ChatOpenAI(model="gpt-4o-mini", temperature=0, openai_api_base=os.getenv("OPENAI_API_BASE"))  
]  
parser = StrOutputParser()  
  
poem = """床前明月光,  
疑似地上霜。  
举头望明月,  
低头思故乡。"""  
  
for llm in llms:  
 chain = prompt | llm | parser  
 translation = chain.invoke({"poem": poem, "language": "英语"})  
 print(f"【{llm.model_name}】的翻译结果")  
 print(f"{translation}")  
 print("----------------------------------------------")

输出结果对比如下:

  
【glm-4-0520】的翻译结果  
Moonlight shines before my bed,  
Like frost on the ground it spreads.  
I raise my head to the bright moon above,  
Then bow it down, my hometown to think of.  
----------------------------------------------  
【glm-4-airx】的翻译结果  
Beneath my bed, the moonlight shines bright,  
Like frost on ground, it brings a silent sight.  
I raise my head to gaze at the moon so fair,  
And bow it down, my hometown I recall.  
----------------------------------------------  
【gpt-4o-mini】的翻译结果  
The bright moonlight before my bed,   
Is it frost upon the ground instead?   
I raise my head to gaze at the moon,   
Then lower it, longing for home all too soon.  
----------------------------------------------  

从翻译结果可以看出,相较于 gpt-4o-miniglm-4-0520 的翻译结果更符合中文的意境,同时对比 glm-4-airx 的结果也更加精炼。

除此之外,从性能的角度看,glm-4-0520 的生成结果耗时更低(LangSmith 平台观测结果,仅供参考):

picture.image

picture.image

综上所述:针对中文语料的翻译工作,glm-4-0520 应该是一个不错的选择。

附 智谱AI开放平台:https://open.bigmodel.cn/console/overview

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
IDC 大模型应用落地白皮书
大模型技术已深度融入业务实践,各企业期望其释放更大商业价值。 但大模型落地之路面临许多挑战和顾虑。 如何精准对接业务需求与发展蓝图,制定切实可行的大模型落地策略? IDC发布首个大模型应用策略与行动指南 一为您揭晓一
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论