Mengambil contoh

Halaman ini menunjukkan cara mengambil contoh dari Contoh Store Anda. Anda memiliki opsi berikut untuk mengambil contoh:

  • FetchExamples: Mengambil semua contoh yang sama persis dengan kriteria pemfilteran Anda. Gunakan opsi ini jika Anda hanya memiliki beberapa contoh atau memerlukan latensi yang lebih rendah.

  • SearchExamples: Mengambil contoh menggunakan penelusuran kemiripan antara kueri pengguna dan contoh yang disimpan. Gunakan opsi ini jika Anda memiliki banyak contoh.

Prasyarat

Sebelum menggunakan contoh Python di halaman ini, instal dan lakukan inisialisasi Vertex AI SDK untuk Python di lingkungan Python lokal Anda.

  1. Jalankan perintah berikut untuk menginstal Vertex AI SDK untuk Python untuk Example Store.

    pip install --upgrade google-cloud-aiplatform>=1.87.0
  2. Gunakan contoh kode berikut untuk mengimpor dan melakukan inisialisasi SDK untuk Example Store.

    import vertexai
    from vertexai.preview import example_stores
    
    vertexai.init(
      project="PROJECT_ID",
      location="LOCATION"
    )
    

    Ganti kode berikut:

    • PROJECT_ID: Project ID Anda.

    • LOCATION: Region Anda. Hanya us-central1 yang didukung.

Contoh pengambilan

Gunakan contoh berikut untuk mengambil contoh. FetchExamples mengambil semua contoh yang sama persis dengan kriteria pemfilteran Anda.

Vertex AI SDK untuk Python

Kode berikut menampilkan semua contoh di Example Store Anda, hingga 100 per halaman:

from vertexai.preview import example_stores

example_store = example_stores.ExampleStore(EXAMPLE_STORE_NAME)
# Returns the dictionary representation of FetchExamplesResponse.
examples = example_store.fetch_examples()

Anda dapat menggunakan function_names untuk menentukan satu atau beberapa filter yang membatasi contoh yang ditampilkan. Contoh berikut hanya menampilkan contoh yang menyertakan fungsi flight_booking_tool dan hotel_booking_tool:

# Returns examples that include either tool.
example_store.fetch_examples(
    filter={
        "function_names": {
            "values": ["flight_booking_tool", "hotel_booking_tool"],
            "array_operator": "CONTAINS_ANY"
        }
    }
)

# Returns examples that include *both* tools.
example_store.fetch_examples(
    filter={
        "function_names": {
            "values": ["flight_booking_tool", "hotel_booking_tool"],
            "array_operator": "CONTAINS_ALL"
        }
    }
)

Anda dapat menggunakan filter search_keys untuk membatasi contoh yang ditampilkan oleh kunci penelusurannya.

# Returns examples that include any of the following search keys.
example_store.fetch_examples(
    filter={"search_keys": ["How do I get to the airport?"]}
)

Anda dapat menggunakan filter example_ids untuk membatasi contoh yang ditampilkan oleh ID Contoh. ID contoh menggunakan format exampleTypes/stored_contents_example/examples/<var>EXAMPLE_ID</var>, dengan EXAMPLE_ID mewakili ID numerik yang dihasilkan untuk contoh.

# Returns examples that have any of the following Example IDs.
example_store.fetch_examples(
  example_ids=["exampleTypes/stored_contents_example/examples/09b1d383f92c47e7a2583a44ebbc7854"]
)

REST API

Untuk mengambil contoh, kirim permintaan POST menggunakan metode exampleStores.fetchExamples.

Filter function_names yang ditentukan dalam contoh isi JSON permintaan hanya menampilkan contoh yang menyertakan fungsi flight_booking_tool dan hotel_booking_tool:

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region tempat Anda ingin membuat contoh toko. Satu-satunya wilayah yang didukung adalah us-central1.
  • EXAMPLE_STORE_ID: ID instance Contoh Toko tempat Anda ingin mengupload contoh.

Metode HTTP dan URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores/EXAMPLE_STORE_ID:fetchExamples

Isi JSON permintaan:

{
  "stored_contents_example_filter": {
    "function_names": {
      "values": ["flight_booking_tool", "hotel_booking_tool"],
      "array_operator": "CONTAINS_ANY"
     }
  }
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

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/EXAMPLE_STORE_ID:fetchExamples"

PowerShell

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$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/EXAMPLE_STORE_ID:fetchExamples" | Select-Object -Expand Content

Anda akan menerima respons JSON yang mirip dengan berikut, dengan EXAMPLE_ID mewakili ID yang dihasilkan untuk contoh.

Contoh penelusuran

Example Store menemukan contoh yang paling relevan berdasarkan skor kesamaan antara stored_contents_example_key dan kunci penelusuran contoh yang disimpan. Menggunakan contoh yang relevan dengan percakapan akan membantu model mempelajari perilaku yang diharapkan.

Vertex AI SDK untuk Python

Gunakan contoh kode berikut untuk menelusuri contoh yang relevan menggunakan Vertex AI SDK untuk Python:

example_store.search_examples(
    parameters={
        "stored_contents_example_key": "what's the weather in nyc"
    },
    # Only fetch the most similar examaple. The default value is 3.
    top_k=1
)

"""
Response -- dictionary representation of SearchExamplesResponse.
{'results': [{'example': {'exampleId': 'exampleTypes/stored_contents_example/examples/16834837b178453783e471b459d99195',
    'storedContentsExample': {'searchKey': 'What is the weather like in Boston?',
    'contentsExample': {'contents': [{'role': 'user',
        'parts': [{'text': 'What is the weather like in Boston?'}]}],
      'expectedContents': [{'content': {'parts': [{'functionCall': {'name': 'get_current_weather',
            'args': {'location': 'New York, NY'}}}]}},
      {'content': {'parts': [{'functionResponse': {'name': 'get_current_weather',
            'response': {'humidity': 65.0,
            'description': 'Partly Cloudy',
            'icon': 'partly-cloudy',
            'temperature': 38.0,
            'location': 'Boston, MA',
            'wind': {'speed': 10.0, 'direction': 'NW'}}}}]}},
      {'content': {'role': 'model',
        'parts': [{'text': 'The weather in Boston is 38 degrees and partly cloudy.'}]}}]}}},
  'similarityScore': 0.73527116}]}
"""

Anda dapat menggunakan filter function_names untuk membatasi contoh yang disertakan dalam penelusuran kemiripan.

example_store.search_examples(
  parameters={
    "stored_contents_example_key": "What's the weather in nyc",
    "function_names": {
      "values": ["weather_tool", "hotel_booking_tool"],
      "array_operator": "CONTAINS_ANY"
    }
  }
)