
2024年您產品必備的10大AI API推薦
把時間推回到250萬年前,舊石器時代見證了人類從簡單工具的使用者到復雜技術創造者的轉變。我們的祖先南方古猿、直立人等,逐步發展出更高級的工具,如手斧和刮刀,以適應狩獵和生活的需求。大約4萬年前,現代人類——智人——的出現標志著技術和文化的飛躍,工具進一步精致化如骨針、魚鉤,藝術創作如洞穴壁畫和雕塑也隨之興起。這一時期,火的利用、語言和社會結構的形成,為人類文明的后續發展奠定了堅實的基礎。
我們無法保證大模型的“進化”和人類的進化史必定存在強相關性,不過,若以歷史作為教科書,解決問題的答案或許已在書中沉淀萬年。我們相信,大模型調用工具的能力(或稱作智能體能力)是解決前文問題的一種有效方法。
我們在 bigmodel.cn[1]對最新的GLM-4-AllTools API進行了評測。
GLM-4-AllTools: Chat API,集成了調用工具的能力,能夠自主使用多種工具組合和多輪對話來達成用戶任務。和傳統LLM API相比,GLM-4-AllTools API集成了五大核心功能,包括智能編程助手,安全代碼沙盒,實時聯網搜索,繪圖設計工具,函數調用能力。其中函數調用功能支持用戶自定義函數,為進階開發者提供了便利。當模型識別到解決用戶輸入的請求需要搜索引擎時,它會自動激活內置的聯網搜索功能,以提供實時且全面的搜索結果。搜索完成后,內置的網頁瀏覽工具能夠打開并細致查看網頁內容,從而進行更深入的分析。此外,借助卓越的自我規劃能力和內置的代碼沙盒、繪圖設計等工具,API能夠僅通過一次互動就高效地處理復雜的任務。
下面請跟隨我們的腳步,我們將一步一步組合這些工具,測試GLM-4-AllTools的真正實力:
Tools: Web Search
代碼:
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="Zhipu API Key") # 請填寫您自己的APIKey
response = client.chat.completions.create(
model="glm-4-alltools", # 填寫需要調用的模型名稱
stream=True,
messages=[
{
"role": "user",
"content":[
{
"type":"text",
"text":"幫我查詢本屆歐洲杯淘汰賽對陣情況。"
}
]
}
],
tools=[
{
"type": "web_browser" #聯網搜索
},
]
)
for trunk in response:
if trunk.choices[0].delta.content is not None:
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)
請注意,以上時間均為北京時間。
Tools: Web Search, Code Interpreter, Sandbox
from zhipuai import ZhipuAI
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", #智能編程助手
},
]
)
for trunk in response:
print(trunk)
if trunk.choices[0].delta.content is not None:
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': 'Weather in Munich in July 2024', 'link': 'https://world-weather.info/forecast/germany/munich/july-2024/', 'content': 'Detailed ? Munich Weather Forecast for July 2024 – day/night ??? temperatures, precipitations – World-Weather.info'}, {'title': 'Munich, Bavaria, Germany Monthly Weather | AccuWeather', 'link': 'https://www.accuweather.com/en/de/munich/80331/july-weather/178086', 'content': 'Get the monthly weather forecast for Munich, Bavaria, Germany, including daily high/low, historical averages, to help you plan ahead.'}, {'title': 'Munich, Bavaria Weather Forecast July 2024: Daily Highs/Lows & Rain Trends', 'link': 'https://www.weathertab.com/en/c/e/07/federal-republic-of-germany/bavaria/munich/', 'content': 'Explore comprehensive July 2024 weather forecasts for Munich, including daily high and low temperatures, precipitation risks, and monthly temperature trends. Featuring detailed day-by-day forecasts, dynamic graphs of daily rain probabilities, and temperature trends to help you plan ahead.'}, {'title': 'Yearly & Monthly weather - Munich, Germany', 'link': 'https://www.weather-atlas.com/en/germany/munich-climate', 'content': 'Detailed climate information with charts - average monthly weather with temperature, pressure, humidity, precipitation, wind, daylight, sunshine, visibility, and UV index data. Munich sees longer daylight hours from 15.2 hours up to 16 hours from May to July, while the shortest daylight hours ranging from 8.5 hours to 8.9 hours happens between ...'}, {'title': 'Munich climate: weather by month, temperature, rain - Climates to Travel', 'link': 'https://www.climatestotravel.com/climate/germany/munich', 'content': 'At the former Munich-Riem airport, the daily average temperature ranges from -0.4 °C (31.4 °F) in January to 18.6 °C (65.5 °F) in July. Here are the average temperatures. Precipitation is quite abundant, around 950 millimeters (37.5 inches) per year.'}, {'title': 'July weather - Summer 2024 - Munich, Germany', 'link': 'https://www.weather-atlas.com/en/germany/munich-weather-july', 'content': 'Average high temperature in July: 23.1°C; The warmest month (with the highest average high temperature) is July (23.1°C). The month with the lowest average high temperature is January (2.7°C). Average low temperature in July: 12.6°C; The month with the highest average low temperature is July (12.6°C).'}, {'title': 'Munich weather in July 2024 | Sunheron', 'link': 'https://www.sunheron.com/europe/germany/bavaria/munich-weather-july/', 'content': 'The average daily temperature in July in Munich is 25 °C (78 °F). The ideal temperature for enjoying sunbathing by the water and a good temperature for other activities. For some people, it will be too hot in the direct sunlight and the stay may not be so pleasant.'}, {'title': 'Munich in july: average Weather, Temperature and Climate', 'link': 'https://www.whereandwhen.net/when/europe/germany/munich/july/', 'content': 'The weather in july in Munich is wet (with 6.9in of precipitation over 10 days). This is better than the previous month since in june it receives an average of 7.8in of precipitation over 12 days. The climate comfortable in that locality this month. Temperatures are up to 76°F. The seasonal minimum is 63°F.'}, {'title': 'Munich weather in July 2024 | Germany: How hot? - Weather2Travel.com', 'link': 'https://www.weather2travel.com/germany/munich/july/', 'content': 'Munich weather in July 2024. Expect 23°C daytime maximum temperatures in the shade with on average 8 hours of sunshine per day in Munich in July. Check more long-term weather averages for Munich in July before you book your next holiday to Germany in 2024/2025.'}]})]), finish_reason=None, index=0)], created=1720460602, model='glm-4-alltools', usage=None, extra_json=None)
...
...
#code interpreter, simulate in sandbox
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)
最后返圖:
Tools: Code Interpreter, Sandbox
#完整代碼
from zhipuai import ZhipuAI
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",
},
]
)
for trunk in response:
print(trunk)
if trunk.choices[0].delta.content is not None:
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開放平臺(bigmodel.cn)目前提供了GLM系列模型的API調用支持,包括通用模型、圖像模型、embedding模型等。可以在文檔中查看相關說明,并在體驗中心進行快速體驗。
通過這次體驗,我確確實實感受到了 GLM-4-AllTools API 給用戶體驗帶來的提升,以及為開發者帶來的便利,希望國內大模型廠商不斷推出更強大的產品,共同助力中國大模型以及大模型產業的發展。
[1]Maas 開放平臺:http://bigmodel.cn/
文章轉自微信公眾號@深度學習自然語言處理