OpenAI?函數(shù)調(diào)用示例及功能入門教程
引言
我是AI小火箭的HB,我探索和寫作人工智能和語言交叉點的所有事物,范圍從LLM,聊天機器人,語音機器人,開發(fā)框架,以數(shù)據(jù)為中心的潛在空間等。
范例
初步體驗
OpenAI新增了“函數(shù)調(diào)用”功能,這是什么呢?
我們先調(diào)用API來體驗下。
下面是發(fā)送到模型的 JSON 文檔。此調(diào)用的目的是生成一個 JSON 文件,該文件可用于發(fā)送到發(fā)送電子郵件的 API。
您可以看到函數(shù)名稱為 send_email,并定義了三個參數(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模型會返回需要調(diào)用的函數(shù)名 send_email和對應的參數(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]" }
這就非常有用,第三方的應用可以提供多個函數(shù)/服務(類似插件),GPT模型可以根據(jù)用戶的指令自動選擇不同的函數(shù)/服務。
現(xiàn)在再來看示例,就比較清晰了。
用途
根據(jù)官網(wǎng)文檔,函數(shù)調(diào)用允許您更可靠地從模型中獲取結(jié)構(gòu)化數(shù)據(jù)。例如,您可以:
創(chuàng)建聊天機器人,通過調(diào)用外部 API 來回答問題(例如 ChatGPT 插件)
例如,定義像 send_email(to: string, body: string) 或 get_current_weather(location: string, unit: 'celsius' | 'fahrenheit') 這樣的函數(shù)
將自然語言轉(zhuǎn)換為 API 調(diào)用
例如,將“誰是我的頂級客戶?”轉(zhuǎn)換為 get_customers(min_revenue: int, created_before: string, limit: int) 并調(diào)用您的內(nèi)部 API
從文本中提取結(jié)構(gòu)化數(shù)據(jù)
例如,定義一個名為 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 對象(注意:模型可能會生成無效的 JSON 或幻覺參數(shù))。
在代碼中將字符串解析為 JSON,并使用提供的參數(shù)調(diào)用函數(shù)(如果存在)。
通過將函數(shù)響應追加為新消息來再次調(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,大家可以去體驗下。
以上就是OpenAI 函數(shù)調(diào)用示例及功能入門教程的詳細內(nèi)容,更多關于OpenAI 函數(shù)調(diào)用的資料請關注腳本之家其它相關文章!
相關文章
VSCode連接遠程服務器調(diào)試代碼超詳細流程(圖文)
這篇文章主要給大家介紹了關于VSCode連接遠程服務器調(diào)試代碼的超詳細流程,遠程調(diào)試是為了解決在本機開發(fā)環(huán)境與線上不一致導致調(diào)試難、搭建繁瑣,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-10-10代碼著色之SyntaxHighlighter項目(最流行的代碼高亮)
dp.SyntaxHighlighter。它可以在網(wǎng)頁中對各種程序源代碼語法進行加亮顯示。支持當前流 行的各種編程語言:C#、CSS、C++、Delphi、Java、JavaScript、PHP、Python、Ruby、SQL、Visual Basic、XML / HTML等2014-04-04使用git?config?--global設置用戶名和郵件問題
這篇文章主要介紹了使用git?config?--global設置用戶名和郵件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05