以下是一個簡潔、實用的 Python 範例,使用了平台文件中介紹的 OpenAI SDK 模式。它會建立一個回應,請求 GPT-5 產生一個小型 API 支援的端點,並示範如何處理函數呼叫。
# Example: Python (OpenAI official SDK style)
from openai import OpenAI
client = OpenAI(api_key="sk-...")
prompt = "Create a small Flask endpoint /summary that accepts POST JSON { 'text': string } and returns a short summary."
resp = client.responses.create(
model="gpt-5",
input=prompt,
# tuning options new in GPT-5
verbosity="medium", # low | medium | high
reasoning_effort="standard" # minimal | standard | deep
)
print(resp.output_text) # GPT-5's generated code + explanation
from openai import OpenAI
client = OpenAI()
functions = [
{
"name": "get_weather",
"description": "Return current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"units": {"type": "string", "enum": }
},
"required":
}
}
]
# Ask GPT-5 to plan what to call
resp = client.responses.create(
model="gpt-5-high",
input="What's the weather like in Kyoto and should I pack an umbrella?",
functions=functions,
function_call="auto", # allow model to decide to call get_weather
verbosity="medium"
)
# If model decides to call the function, you'll get a function_call object
if resp.output.get("function_call"):
call = resp.output
func_name = call
func_args = call # parsed JSON-like dict
# Now call your backend or external API using func_args...
這種模式將 模型決策外部執行,讓模型協調工作流程,同時您的程式碼保留控制和安全性。
為什麼函數呼叫對於編碼工作流程很重要
安全指引:模型不能直接在您的基礎架構上執行任意程式碼—您的應用程式負責調解一切。
自動化:將模型規劃與安全編排結合(建立分支→執行CI→返回測試日誌)。
可解釋性:結構化呼叫比自由文字更容易審核和記錄。
GPT-5 的函數呼叫與早期模型有何不同?
更豐富的工具類型 (具有純文字輸入的自訂工具),從而更容易整合非 JSON 或臨時工具。
改進的結構化輸出 和 CFG(上下文無關語法)支持,使得受監管領域的高度受限的輸出成為可能。
更可靠的功能選擇但是社群報告表明偶爾仍會出現參數錯誤;因此在伺服器端驗證函數參數是明智的。
GPT-5的編碼能力有多好?
基準測試結果如何?
多個獨立基準測試團隊看到了與先前的 OpenAI 模型相比的實質改進:
On SWE-長凳 和其他以程式碼為中心的套件相比,GPT-5 變體表現出更高的任務完成率(公共基準測試貼文中的範例報告顯示,在某些任務上,GPT-60.x 的成功率明顯較低,而成功率躍升至 75-4% 的範圍)。
"Create a minimal fullstack app: an Express backend with one POST /summarize endpoint (accepts {text}) that calls a GPT-5 function 'summarize_text', and a React front-end that posts text and displays the summary. Use modern conventions, include package.json and instructions."