本實際作教學課程說明如何使用適用於PostgreSQL的 Azure 資料庫和 Azure OpenAI 來建置語意搜尋應用程式。
語意搜尋會根據語意進行搜尋。 標準語彙搜尋會根據查詢中提供的關鍵詞進行搜尋。 例如,您的食譜數據集可能不包含無麩質、素食、無乳製品、無水果或甜點等標籤,但這些特性可以從成分推斷。 這個想法是發出這類語意查詢並獲得相關的搜尋結果。
在本教學課程中,您會:
- 識別將參與搜尋的搜尋案例和數據欄位。
- 針對搜尋所涉及的每個數據欄位,建立對應的向量欄位,以儲存儲存在數據欄位中的值內嵌。
- 為所選資料欄位中的資料產生內嵌,並將內嵌儲存在對應的向量欄位中。
- 產生任何輸入搜尋查詢的內嵌。
- 搜尋向量資料欄位,並列出最接近的相鄰項目。
- 透過適當的相關性、排名和個人化模型執行結果,以產生最終排名。 如果沒有這類模型,請以遞減點乘積順序來排名結果。
- 監視模型、結果品質和商務計量,例如點擊率和停留時間。 納入意見反應機制,以偵錯和改善搜尋堆疊,從數據品質、數據新鮮度和個人化到用戶體驗。
先決條件
- 建立 OpenAI 帳戶並 要求存取 Azure OpenAI。
- 在所需的訂用帳戶中授與 Azure OpenAI 的存取權。
- 授與建立 Azure OpenAI 資源及部署模型的權限。
- 建立及部署 Azure OpenAI 資源和模型。 部署嵌入模型 text-embedding-ada-002。 複製部署名稱,因為您需要它來建立內嵌。
啟用azure_ai和 pgvector 延伸模組
在您可以啟用 azure_ai 和 pgvector 於 Azure 資料庫的 PostgreSQL 彈性伺服器執行個體上之前,必須先將它們新增至允許清單。 請執行 SHOW azure.extensions;,以確定它們已正確新增。
然後,您可以連線到目標資料庫並執行 CREATE EXTENSION 命令來安裝擴充功能。 您必須針對您想要讓擴充功能可供使用的每個資料庫個別重複命令。
CREATE EXTENSION azure_ai;
CREATE EXTENSION vector;
設定 OpenAI 端點和金鑰
在 Azure AI 服務中,在 [資源管理>金鑰和端點] 下,您可以找到 Azure AI 資源的端點和金鑰。 使用端點和其中一個金鑰來啟用 azure_ai 延伸模組,以叫用模型部署:
select azure_ai.set_setting('azure_openai.endpoint','https://<endpoint>.openai.azure.com');
select azure_ai.set_setting('azure_openai.subscription_key', '<API Key>');
下載數據
從 Kaggle 下載資料。
建立資料表
線上到您的伺服器並建立 test 資料庫。 在該資料庫中,使用下列命令來建立您要匯入資料的數據表:
CREATE TABLE public.recipes(
rid integer NOT NULL,
recipe_name text,
prep_time text,
cook_time text,
total_time text,
servings integer,
yield text,
ingredients text,
directions text,
rating real,
url text,
cuisine_path text,
nutrition text,
timing text,
img_src text,
PRIMARY KEY (rid)
);
匯入資料
在用戶端視窗上設定下列環境變數,將編碼設定為UTF-8。 此步驟是必要的,因為這個特定數據集使用 Windows-1252 編碼。
Rem on Windows
Set PGCLIENTENCODING=utf-8;
# on Unix based operating systems
export PGCLIENTENCODING=utf-8
將數據匯入您建立的數據表。 請注意,此數據集包含標頭數據列。
psql -d <database> -h <host> -U <user> -c "\copy recipes FROM <local recipe data file> DELIMITER ',' CSV HEADER"
新增資料行以儲存內嵌
將內嵌資料列新增至資料表:
ALTER TABLE recipes ADD COLUMN embedding vector(1536);
產生內嵌
使用 azure_ai 擴充功能為您的數據生成嵌入向量。 下列範例會向量化幾個欄位,並串連。
WITH ro AS (
SELECT ro.rid
FROM
recipes ro
WHERE
ro.embedding is null
LIMIT 500
)
UPDATE
recipes r
SET
embedding = azure_openai.create_embeddings('text-embedding-ada-002', r.recipe_name||' '||r.cuisine_path||' '||r.ingredients||' '||r.nutrition||' '||r.directions)
FROM
ro
WHERE
r.rid = ro.rid;
重複命令,直到沒有其他要處理的數據列為止。
秘訣
使用 LIMIT 值來試試。 使用高參數值時,語句可能會因 Azure OpenAI 採取的節流措施而中途失敗。 如果語句失敗,請至少等候一分鐘,然後再次執行命令。
搜尋
為了方便起見,請在資料庫中建立搜尋函式:
create function
recipe_search(searchQuery text, numResults int)
returns table(
recipeId int,
recipe_name text,
nutrition text,
score real)
as $$
declare
query_embedding vector(1536);
begin
query_embedding := (azure_openai.create_embeddings('text-embedding-ada-002', searchQuery));
return query
select
r.rid,
r.recipe_name,
r.nutrition,
(r.embedding <=> query_embedding)::real as score
from
recipes r
order by score asc limit numResults; -- cosine distance
end $$
language plpgsql;
現在只要叫用函式便可進行搜尋:
select recipeid, recipe_name, score from recipe_search('vegan recipes', 10);
並探索結果:
recipeid | recipe_name | score
----------+--------------------------------------------------------------+------------
829 | Avocado Toast (Vegan) | 0.15672222
836 | Vegetarian Tortilla Soup | 0.17583494
922 | Vegan Overnight Oats with Chia Seeds and Fruit | 0.17668104
600 | Spinach and Banana Power Smoothie | 0.1773768
519 | Smokey Butternut Squash Soup | 0.18031077
604 | Vegan Banana Muffins | 0.18287598
832 | Kale, Quinoa, and Avocado Salad with Lemon Dijon Vinaigrette | 0.18368931
617 | Hearty Breakfast Muffins | 0.18737361
946 | Chia Coconut Pudding with Coconut Milk | 0.1884186
468 | Spicy Oven-Roasted Plums | 0.18994217
(10 rows)
相關內容
- 整合適用於 PostgreSQL 的 Azure 資料庫與 Azure 認知服務
- 整合適用於 PostgreSQL 的 Azure 資料庫與 Azure Machine Learning 服務
- 使用適用於 PostgreSQL 的 Azure 資料庫中的 Azure OpenAI 產生向量內嵌
- 適用於 PostgreSQL 的 Azure 資料庫中的 Azure AI 擴充功能
- 使用適用於 PostgreSQL 的 Azure 資料庫建立 AI
- 使用適用於 PostgreSQL 的 Azure 資料庫和 Azure OpenAI 建立建議系統
- 在適用於 PostgreSQL 的 Azure 資料庫中啟用和使用 pgvector