Questa pagina mostra come utilizzare l'SDK Vertex AI per eseguire RAG Engine nelle attività della piattaforma di agenti Gemini Enterprise.
Puoi anche seguire le istruzioni utilizzando questo notebook Intro to RAG Engine.
Ruoli obbligatori
Concedi ruoli al tuo account utente. Esegui il seguente comando una volta per ciascuno dei seguenti
ruoli IAM:
roles/aiplatform.user
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Sostituisci quanto segue:
PROJECT_ID: il tuo ID progetto.USER_IDENTIFIER: l'identificatore del tuo account utente . Ad esempio:myemail@example.com.ROLE: il ruolo IAM che concedi al tuo account utente.
Prepara la tua console Google Cloud
Per utilizzare RAG Engine:
Esegui questo comando nella Google Cloud console per configurare il progetto.
gcloud config set project {project}Esegui questo comando per autorizzare l'accesso.
gcloud auth application-default login
Esegui RAG Engine
Copia e incolla questo codice campione nella console Google Cloud per eseguire RAG Engine.
Python
Per scoprire come installare o aggiornare l'SDK Vertex AI per Python, consulta Installare l'SDK Vertex AI per Python. Per saperne di più, consulta la documentazione di riferimento dell'API Python.
curl
Crea un corpus RAG.
export LOCATION=LOCATION export PROJECT_ID=PROJECT_ID export CORPUS_DISPLAY_NAME=CORPUS_DISPLAY_NAME // CreateRagCorpus // Output: CreateRagCorpusOperationMetadata curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora \ -d '{ "display_name" : "'"CORPUS_DISPLAY_NAME"'" }'Per saperne di più, vedi Creare un corpus RAG di esempio.
Importa un file RAG.
// ImportRagFiles // Import a single Cloud Storage file or all files in a Cloud Storage bucket. // Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID, GCS_URIS export RAG_CORPUS_ID=RAG_CORPUS_ID export GCS_URIS=GCS_URIS export CHUNK_SIZE=CHUNK_SIZE export CHUNK_OVERLAP=CHUNK_OVERLAP export EMBEDDING_MODEL_QPM_RATE=EMBEDDING_MODEL_QPM_RATE // Output: ImportRagFilesOperationMetadataNumber // Use ListRagFiles, or import_result_sink to get the correct rag_file_id. curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import \ -d '{ "import_rag_files_config": { "gcs_source": { "uris": "GCS_URIS" }, "rag_file_chunking_config": { "chunk_size": CHUNK_SIZE, "chunk_overlap": CHUNK_OVERLAP }, "max_embedding_requests_per_min": EMBEDDING_MODEL_QPM_RATE } }'Per saperne di più, consulta Esempio di importazione di file RAG.
Esegui una query di recupero RAG.
export RAG_CORPUS_RESOURCE=RAG_CORPUS_RESOURCE export VECTOR_DISTANCE_THRESHOLD=VECTOR_DISTANCE_THRESHOLD export SIMILARITY_TOP_K=SIMILARITY_TOP_K { "vertex_rag_store": { "rag_resources": { "rag_corpus": "RAG_CORPUS_RESOURCE" }, "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD }, "query": { "text": TEXT "similarity_top_k": SIMILARITY_TOP_K } } 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/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts"Per saperne di più, consulta l'API RAG Engine.
Crea contenuti.
{ "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 } } } } 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/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD"Per saperne di più, consulta l'API RAG Engine.
Passaggi successivi
Per scoprire di più sull'API RAG, consulta API RAG Engine.
Per saperne di più sulle risposte di RAG, consulta Output di recupero e generazione di RAG Engine.
Per scoprire di più su RAG Engine, consulta la panoramica di RAG Engine.