本頁面說明如何建立新的 Example Store 執行個體,或重複使用現有的 Example Store 執行個體。開發 LLM 應用程式時,您可以將樣本儲存在樣本儲存庫中,並動態擷取樣本,用於 LLM 提示。
如要使用少樣本範例訓練大型語言模型或代理程式,您必須先為專案和位置建立或重複使用 Example Store 執行個體,然後將範例上傳至該執行個體。
每個專案和位置最多可有 50 個範例商店執行個體。建立樣本儲存庫執行個體後,即可在多個 LLM 應用程式和代理程式之間共用。
您可以使用兩種方式佈建 Example Store 執行個體:
建立新的 Example Store 執行個體:建立新的 Example Store 執行個體時,您需要指定嵌入模型,Example Store 會使用該模型判斷哪些範例與使用者的查詢相關。Example Store 支援下列嵌入模型:
text-embedding-005text-multilingual-embedding-002
建立 Example Store 執行個體後,就無法變更嵌入模型。如要使用其他嵌入模型,請建立另一個範例儲存庫。如要進一步瞭解文字嵌入,請參閱「取得文字嵌入」。
重複使用現有的樣本儲存庫執行個體:樣本儲存庫執行個體可供多個代理程式使用,因此您可以在 LLM 應用程式中存取儲存的樣本。重複使用現有 Example Store 執行個體時,無法變更嵌入模型。
必要條件
使用本頁的 Python 範例前,請先在本機 Python 環境中安裝並初始化 Agent Platform SDK。
執行下列指令,安裝 Example Store 的 Agent Platform SDK。
pip install --upgrade google-cloud-aiplatform>=1.87.0使用下列程式碼範例,匯入及初始化 Example Store 的 SDK。
import vertexai from vertexai.preview import example_stores vertexai.init( project="PROJECT_ID", location="LOCATION" )更改下列內容:
PROJECT_ID:專案 ID。
LOCATION:您的區域。僅支援
us-central1。
建立 Example Store 執行個體
請使用下列範例,為指定專案和位置建立 Example Store 執行個體。請注意,建立範例商店執行個體需要幾分鐘。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr。
import vertexai
from vertexai.preview import example_stores
vertexai.init(
project="PROJECT_ID",
location="LOCATION"
)
my_example_store = example_stores.ExampleStore.create(
example_store_config=example_stores.ExampleStoreConfig(
vertex_embedding_model="EMBEDDING_MODEL"
)
)
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:要建立範例商店的區域。目前僅支援
us-central1區域。 - EMBEDDING_MODEL:Example Store 執行個體使用的嵌入模型,用於判斷哪些範例與使用者的查詢相關。Example Store 支援下列嵌入模型:
text-embedding-005text-multilingual-embedding-002
REST
如要建立 ExampleStore 資源,請使用 exampleStores.create 方法傳送 POST 要求。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:專案 ID。
- LOCATION:要建立 Example Store 執行個體的區域。目前僅支援
us-central1區域。 - DISPLAY_NAME:Example Store 執行個體的名稱。
- EMBEDDING_MODEL:Example Store 執行個體使用的嵌入模型,用於判斷哪些範例與使用者的查詢相關。Example Store 支援下列嵌入模型:
textembedding-gecko@003text-embedding-004text-multilingual-embedding-002
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores
JSON 要求內文:
{
"display_name": "DISPLAY_NAME",
"example_store_config": {"vertex_embedding_model": EMBEDDING_MODEL}
}
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores"
PowerShell
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores" | Select-Object -Expand Content
您應該會收到類似以下的 JSON 回覆,其中 EXAMPLE_STORE_ID 代表 Example Store 執行個體的 ID。
重複使用現有的 Example Store 執行個體
請使用下列範例,針對特定專案和地點重複使用現有的 Example Store 執行個體。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr。
import vertexai
from vertexai.preview import example_stores
vertexai.init(
project="PROJECT_ID",
location="LOCATION"
)
example_store = example_stores.ExampleStore(
"EXAMPLE_STORE_NAME")
更改下列內容:
- PROJECT_ID:專案 ID。
- LOCATION:要建立範例商店的區域。目前僅支援
us-central1區域。 - EXAMPLE_STORE_NAME: 要重複使用的 Example Store 執行個體名稱。