更穩定可重現的嵌入向量
當你持續使用嵌入向量(embedding)時,會一再呼叫 OpenAI 的嵌入模型。為了讓這些呼叫更易於重複使用且模組化,最好自訂一個名為 create_embeddings() 的函式,能對任意數量的文字輸入輸出嵌入向量。這個練習就要帶你完成這件事!
本練習屬於課程
Introduction to Embeddings with the OpenAI API
練習說明
- 定義一個
create_embeddings()函式,將輸入texts送到嵌入模型,並回傳一個清單,裡面包含每個輸入文字的嵌入向量。 - 使用
create_embeddings()對short_description進行嵌入,並將嵌入向量萃取為單一清單後印出。 - 使用
create_embeddings()對list_of_descriptions進行嵌入並印出。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define a create_embeddings function
def create_embeddings(texts):
response = client.____(
model="text-embedding-3-small",
input=____
)
response_dict = response.model_dump()
return [data['____'] for data in ____['data']]
# Embed short_description and print
print(____)
# Embed list_of_descriptions and print
print(____)