開始使用免費開始

建立嵌入向量

在這個練習中,你將使用 OpenAI API 建立你的第一個嵌入向量(embedding)。一般來說,與 OpenAI API 互動需要 OpenAI API 金鑰,而且建立嵌入向量會產生成本。不過,在本課程中,你不需要建立或提供 API 金鑰

程式碼中已提供 <OPENAI_API_TOKEN> 佔位符,足以在本課程的練習中送出有效請求。若你在課程中任何時候遇到 RateLimitError,請稍等片刻再試一次。

整個課程中都會替你匯入 openai 函式庫的 OpenAI 類別,而在本練習之後,client 物件也會替你建立好。

本練習屬於課程

Introduction to Embeddings with the OpenAI API

檢視課程

練習說明

  • 建立一個 OpenAI 用戶端(可以保留 api_key 為提供的佔位符)。
  • 向 Embeddings 端點建立請求,使用 text-embedding-3-small 模型並傳入任意你想要的文字。
  • 將模型的 response 轉換為字典。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create an OpenAI client
client = ____(api_key="")

# Create a request to obtain embeddings
response = ____

# Convert the response into a dictionary
response_dict = ____
print(response_dict)
編輯並執行程式碼