OpenAI?函數(shù)調(diào)用示例及功能入門教程
引言
我是AI小火箭的HB,我探索和寫作人工智能和語言交叉點(diǎn)的所有事物,范圍從LLM,聊天機(jī)器人,語音機(jī)器人,開發(fā)框架,以數(shù)據(jù)為中心的潛在空間等。
范例

初步體驗(yàn)
OpenAI新增了“函數(shù)調(diào)用”功能,這是什么呢?
我們先調(diào)用API來體驗(yàn)下。
下面是發(fā)送到模型的 JSON 文檔。此調(diào)用的目的是生成一個(gè) JSON 文件,該文件可用于發(fā)送到發(fā)送電子郵件的 API。
您可以看到函數(shù)名稱為 send_email,并定義了三個(gè)參數(shù), to_address , subject 和 body ,即電子郵件正文。
用戶請求為:Send Cobus from humanfirst ai an email asking for the monthly report?
curl --location 'https://api.openai.com/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-xxxx' \
--data '{
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "Send Cobus from humanfirst ai an email asking for the monthly report?"}
],
"functions": [
{
"name": "send_email",
"description": "Please send an email.",
"parameters": {
"type": "object",
"properties": {
"to_address": {
"type": "string",
"description": "To address for email"
},
"subject": {
"type": "string",
"description": "subject of the email"
},
"body": {
"type": "string",
"description": "Body of the email"
}
}
}
}
]
}'下面是返回的 JSON
{
"id": "chatcmpl-7TQuwzJpQAY470saQM2RPfxwF6DDE",
"object": "chat.completion",
"created": 1687249338,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "send_email",
"arguments": "{\n \"to_address\": \"cobus@humanfirst.ai\",\n \"subject\": \"Request for Monthly Report\",\n \"body\": \"Hi Cobus,\\n\\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\\n\\nThanks,\\n[Your Name]\"\n}"
}
},
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 86,
"completion_tokens": 82,
"total_tokens": 168
}
}GPT模型會(huì)返回需要調(diào)用的函數(shù)名 send_email和對(duì)應(yīng)的參數(shù)(放在arguments字段)。
{
"to_address": "cobus@humanfirst.ai",
"subject": "Request for Monthly Report",
"body": "Hi Cobus,\n\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\n\nThanks,\n[Your Name]"
}這就非常有用,第三方的應(yīng)用可以提供多個(gè)函數(shù)/服務(wù)(類似插件),GPT模型可以根據(jù)用戶的指令自動(dòng)選擇不同的函數(shù)/服務(wù)。
現(xiàn)在再來看示例,就比較清晰了。

用途
根據(jù)官網(wǎng)文檔,函數(shù)調(diào)用允許您更可靠地從模型中獲取結(jié)構(gòu)化數(shù)據(jù)。例如,您可以:
創(chuàng)建聊天機(jī)器人,通過調(diào)用外部 API 來回答問題(例如 ChatGPT 插件)
例如,定義像 send_email(to: string, body: string) 或 get_current_weather(location: string, unit: 'celsius' | 'fahrenheit') 這樣的函數(shù)
將自然語言轉(zhuǎn)換為 API 調(diào)用
例如,將“誰是我的頂級(jí)客戶?”轉(zhuǎn)換為 get_customers(min_revenue: int, created_before: string, limit: int) 并調(diào)用您的內(nèi)部 API
從文本中提取結(jié)構(gòu)化數(shù)據(jù)
例如,定義一個(gè)名為 extract_data(name: string, birthday: string) 或 sql_query(query: string) 的函數(shù)
函數(shù)調(diào)用的基本步驟順序如下:
使用用戶查詢和函數(shù)參數(shù)中定義的一組函數(shù)調(diào)用模型。
模型可以選擇調(diào)用函數(shù);如果是這樣,內(nèi)容將是符合自定義架構(gòu)的字符串化 JSON 對(duì)象(注意:模型可能會(huì)生成無效的 JSON 或幻覺參數(shù))。
在代碼中將字符串解析為 JSON,并使用提供的參數(shù)調(diào)用函數(shù)(如果存在)。
通過將函數(shù)響應(yīng)追加為新消息來再次調(diào)用模型,并讓模型將結(jié)果匯總回給用戶。
AI小火箭已經(jīng)支持函數(shù)調(diào)用和gpt-3.5-turbo-16k、gpt-3.5-turbo-0613、gpt-3.5-turbo-16k-0613,大家可以去體驗(yàn)下。
以上就是OpenAI 函數(shù)調(diào)用示例及功能入門教程的詳細(xì)內(nèi)容,更多關(guān)于OpenAI 函數(shù)調(diào)用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Git打標(biāo)簽從本地創(chuàng)建到遠(yuǎn)端推送的詳細(xì)流程
在軟件開發(fā)中,Git標(biāo)簽(Tag)是為發(fā)布版本、標(biāo)記里程碑量身定制的“快照錨點(diǎn)”,它能永久記錄項(xiàng)目歷史中的關(guān)鍵節(jié)點(diǎn),然而,僅創(chuàng)建本地標(biāo)簽往往不夠,如何將其高效地推送到遠(yuǎn)程倉庫以實(shí)現(xiàn)團(tuán)隊(duì)共享,是許多開發(fā)者會(huì)遇到的實(shí)際問題,本文給大家介紹了Git打標(biāo)簽的完整流程2025-09-09
Visual?Studio實(shí)用調(diào)試技巧大全
這篇文章主要給大家介紹了關(guān)于Visual?Studio實(shí)用調(diào)試技巧的相關(guān)資料,如果你還沒有使用過這些技巧,希望這篇博文能幫你發(fā)現(xiàn)它們,它們學(xué)起來很容易,能幫你節(jié)省很多時(shí)間,需要的朋友可以參考下2023-06-06

