2024-07-25 | Guilherme Rocha
上個月13號,OpenAI 在 Google I/O 活動前一天宣布了他們的最新模型 GPT-4o,讓大家大吃一驚。
GPT-4o 是什麼?
GPT-4o(“o”代表“omni”)是下一代模型,能處理文本、音頻和視頻輸入,並生成文本、音頻和圖像格式的輸出。這種統一方法意味著所有輸入都由同一神經網絡處理,從而帶來更連貫和平滑的互動。
它在英語和編碼任務上表現與 GPT-4 Turbo 類似,並且在非英語語言的文本處理方面更強。該 API 提升了速度,並降低了成本50%。
目前,API 只支持文本和圖像輸入,並輸出文本,但即將支持其他模式,如音頻。
多模態功能、實時互動和響應能力
在 GPT-4o 之前,ChatGPT 的語音模式需要三個獨立模型來執行以下任務:
- 將音頻轉換為文本
- 處理輸入和輸出文本
- 將文本轉回音頻
該模型無法有效處理語氣、多人對話或背景噪音,也無法輸出笑聲、歌聲或情感。
GPT-4o 現在將這些功能整合到一個模型中,更有效地理解和輸出情感。它還能模仿人類對話中的響應時間,平均為320毫秒。
實際應用——圖像處理
以下是一個來自 OpenAI 食譜的圖像處理示例。首先,安裝並升級 OpenAI SDK,然後初始化客戶端:
pip install --upgrade openai
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as an env var>"))
GPT-4o 可以直接處理圖片並根據圖片採取智能行動。我們可以提供兩種格式的圖片:Base64 編碼或 URL。
以下是查看我們將使用的圖片的程式:
from IPython.display import Image, display, Audio, Markdown
import base64
IMAGE_PATH = "data/triangle.png"
# Preview image for context
display(Image(IMAGE_PATH))
三角形示例
現在,我們可以將其編碼為 Base64 並發送給客戶端,例如,我們詢問三角形的面積:
# Open the image file and encode it as a base64 string
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
base64_image = encode_image(IMAGE_PATH)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant that responds in Markdown. Help me with my math homework!"},
{"role": "user", "content": [
{"type": "text", "text": "What's the area of the triangle?"},
{"type": "image_url", "image_url": {
"url": f"data:image/png;base64,{base64_image}"}
}
]}
],
temperature=0.0,
)
print(response.choices[0].message.content)
我們在控制台上得到了正確的答案:
To find the area of the triangle, we can use Heron's formula. First, we need to find the semi-perimeter of the triangle.
The sides of the triangle are 6, 5, and 9.
1. Calculate the semi-perimeter \( s \):
\[ s = \frac{a + b + c}{2} = \frac{6 + 5 + 9}{2} = 10 \]
2. Use Heron's formula to find the area \( A \):
\[ A = \sqrt{s(s-a)(s-b)(s-c)} \]
Substitute the values:
\[ A = \sqrt{10(10-6)(10-5)(10-9)} \]
\[ A = \sqrt{10 \cdot 4 \cdot 5 \cdot 1} \]
\[ A = \sqrt{200} \]
\[ A = 10\sqrt{2} \]
So, the area of the triangle is \( 10\sqrt{2} \) square units.
結論
結合包括音頻、視覺和文本在內的多種輸入模式,提高了模型在各種任務上的性能。這種方法使得理解和互動更加全面,接近人類處理信息的方式。
資料來源:https://medium.com/indiciumtech/exploring-the-new-gpt-4o-model-capabilities-865445fd31da