做網(wǎng)站 做app好百度商城官網(wǎng)
本教程介紹了如何使用OpenAI最新的LLM GPT-4o通過函數(shù)調(diào)用將實時數(shù)據(jù)引入LLM。
我們在LLM函數(shù)調(diào)用指南(詳見https://thenewstack.io/a-comprehensive-guide-to-function-calling-in-llms/)中討論了如何將實時數(shù)據(jù)引入聊天機器人和代理?,F(xiàn)在,我們將通過將來自FlightAware.com的API與新的GPT-4o模型集成,進(jìn)一步探究這個概念,以便實時跟蹤航班狀態(tài)。
FlightAware的AeroAPI是一個可靠的充分利用REST的API,提供按需訪問航班跟蹤和狀態(tài)數(shù)據(jù)。它允許開發(fā)人員通過一個基于查詢的簡單系統(tǒng),獲取實時、歷史或未來的航班信息。API支持基于航班標(biāo)識符、飛機注冊號或機場或運營商等位置的詳細(xì)請求。它旨在以JSON格式提供精確、可操作的航空數(shù)據(jù),支持整個航空業(yè)從航空公司到機場的運營需求。
在繼續(xù)之前,注冊FlightAware并獲得API密鑰,這對于調(diào)用REST API至關(guān)重要。免費的個人套餐足以完成本教程。
第1步:定義獲取航班狀態(tài)的函數(shù)
一旦您獲得了API密鑰,用Python創(chuàng)建以下函數(shù)來檢索任何航班的狀態(tài)。
import astimport jsonimport randomfrom datetime import datetime, timedeltaimport requestsimport pytzdef 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 sessiondef 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ù)接受一個航班參數(shù)(假設(shè)是航班標(biāo)識符),并以JSON格式返回格式化的航班詳細(xì)信息。它查詢AeroAPI以根據(jù)給定的航班標(biāo)識符獲取航班數(shù)據(jù),并確定關(guān)鍵細(xì)節(jié)的格式,比如出發(fā)地、目的地、離開時間、到達(dá)時間和狀態(tài)。
不妨看看腳本的組件:
API憑據(jù):
AEROAPI_BASE_URL是FlightAware AeroAPI的基礎(chǔ)URL。
AEROAPI_KEY是用于身份驗證的API密鑰。
會話管理:
get_api_session:這個嵌套函數(shù)初始化請求。會話對象使用API密鑰設(shè)置所需的報頭,并返回會話對象。該會話將處理所有API請求。
數(shù)據(jù)獲取:
fetch_flight_data:這個函數(shù)接受flight_id和session作為參數(shù)。它使用適當(dāng)?shù)娜掌谶^濾器構(gòu)造端點URL,用于獲取一天的數(shù)據(jù),并發(fā)送GET請求以檢索航班數(shù)據(jù)。該函數(shù)處理API響應(yīng),并提取相關(guān)的航班信息。
時間轉(zhuǎn)換:
utc_to_local:根據(jù)所提供的時區(qū)字符串將UTC時間(來自API響應(yīng))轉(zhuǎn)換為本地時間。這個函數(shù)可以幫助我們獲得基于城市的到達(dá)和離開時間。
數(shù)據(jù)處理:
腳本根據(jù)估計或?qū)嶋H時間的可用性確定離開時間和到達(dá)時間的鍵,并返回到計劃時間。然后,它構(gòu)造一個含有格式化航班詳細(xì)信息的字典。
上面的截圖顯示了我們從FlightAware API收到的從迪拜飛往海得拉巴的阿聯(lián)酋航空EK524航班的響應(yīng)信息。請注意,到達(dá)和離開時間是基于城市的當(dāng)?shù)貢r間。
我們的目的是將該函數(shù)與GPT-4 Omni集成,使其能夠?qū)崟r訪問航班跟蹤信息。
第2步:用GPT- 4o實現(xiàn)函數(shù)調(diào)用
不妨從導(dǎo)入OpenAI庫并初始化它入手。
from openai import OpenAI
client = OpenAI()
這一行創(chuàng)建了OpenAI類的一個實例。這個實例(客戶端)將用于與OpenAI API交互。
我們將定義一個名為tools的列表,含有一個字典,該字典指定了函數(shù)get_flight_status。該函數(shù)旨在用作OpenAI API上下文中的工具,描述參數(shù)和所需輸入。
繁重工作在下面的函數(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 modelmessages = [{"role": "user", "content": prompt}]response = client.chat.completions.create(model="gpt-4o",messages=messages,tools=tools,tool_choice="auto")response_message = response.choices[0].messagetool_calls = response_message.tool_calls# Step 2: check if the model wanted to call a functionif tool_calls:available_functions = {"get_flight_status": get_flight_status,} messages.append(response_message) # Step 3: send the function response to the modelfor tool_call in tool_calls:function_name = tool_call.function.namefunction_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
這個函數(shù)chatbot接受用戶提示,并使用OpenAI API對其進(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ù),將它們的輸出集成到對話中,并將這些更新后的信息發(fā)回到OpenAI API以進(jìn)行進(jìn)一步處理。
# Step 2: check if the model wanted to call a functionif 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 modelfor tool_call in tool_calls:function_name = tool_call.function.namefunction_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,})
此時,messages列表包括原始提示、帶有函數(shù)名和變量的初始響應(yīng)以及函數(shù)的實際輸出。下面的屏幕截圖顯示了含有所有要素的列表。
由于來自工具的響應(yīng)附加到歷史記錄中,我們可以調(diào)用聊天完成端點,從LLM獲得最終答案。
final_response = client.chat.completions.create(model="gpt-4o",messages=messages,) return final_response
final_response對象有我們所尋找的答案:
將提示發(fā)送給函數(shù)chatbot將返回指定航班的實時狀態(tài)。
下面是本教程的完整代碼:
from openai import OpenAI#Initialize the environment variable OPENAI_API_KEY with your api keyclient = OpenAI()#Function is available at
https://gist.github.com/janakiramm/2143b909626f5f01d64739e3fe90c9c8tools = [{"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 modelmessages = [{"role": "user", "content": prompt}]response = client.chat.completions.create(model="gpt-4o",messages=messages,tools=tools,tool_choice="auto")response_message = response.choices[0].messagetool_calls = response_message.tool_calls# Step 2: check if the model wanted to call a functionif 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 modelfor tool_call in tool_calls:function_name = tool_call.function.namefunction_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_responseres=chatbot("What's the status of EK226?")print(res.choices[0].message.content)
我們在本教程中探討了如何通過函數(shù)調(diào)用將實時數(shù)據(jù)引入LLM。在本系列的下一部分中,我們將把GPT-4o換成Gemini Pro,以探究相同的概念,但使用不同的模型。