如何使用GPT-4o函數(shù)調(diào)用構(gòu)建一個(gè)實(shí)時(shí)應(yīng)用程序? 原創(chuàng)
本教程介紹了如何使用OpenAI最新的LLM GPT-4o通過函數(shù)調(diào)用將實(shí)時(shí)數(shù)據(jù)引入LLM。
我們?cè)贚LM函數(shù)調(diào)用指南(詳見https://thenewstack.io/a-comprehensive-guide-to-function-calling-in-llms/)中討論了如何將實(shí)時(shí)數(shù)據(jù)引入聊天機(jī)器人和代理?,F(xiàn)在,我們將通過將來自FlightAware.com的API與新的GPT-4o模型集成,進(jìn)一步探究這個(gè)概念,以便實(shí)時(shí)跟蹤航班狀態(tài)。
FlightAware的AeroAPI是一個(gè)可靠的充分利用REST的API,提供按需訪問航班跟蹤和狀態(tài)數(shù)據(jù)。它允許開發(fā)人員通過一個(gè)基于查詢的簡單系統(tǒng),獲取實(shí)時(shí)、歷史或未來的航班信息。API支持基于航班標(biāo)識(shí)符、飛機(jī)注冊(cè)號(hào)或機(jī)場或運(yùn)營商等位置的詳細(xì)請(qǐng)求。它旨在以JSON格式提供精確、可操作的航空數(shù)據(jù),支持整個(gè)航空業(yè)從航空公司到機(jī)場的運(yùn)營需求。
在繼續(xù)之前,注冊(cè)FlightAware并獲得API密鑰,這對(duì)于調(diào)用REST API至關(guān)重要。免費(fèi)的個(gè)人套餐足以完成本教程。
第1步:定義獲取航班狀態(tài)的函數(shù)
一旦您獲得了API密鑰,用Python創(chuàng)建以下函數(shù)來檢索任何航班的狀態(tài)。
import ast
import json
import random
from datetime import datetime, timedelta
import requests
import pytz
def get_flight_status(flight):
"""Returns Flight Information"""
AEROAPI_BASE_URL = "https://aeroapi.flightaware.com/aeroapi"
AEROAPI_KEY="YOUR FLIGHTAWARE API KEY"
def get_api_session():
session = requests.Session()
session.headers.update({"x-apikey": AEROAPI_KEY})
return session
def fetch_flight_data(flight_id, session):
if "flight_id=" in flight_id:
flight_id = flight_id.split("flight_id=")[1]
start_date = datetime.now().date().strftime('%Y-%m-%d')
end_date = (datetime.now().date() + timedelta(days=1)).strftime('%Y-%m-%d')
api_resource = f"/flights/{flight_id}?start={start_date}&end={end_date}"
response = session.get(f"{AEROAPI_BASE_URL}{api_resource}")
response.raise_for_status()
return response.json()['flights'][0]
def utc_to_local(utc_date_str, local_timezone_str):
utc_datetime = datetime.strptime(utc_date_str, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.utc)
local_timezone = pytz.timezone(local_timezone_str)
local_datetime = utc_datetime.astimezone(local_timezone)
return local_datetime.strftime('%Y-%m-%d %H:%M:%S')
session = get_api_session()
flight_data = fetch_flight_data(flight, session)
dep_key = 'estimated_out' if 'estimated_out' in flight_data and flight_data['estimated_out'] else \
'actual_out' if 'actual_out' in flight_data and flight_data['actual_out'] else \
'scheduled_out'
arr_key = 'estimated_in' if 'estimated_in' in flight_data and flight_data['estimated_in'] else \
'actual_in' if 'actual_in' in flight_data and flight_data['actual_in'] else \
'scheduled_in'
flight_details = {
'flight':flight,
'source': flight_data['origin']['city'],
'destination': flight_data['destination']['city'],
'depart_time': utc_to_local(flight_data[dep_key], flight_data['origin']['timezone']),
'arrival_time': utc_to_local(flight_data[arr_key], flight_data['destination']['timezone']),
'status': flight_data['status']
}
return json.dumps(flight_details)
flight_info = get_flight_status("EK524")
print(flight_info)
#'{"flight": "EK524", "source": "Dubai", "destination": "Hyderabad", "depart_time": "2024-05-23 22:00:00", "arrival_time": "2024-05-24 03:05:00", "status": "Scheduled"}'
雖然代碼很簡單,但還是不妨解釋一下關(guān)鍵步驟。
get_flight_status函數(shù)接受一個(gè)航班參數(shù)(假設(shè)是航班標(biāo)識(shí)符),并以JSON格式返回格式化的航班詳細(xì)信息。它查詢AeroAPI以根據(jù)給定的航班標(biāo)識(shí)符獲取航班數(shù)據(jù),并確定關(guān)鍵細(xì)節(jié)的格式,比如出發(fā)地、目的地、離開時(shí)間、到達(dá)時(shí)間和狀態(tài)。
不妨看看腳本的組件:
API憑據(jù):
AEROAPI_BASE_URL是FlightAware AeroAPI的基礎(chǔ)URL。
AEROAPI_KEY是用于身份驗(yàn)證的API密鑰。
會(huì)話管理:
get_api_session:這個(gè)嵌套函數(shù)初始化請(qǐng)求。會(huì)話對(duì)象使用API密鑰設(shè)置所需的報(bào)頭,并返回會(huì)話對(duì)象。該會(huì)話將處理所有API請(qǐng)求。
數(shù)據(jù)獲?。?/h4>
fetch_flight_data:這個(gè)函數(shù)接受flight_id和session作為參數(shù)。它使用適當(dāng)?shù)娜掌谶^濾器構(gòu)造端點(diǎn)URL,用于獲取一天的數(shù)據(jù),并發(fā)送GET請(qǐng)求以檢索航班數(shù)據(jù)。該函數(shù)處理API響應(yīng),并提取相關(guān)的航班信息。
時(shí)間轉(zhuǎn)換:
utc_to_local:根據(jù)所提供的時(shí)區(qū)字符串將UTC時(shí)間(來自API響應(yīng))轉(zhuǎn)換為本地時(shí)間。這個(gè)函數(shù)可以幫助我們獲得基于城市的到達(dá)和離開時(shí)間。
數(shù)據(jù)處理:
腳本根據(jù)估計(jì)或?qū)嶋H時(shí)間的可用性確定離開時(shí)間和到達(dá)時(shí)間的鍵,并返回到計(jì)劃時(shí)間。然后,它構(gòu)造一個(gè)含有格式化航班詳細(xì)信息的字典。
上面的截圖顯示了我們從FlightAware API收到的從迪拜飛往海得拉巴的阿聯(lián)酋航空EK524航班的響應(yīng)信息。請(qǐng)注意,到達(dá)和離開時(shí)間是基于城市的當(dāng)?shù)貢r(shí)間。
我們的目的是將該函數(shù)與GPT-4 Omni集成,使其能夠?qū)崟r(shí)訪問航班跟蹤信息。
第2步:用GPT- 4o實(shí)現(xiàn)函數(shù)調(diào)用
不妨從導(dǎo)入OpenAI庫并初始化它入手。
from openai import OpenAI
client = OpenAI()
這一行創(chuàng)建了OpenAI類的一個(gè)實(shí)例。這個(gè)實(shí)例(客戶端)將用于與OpenAI API交互。
我們將定義一個(gè)名為tools的列表,含有一個(gè)字典,該字典指定了函數(shù)get_flight_status。該函數(shù)旨在用作OpenAI API上下文中的工具,描述參數(shù)和所需輸入。
tools = [
{
"type": "function",
"function": {
"name": "get_flight_status",
"description": "Get status of a flight",
"parameters": {
"type": "object",
"properties": {
"flight": {
"type": "string",
"description": "Flight number"
}
},
"required": ["flight"]
}
}
}
]
繁重工作在下面的函數(shù)中進(jìn)行,其中LLM檢查提示以確定是否需要調(diào)用函數(shù)/工具,然后繼續(xù)生成適當(dāng)?shù)捻憫?yīng)。
def chatbot(prompt):
# Step 1: send the conversation and available functions to the model
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto"
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
# Step 2: check if the model wanted to call a function
if tool_calls:
available_functions = {
"get_flight_status": get_flight_status,
}
messages.append(response_message)
# Step 3: send the function response to the model
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(flight=function_args.get("flight"))
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
)
return final_response
這個(gè)函數(shù)chatbot接受用戶提示,并使用OpenAI API對(duì)其進(jìn)行處理。它將提示和定義的工具發(fā)送到OpenAI模型并處理響應(yīng)。
通過嵌入來自用戶的提示并將其發(fā)送到OpenAI API(chat.completion .create)來創(chuàng)建消息。API使用指定的工具(如果適用)處理這些消息。
比如說,當(dāng)我們發(fā)送提示“EK524的狀態(tài)是什么?”,GPT- 4o需要調(diào)用工具列表中提供的函數(shù),并返回以下響應(yīng):
注意,響應(yīng)包括函數(shù)(get_flight_status)和參數(shù)(EK226)。
下一步檢查是否調(diào)用了任何工具(即工具中的函數(shù))。它使用提供的參數(shù)執(zhí)行這些函數(shù),將它們的輸出集成到對(duì)話中,并將這些更新后的信息發(fā)回到OpenAI API以進(jìn)行進(jìn)一步處理。
# Step 2: check if the model wanted to call a function
if tool_calls:
available_functions = {
"get_flight_status": get_flight_status,
}
messages.append(response_message)
# Step 3: send the info for each function call and function response to the model
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(flight=function_args.get("flight"))
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
此時(shí),messages列表包括原始提示、帶有函數(shù)名和變量的初始響應(yīng)以及函數(shù)的實(shí)際輸出。下面的屏幕截圖顯示了含有所有要素的列表。
由于來自工具的響應(yīng)附加到歷史記錄中,我們可以調(diào)用聊天完成端點(diǎn),從LLM獲得最終答案。
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
)
return final_response
final_response對(duì)象有我們所尋找的答案:
將提示發(fā)送給函數(shù)chatbot將返回指定航班的實(shí)時(shí)狀態(tài)。
下面是本教程的完整代碼:
from openai import OpenAI
#Initialize the environment variable OPENAI_API_KEY with your api key
client = OpenAI()
#Function is available at
https://gist.github.com/janakiramm/2143b909626f5f01d64739e3fe90c9c8
tools = [
{
"type": "function",
"function": {
"name": "get_flight_status",
"description": "Get status of a flight",
"parameters": {
"type": "object",
"properties": {
"flight": {
"type": "string",
"description": "Flight number"
}
},
"required": ["flight"]
}
}
}
]
def chatbot(prompt):
# Step 1: send the conversation and available functions to the model
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto"
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
# Step 2: check if the model wanted to call a function
if tool_calls:
available_functions = {
"get_flight_status": get_flight_status,
}
messages.append(response_message)
# Step 3: send the info for each function call and function response to the model
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(flight=function_args.get("flight"))
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
)
return final_response
res=chatbot("What's the status of EK226?")
print(res.choices[0].message.content)
我們?cè)诒窘坛讨刑接懥巳绾瓮ㄟ^函數(shù)調(diào)用將實(shí)時(shí)數(shù)據(jù)引入LLM。在本系列的下一部分中,我們將把GPT-4o換成Gemini Pro,以探究相同的概念,但使用不同的模型。
原文標(biāo)題:How To Build a Real-Time App With GPT-4o Function Calling,作者:Janakiram MSV
鏈接:https://thenewstack.io/how-to-build-a-real-time-app-with-gpt-4o-function-calling/。
