在日常使用大模型的时候你是否遇到过以下问题:
需要自己字斟句酌提示词(Prompt),进行CoT
需要在多种大模型,多种工具(绘图模型,集成开发环境IDE,搜索引擎….)之间辗转才能达到最终目标......
这些都是普遍存在,靠目前的单一模型无法解决的问题。 把时间推回到250万年前,旧石器时代见证了人类从简单工具的使用者到复杂技术创造者的转变。我们的祖先南方古猿、直立人等,逐步发展出更高级的工具 ,如手斧和刮刀,以适应狩猎和生活的需求。大约4万年前,现代人类——智人——的出现标志着技术和文化的飞跃,工具 进一步精致化如骨针、鱼钩,艺术创作如洞穴壁画和雕塑也随之兴起。这一时期,火的利用、语言和社会结构的形成,为人类文明的后续发展奠定了坚实的基础。
我们无法保证大模型的“进化”和人类的进化史必定存在强相关性,不过,若以历史作为教科书,解决问题的答案或许已在书中沉淀万年。我们相信,大模型调用工具的能力(或称作智能体能力)是解决前文问题的一种有效方法。
GLM-4-AllTools - 革新大模型API调用体验 我们在 bigmodel.cn[1] 对最新的GLM-4-AllTools API 进行了评测。
GLM-4-AllTools: Chat API,集成了调用工具的能力,能够自主使用多种工具组合和多轮对话来达成用户任务。和传统LLM API相比,GLM-4-AllTools API集成了五大核心功能,包括智能编程助手,安全代码沙盒,实时联网搜索,绘图设计工具,函数调用能力。其中函数调用功能支持用户自定义函数,为进阶开发者提供了便利。当模型识别到解决用户输入的请求需要搜索引擎时,它会自动激活内置的联网搜索功能,以提供实时且全面的搜索结果。搜索完成后,内置的网页浏览工具能够打开并细致查看网页内容,从而进行更深入的分析。此外,借助卓越的自我规划能力和内置的代码沙盒、绘图设计等工具,API能够仅通过一次互动就高效地处理复杂的任务。
下面请跟随我们的脚步,我们将一步一步组合这些工具,测试GLM-4-AllTools的真正实力:
Case1: 实时查询欧洲杯淘汰赛对阵 Tools: Web Search
代码:
fromzhipuaiimportZhipuAI client=ZhipuAI(api_key="ZhipuAPIKey")#请填写您自己的APIKey response=client.chat.completions.create( model="glm-4-alltools",#填写需要调用的模型名称 stream=True, messages=[ { "role":"user", "content":[ { "type":"text", "text":"帮我查询本届欧洲杯淘汰赛对阵情况。" } ] } ], tools=[ { "type":"web_browser"#联网搜索 }, ] ) fortrunkinresponse: iftrunk.choices[0].delta.contentisnotNone: print(trunk.choices[0].delta.content,end="")回复:
2024年欧洲杯淘汰赛的对阵情况如下: -6月30日: -瑞士vs意大利(00:00) -德国vs丹麦(03:00) -7月1日: -英格兰vs斯洛伐克(00:00) -西班牙vs格鲁吉亚(03:00) -7月2日: -法国vs比利时(00:00) -葡萄牙vs斯洛文尼亚(03:00) -7月3日: -荷兰vs罗马尼亚(00:00) -奥地利vs土耳其(03:00) 请注意,以上时间均为北京时间。Case2: 查询慕尼黑天气,自动绘画天气曲线 Tools: Web Search, Code Interpreter, Sandbox
fromzhipuaiimportZhipuAI client=ZhipuAI(api_key="")#请填写您自己的APIKey response=client.chat.completions.create( model="glm-4-alltools",#填写需要调用的模型名称 stream=True, messages=[ { "role":"user", "content":[ { "type":"text", "text":"帮我查询慕尼黑07/05至07/15的日平均气温。并将所有平均气温数值组成数列,绘出折线图显示趋势。" } ] } ], tools=[ { "type":"web_browser" }, { "type":"code_interpreter",#智能编程助手 }, ] ) fortrunkinresponse: print(trunk) iftrunk.choices[0].delta.contentisnotNone: print(trunk.choices[0].delta.content,end="")回复: 这次我们进入到GLM-4-AllTools API的工作流中,看看模型为了解决Case2执行了哪些步骤:
#search ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'search'})]),finish_reason=None,index=0)],created=1720460600,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'("'})]),finish_reason=None,index=0)],created=1720460600,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'M'})]),finish_reason=None,index=0)],created=1720460600,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'un'})]),finish_reason=None,index=0)],created=1720460600,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'ich'})]),finish_reason=None,index=0)],created=1720460600,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'average'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'temperature'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'July'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':''})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'5'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'to'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':'July'})]),finish_reason=None,index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ... ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_bwN-_DC2XvXuP2meMxKzd',function=None,type='web_browser',web_browser={'input':''})]),finish_reason='tool_calls',index=0)],created=1720460601,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='tool',tool_calls=[ChoiceDeltaToolCall(index=None,id=None,function=None,type='web_browser',web_browser={'outputs':[{'title':'WeatherinMunichinJuly2024','link':'https://world-weather.info/forecast/germany/munich/july-2024/','content':'Detailed⚡MunichWeatherForecastforJuly2024–day/night?️temperatures,precipitations–World-Weather.info'},{'title':'Munich,Bavaria,GermanyMonthlyWeather|AccuWeather','link':'https://www.accuweather.com/en/de/munich/80331/july-weather/178086','content':'GetthemonthlyweatherforecastforMunich,Bavaria,Germany,includingdailyhigh/low,historicalaverages,tohelpyouplanahead.'},{'title':'Munich,BavariaWeatherForecastJuly2024 ailyHighs/Lows&RainTrends','link':'https://www.weathertab.com/en/c/e/07/federal-republic-of-germany/bavaria/munich/','content':'ExplorecomprehensiveJuly2024weatherforecastsforMunich,includingdailyhighandlowtemperatures,precipitationrisks,andmonthlytemperaturetrends.Featuringdetailedday-by-dayforecasts,dynamicgraphsofdailyrainprobabilities,andtemperaturetrendstohelpyouplanahead.'},{'title':'Yearly&Monthlyweather-Munich,Germany','link':'https://www.weather-atlas.com/en/germany/munich-climate','content':'Detailedclimateinformationwithcharts-averagemonthlyweatherwithtemperature,pressure,humidity,precipitation,wind,daylight,sunshine,visibility,andUVindexdata.Munichseeslongerdaylighthoursfrom15.2hoursupto16hoursfromMaytoJuly,whiletheshortestdaylighthoursrangingfrom8.5hoursto8.9hourshappensbetween...'},{'title':'Munichclimate:weatherbymonth,temperature,rain-ClimatestoTravel','link':'https://www.climatestotravel.com/climate/germany/munich','content':'AttheformerMunich-Riemairport,thedailyaveragetemperaturerangesfrom-0.4°C(31.4°F)inJanuaryto18.6°C(65.5°F)inJuly.Herearetheaveragetemperatures.Precipitationisquiteabundant,around950millimeters(37.5inches)peryear.'},{'title':'Julyweather-Summer2024-Munich,Germany','link':'https://www.weather-atlas.com/en/germany/munich-weather-july','content':'AveragehightemperatureinJuly:23.1°C;Thewarmestmonth(withthehighestaveragehightemperature)isJuly(23.1°C).ThemonthwiththelowestaveragehightemperatureisJanuary(2.7°C).AveragelowtemperatureinJuly:12.6°C;ThemonthwiththehighestaveragelowtemperatureisJuly(12.6°C).'},{'title':'MunichweatherinJuly2024|Sunheron','link':'https://www.sunheron.com/europe/germany/bavaria/munich-weather-july/','content':'TheaveragedailytemperatureinJulyinMunichis25°C(78°F).Theidealtemperatureforenjoyingsunbathingbythewaterandagoodtemperatureforotheractivities.Forsomepeople,itwillbetoohotinthedirectsunlightandthestaymaynotbesopleasant.'},{'title':'Munichinjuly:averageWeather,TemperatureandClimate','link':'https://www.whereandwhen.net/when/europe/germany/munich/july/','content':'TheweatherinjulyinMunichiswet(with6.9inofprecipitationover10days).Thisisbetterthanthepreviousmonthsinceinjuneitreceivesanaverageof7.8inofprecipitationover12days.Theclimatecomfortableinthatlocalitythismonth.Temperaturesareupto76°F.Theseasonalminimumis63°F.'},{'title':'MunichweatherinJuly2024|Germany:Howhot?-Weather2Travel.com','link':'https://www.weather2travel.com/germany/munich/july/','content':'MunichweatherinJuly2024.Expect23°Cdaytimemaximumtemperaturesintheshadewithonaverage8hoursofsunshineperdayinMunichinJuly.Checkmorelong-termweatheraveragesforMunichinJulybeforeyoubookyournextholidaytoGermanyin2024/2025.'}]})]),finish_reason=None,index=0)],created=1720460602,model='glm-4-alltools',usage=None,extra_json=None)... #codeinterpreter,simulateinsandbox ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'\n'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'plt'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'.'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'grid'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'('})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'True'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':')'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'\n'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'plt'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'.'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'show'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':'()'})]),finish_reason=None,index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_ByzIxpcobsFBHhonh9UgI',function=None,type='code_interpreter',code_interpreter={'input':''})]),finish_reason='tool_calls',index=0)],created=1720460616,model='glm-4-alltools',usage=None,extra_json=None) ChatCompletionChunk(id='8822098568301210663',choices=[Choice(delta=ChoiceDelta(content=None,role='tool',tool_calls=[ChoiceDeltaToolCall(index=None,id=None,function=None,type='code_interpreter',code_interpreter={'outputs':[{'type':'file','file':'http://all-tool-interpreter.cn-wlcb.ufileos.com/3776fa6a-c175-457b-8911-0a697383fb03_fig.png?UCloudPublicKey=TOKEN_69f58d9b-ddb3-4bd0-9b29-5a4fe422c720&Expires=1720467819&Signature=dEJgDv6+/HkQokVWmJidf39WxpU='}]})]),finish_reason=None,index=0)],created=1720460619,model='glm-4-alltools',usage=None,extra_json=None)最后返图:
Case3: Function Calling Tools: Code Interpreter, Sandbox
#完整代码 fromzhipuaiimportZhipuAI client=ZhipuAI(api_key="")#请填写您自己的APIKey response=client.chat.completions.create( model="glm-4-alltools",#填写需要调用的模型名称 stream=True, messages=[ { "role":"user", "content":[ { "type":"text", "text":"帮我查询慕尼黑07/05至07/15的日平均气温。并用日平均气温数列绘出折线图显示趋势。" } ] } ], tools=[ { "type":"function", "function":{ "name":"get_average_temperature_data_by_date", "description":"用于汇总某地某段时间内的日平均气温,输入地点(city),日期范围(from_day,to_day),返回对应的日平均气温数列。", "parameters":{ "type":"object", "properties":{ "from_day":{ "description":"开始日期,格式为mm/dd", "type":"string" }, "to_day":{ "description":"截止日期,格式为mm/dd", "type":"string" }, "avg_temperature":{ "description":"平均气温", "type":"string" }, "required":["city","from_day","to_day"] }, } } }, { "type":"code_interpreter", }, ] ) fortrunkinresponse: print(trunk) iftrunk.choices[0].delta.contentisnotNone: print(trunk.choices[0].delta.content,end="")需要调用函数 Function Call,模型会返回 status='requires_action'
ChatCompletionChunk(id='8822109666497191178',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=[ChoiceDeltaToolCall(index=None,id='call_cPf6NnnlWzSB0Fm7ZpgUF',function=ChoiceDeltaToolCallFunction(arguments='{"city":"慕尼黑","from_day":"07/05","to_day":"07/15"}',name='get_average_temperature_data_by_date'),type='function')]),finish_reason='tool_calls',index=0)],created=1720463116,model='glm-4-alltools',usage=CompletionUsage(prompt_tokens=435,completion_tokens=50,total_tokens=485),extra_json=None,status='requires_action')添加函数调用的执行结果(Tool Message),以及模型生成的函数参数(Assistant Message)。
messages=[ { "role":"user", "content":[ { "type":"text", "text":"帮我查询慕尼黑07/05至07/15的日平均气温。并用日平均气温数列绘出折线图显示趋势。" } ] }, { "role":"assistant", "content":"arguments='{city\":\"慕尼黑\",\"from_day\":\"07/05\",\"to_day\":\"07/15\"}',name='get_average_temperature_data_by_date'" }, { "role":"tool", "content":"[21.7,16.6,29.2,25.0,22.2,25.9,18.8,22.2,26.2,26.1,19.6]" } ],#执行 ChatCompletionChunk(id='8822109666497168900',choices=[Choice(delta=ChoiceDelta(content=None,role='tool',tool_calls=[ChoiceDeltaToolCall(index=None,id=None,function=None,type='code_interpreter',code_interpreter={'outputs':[{'type':'file','file':'http://all-tool-interpreter.cn-wlcb.ufileos.com/b9200b5b-73ed-4893-8187-1f7a1ab1c7d1_fig.png?UCloudPublicKey=TOKEN_69f58d9b-ddb3-4bd0-9b29-5a4fe422c720&Expires=1720470579&Signature=oSMDBUI6hDdZboES85CZUxgAZRc='}]})]),finish_reason=None,index=0)],created=1720463379,model='glm-4-alltools',usage=None,extra_json=None)每次GLM-4-AllTools API调用结束,都会在最后的ChatCompletionChunk返回此次调用的Token使用情况,帮助用户进行及时快速监测。
#计费 ChatCompletionChunk(id='8822109735216674822',choices=[Choice(delta=ChoiceDelta(content=None,role='assistant',tool_calls=None),finish_reason='stop',index=0)],created=1720462433,model='glm-4-alltools',usage=CompletionUsage(prompt_tokens=1518,completion_tokens=313,total_tokens=1831),extra_json=None,status='completed'总结:一番体验下来我对 GLM-4-AllTools API 是越用越喜欢,它主要有以下几个优点:
可以联网搜索,可以在沙盒环境快速执行代码,大大加速方案验证 不过也存在一些小缺点:
关于MaaS平台 大模型MaaS开放平台(bigmodel.cn)目前提供了GLM系列模型的API调用支持,包括通用模型、图像模型、embedding模型等。可以在文档中查看相关说明,并在体验中心进行快速体验。 通过这次体验,我确确实实感受到了 GLM-4-AllTools API 给用户体验带来的提升,以及为开发者带来的便利,希望国内大模型厂商不断推出更强大的产品,共同助力中国大模型以及大模型产业的发展。