Fundierte Generierung mit Inline- und Vertex AI Search-Daten

Fundierte Generierung mit Inline- und Vertex AI Search-Daten

Weitere Informationen

Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:

Codebeispiel

Python

Weitere Informationen finden Sie in der Vertex AI Agent Builder Python API Referenzdokumentation.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Vertex AI Agent Builder zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_number = "YOUR_PROJECT_NUMBER"
# engine_id = "YOUR_ENGINE_ID"

client = discoveryengine.GroundedGenerationServiceClient()

request = discoveryengine.GenerateGroundedContentRequest(
    # The full resource name of the location.
    # Format: projects/{project_number}/locations/{location}
    location=client.common_location_path(project=project_number, location="global"),
    generation_spec=discoveryengine.GenerateGroundedContentRequest.GenerationSpec(
        model_id="gemini-1.5-flash",
    ),
    # Conversation between user and model
    contents=[
        discoveryengine.GroundedGenerationContent(
            role="user",
            parts=[
                discoveryengine.GroundedGenerationContent.Part(
                    text="How did Google do in 2020? Where can I find BigQuery docs?"
                )
            ],
        )
    ],
    system_instruction=discoveryengine.GroundedGenerationContent(
        parts=[
            discoveryengine.GroundedGenerationContent.Part(
                text="Add a smiley emoji after the answer."
            )
        ],
    ),
    # What to ground on.
    grounding_spec=discoveryengine.GenerateGroundedContentRequest.GroundingSpec(
        grounding_sources=[
            discoveryengine.GenerateGroundedContentRequest.GroundingSource(
                inline_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.InlineSource(
                    grounding_facts=[
                        discoveryengine.GroundingFact(
                            fact_text=(
                                "The BigQuery documentation can be found at https://cloud.google.com/bigquery/docs/introduction"
                            ),
                            attributes={
                                "title": "BigQuery Overview",
                                "uri": "https://cloud.google.com/bigquery/docs/introduction",
                            },
                        ),
                    ]
                ),
            ),
            discoveryengine.GenerateGroundedContentRequest.GroundingSource(
                search_source=discoveryengine.GenerateGroundedContentRequest.GroundingSource.SearchSource(
                    # The full resource name of the serving config for a Vertex AI Search App
                    serving_config=f"projects/{project_number}/locations/global/collections/default_collection/engines/{engine_id}/servingConfigs/default_search",
                ),
            ),
        ]
    ),
)
response = client.generate_grounded_content(request)

# Handle the response
print(response)

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.