LlamaIndex di Vertex AI untuk RAG API

Retrieval-Augmented Generation (RAG) adalah teknik yang meningkatkan kemampuan model generatif, khususnya, model bahasa besar (LLM). LLM menggabungkan kecanggihan LLM dengan sumber pengetahuan eksternal, seperti dokumen dan database, untuk menghasilkan respons yang lebih akurat dan informatif.

Untuk mengetahui informasi selengkapnya tentang cara kerja RAG, lihat Ringkasan Generasi Pengambilan-Augmented (RAG).

Model yang didukung

Model Versi
Gemini 1.5 Pro gemini-1.5-pro-001
Gemini 1.0 Pro Vision gemini-1.0-pro-vision-001
Gemini 1.0 Pro gemini-1.0-pro-001
gemini-1.0-pro-002
Gemini gemini-experimental

Contoh sintaksis

Sintaksis untuk membuat korpus RAG.

curl

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  http://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora\
  -d '{
  "display_name" : "...",
  "description": ".."
}'

Python

corpus = rag.create_corpus(display_name=..., description=...)
print(corpus)

Daftar parameter

Lihat contoh untuk detail penerapan.

Pengelolaan korpus

Untuk informasi tentang korpus RAG, lihat Pengelolaan Corpus.

Membuat RagCorpus

Parameter

display_name

Opsional: string

Nama tampilan RagCorpus.

description

Opsional: string

Deskripsi RagCorpus.

Mencantumkan RagCorpora

Parameter

page_size

Opsional: int

Ukuran halaman daftar standar.

page_token

Opsional: string

Token halaman daftar standar. Biasanya diperoleh dari [ListRagCorporaResponse.next_page_token][] dari panggilan [VertexRagDataService.ListRagCorpora][] sebelumnya.

Dapatkan RagCorpus

Parameter

rag_corpus_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

Menghapus RagCorpus

Parameter

rag_corpus_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

Upload RagFile

Parameter

rag_corpus_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

display_name

Opsional: string

Nama tampilan RagCorpus.

description

Opsional: string

Deskripsi RagCorpus.

Pengelolaan file

Untuk informasi tentang file RAG, lihat Pengelolaan Corpus.

Impor RagFile

Parameter

rag_corpus_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

gcs_source.uris

list

URI Cloud StorageStorage yang berisi file upload

google_drive_source.resource_id

Opsional: string

Jenis resource Google Drive.

google_drive_source.resource_ids.resource_type

Opsional: string

ID resource Google Drive.

rag_file_chunking_config.chunk_size

Opsional: int

Jumlah token yang harus dimiliki setiap potongan.

rag_file_chunking_config.chunk_overlap

Opsional: int

Jumlah token yang tumpang tindih antara dua potongan.

Mencantumkan RagFiles

Parameter

rag_corpus_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

page_size

Opsional: int

Ukuran halaman daftar standar.

page_token

Opsional: string

Token halaman daftar standar. Biasanya diperoleh dari [ListRagCorporaResponse.next_page_token][] dari panggilan [VertexRagDataService.ListRagCorpora][] sebelumnya.

Mendapatkan RagFile

Parameter

rag_file_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

Menghapus RagFile

Parameter

rag_file_id

string

ID resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

Pengambilan dan prediksi

Retrieval

Parameter Deskripsi
similarity_top_k Mengontrol jumlah maksimum konteks yang diambil.
vector_distance_threshold Hanya konteks dengan jarak yang lebih kecil dari nilai minimum yang dipertimbangkan.

Prediksi

Parameter

model_id

string

Model LLM untuk pembuatan konten.

rag_corpora

string

Nama resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}

text

string (list)

Teks ke LLM untuk pembuatan konten. Nilai maksimum: 1 daftar.

vector_distance_threshold

Opsional: double

Hanya konteks dengan jarak vektor yang lebih kecil dari nilai minimum yang ditampilkan.

similarity_top_k

Opsional: int

Jumlah konteks teratas yang akan diambil.

Contoh

Membuat korpus RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • CORPUS_DISPLAY_NAME: Nama tampilan RagCorpus.
  • CORPUS_DESCRIPTION: Deskripsi RagCorpus.

Metode HTTP dan URL:

POST http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora

Isi JSON permintaan:

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION"
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

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

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora"

PowerShell

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

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" | Select-Object -Expand Content
Anda akan menerima kode status yang berhasil (2xx).

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

corpus = rag.create_corpus(display_name=display_name, description=description)
print(corpus)

Mencantumkan korpus RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • PAGE_SIZE: Ukuran halaman daftar standar. Anda dapat menyesuaikan jumlah RagCorpora yang akan ditampilkan per halaman dengan memperbarui parameter page_size.
  • PAGE_TOKEN: Token halaman daftar standar. Biasanya diperoleh menggunakan ListRagCorporaResponse.next_page_token dari panggilan VertexRagDataService.ListRagCorpora sebelumnya.

Metode HTTP dan URL:

GET http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content
Anda akan menerima kode status yang berhasil (2xx) dan daftar RagCorpora pada PROJECT_ID yang diberikan.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

corpora = rag.list_corpora()
print(corpora)

Dapatkan korpus RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.

Metode HTTP dan URL:

GET http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan resource RagCorpus.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# corpus_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

corpus = rag.get_corpus(name=corpus_name)
print(corpus)

Menghapus korpus RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.

Metode HTTP dan URL:

DELETE http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X DELETE \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan DeleteOperationMetadata.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# corpus_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

rag.delete_corpus(name=corpus_name)
print(f"Corpus {corpus_name} deleted.")

Upload file RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.
  • INPUT_FILE: Jalur file lokal.
  • FILE_DISPLAY_NAME: Nama tampilan RagFile.
  • RAG_FILE_DESCRIPTION: Deskripsi RagFile.

Metode HTTP dan URL:

POST http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload

Isi JSON permintaan:

{
 "rag_file": {
  "display_name": "FILE_DISPLAY_NAME",
  "description": "RAG_FILE_DESCRIPTION"
 }
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama INPUT_FILE, lalu jalankan perintah berikut:

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @INPUT_FILE \
"http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload"

PowerShell

Simpan isi permintaan dalam file bernama INPUT_FILE, dan jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile INPUT_FILE `
-Uri "http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan resource RagFile. Komponen terakhir kolom RagFile.name adalah rag_file_id yang dihasilkan server.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# corpus_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# path = "path/to/local/file.txt"
# display_name = "file_display_name"
# description = "file description"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

rag_file = rag.upload_file(
    corpus_name=corpus_name,
    path=path,
    display_name=display_name,
    description=description,
)
print(rag_file)

Impor file RAG

File dan folder dapat diimpor dari Google Drive atau Cloud Storage.

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.
  • GCS_URIS: Daftar lokasi Cloud Storage. Contoh: gs://my-bucket1, gs://my-bucket2.
  • DRIVE_RESOURCE_ID: ID resource Google Drive. Contoh:
    • http://drive.go888ogle.com.fqhub.com/file/d/ABCDE
    • http://drive.go888ogle.com.fqhub.com/corp/drive/u/0/folders/ABCDEFG
  • DRIVE_RESOURCE_TYPE: Jenis resource Google Drive. Opsi:
    • RESOURCE_TYPE_FILE - File
    • RESOURCE_TYPE_FOLDER - Folder
  • CHUNK_SIZE: Opsional: Jumlah token yang harus dimiliki setiap potongan.
  • CHUNK_OVERLAP: Opsional: Jumlah token yang tumpang tindih di antara potongan.

Metode HTTP dan URL:

POST http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import

Isi JSON permintaan:

{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": GCS_URIS
    },
    "google_drive_source": {
      "resource_ids": {
        "resource_id": DRIVE_RESOURCE_ID,
        "resource_type": DRIVE_RESOURCE_TYPE
      },
    }
  }
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

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

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import"

PowerShell

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

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan resource ImportRagFilesOperationMetadata.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# corpus_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# paths = ["http://drive.go888ogle.com.fqhub.com/file/123", "gs://my_bucket/my_files_dir"]  # Supports Google Cloud Storage and Google Drive Links

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

response = rag.import_files(
    corpus_name=corpus_name,
    paths=paths,
    chunk_size=512,  # Optional
    chunk_overlap=100,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")

Mendapatkan file RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.
  • RAG_FILE_ID: ID resource RagFile.

Metode HTTP dan URL:

GET http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan resource RagFile.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# file_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

rag_file = rag.get_file(name=file_name)
print(rag_file)

Membuat daftar file RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.
  • PAGE_SIZE: Ukuran halaman daftar standar. Anda dapat menyesuaikan jumlah RagFiles yang akan ditampilkan per halaman dengan memperbarui parameter page_size.
  • PAGE_TOKEN: Token halaman daftar standar. Biasanya diperoleh menggunakan ListRagFilesResponse.next_page_token dari panggilan VertexRagDataService.ListRagFiles sebelumnya.

Metode HTTP dan URL:

GET http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X GET \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content
Anda akan menerima kode status yang berhasil (2xx) beserta daftar RagFiles pada RAG_CORPUS_ID yang diberikan.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# corpus_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

files = rag.list_files(corpus_name=corpus_name)
for file in files:
    print(file)

Menghapus file RAG

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • RAG_CORPUS_ID: ID resource RagCorpus.
  • RAG_FILE_ID: ID resource RagFile. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file_id}.

Metode HTTP dan URL:

DELETE http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Jalankan perintah berikut:

curl -X DELETE \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

PowerShell

Jalankan perintah berikut:

$headers = @{  }

Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan resource DeleteOperationMetadata.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# file_name = "projects/{project_id}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

rag.delete_file(name=file_name)
print(f"File {file_name} deleted.")

Kueri pengambilan

Saat pengguna mengajukan pertanyaan atau memberikan perintah, komponen pengambilan di RAG akan menelusuri pusat informasi untuk menemukan informasi yang relevan dengan kueri.

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • LOCATION: Region untuk memproses permintaan.
  • PROJECT_ID: Project ID Anda.
  • RAG_CORPUS_RESOURCE: Nama resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}.
  • VECTOR_DISTANCE_THRESHOLD: Hanya konteks dengan jarak vektor yang lebih kecil dari nilai minimum yang ditampilkan.
  • TEXT: Teks kueri untuk mendapatkan konteks yang relevan.
  • SIMILARITY_TOP_K: Jumlah konteks teratas yang akan diambil.

Metode HTTP dan URL:

POST http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts

Isi JSON permintaan:

{
 "vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE",
    },
    "vector_distance_threshold": 0.8
  },
  "query": {
   "text": "TEXT",
   "similarity_top_k": SIMILARITY_TOP_K
  }
 }

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

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

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts"

PowerShell

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

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" | Select-Object -Expand Content
Anda akan menerima kode status yang berhasil (2xx) dan daftar RagFiles terkait.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# rag_corpus_id = "9183965540115283968" # Only one corpus is supported at this time

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=rag_corpus_id,
            # Supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="What is RAG and why it is helpful?",
    similarity_top_k=10,  # Optional
    vector_distance_threshold=0.5,  # Optional
)
print(response)

Prediksi

Prediksi mengontrol metode LLM yang menghasilkan konten.

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: Project ID Anda.
  • LOCATION: Region untuk memproses permintaan.
  • MODEL_ID: Model LLM untuk pembuatan konten. Contoh: gemini-1.5-pro-001
  • GENERATION_METHOD: Metode LLM untuk pembuatan konten. Opsi: generateContent, streamGenerateContent
  • INPUT_PROMPT: Teks yang dikirim ke LLM untuk pembuatan konten. Coba gunakan perintah yang relevan dengan File rag yang diupload.
  • RAG_CORPUS_RESOURCE: Nama resource RagCorpus. Format: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}.
  • SIMILARITY_TOP_K: Opsional: Jumlah konteks teratas yang akan diambil.
  • VECTOR_DISTANCE_THRESHOLD: Opsional: Konteks dengan jarak vektor yang lebih kecil dari nilai minimum akan ditampilkan.

Metode HTTP dan URL:

POST http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD

Isi JSON permintaan:

{
 "contents": {
  "role": "user",
  "parts": {
    "text": "INPUT_PROMPT"
  }
 },
 "tools": {
  "retrieval": {
   "disable_attribution": false,
   "vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE",
    },
    "similarity_top_k": SIMILARITY_TOP_K,
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
   }
  }
 }
}

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

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

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD"

PowerShell

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

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "http://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" | Select-Object -Expand Content
Respons yang berhasil akan menampilkan konten yang dihasilkan beserta kutipan.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Python API.


from vertexai.preview import rag
from vertexai.preview.generative_models import GenerativeModel, Tool
import vertexai

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# rag_corpus_id = "9183965540115283968" # Only one corpus is supported at this time

# Initialize Vertex AI API once per session
vertexai.init(project=project_id, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=rag_corpus_id,  # Currently only 1 corpus is allowed.
                    # Supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            similarity_top_k=3,  # Optional
            vector_distance_threshold=0.5,  # Optional
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-1.0-pro-002", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
print(response.text)

Langkah Berikutnya