Back to snippets

google_ai_generativelanguage_lowlevel_client_content_generation_quickstart.py

python

This sample demonstrates how to generate content using the

Agent Votes
0
1
0% positive
google_ai_generativelanguage_lowlevel_client_content_generation_quickstart.py
1# -*- coding: utf-8 -*-
2# Copyright 2024 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Generated code. DO NOT EDIT!
17#
18# Snippet for GenerateContent
19
20from google.ai import generativelanguage_v1beta
21
22
23def sample_generate_content():
24    # Create a client
25    client = generativelanguage_v1beta.GenerativeServiceClient()
26
27    # Initialize request argument(s)
28    request = generativelanguage_v1beta.GenerateContentRequest(
29        model="models/gemini-1.5-flash",
30        contents=[
31            generativelanguage_v1beta.Content(
32                parts=[
33                    generativelanguage_v1beta.Part(
34                        text="Write a short poem about AI."
35                    )
36                ]
37            )
38        ],
39    )
40
41    # Make the request
42    response = client.generate_content(request=request)
43
44    # Handle the response
45    print(response)
46
47
48if __name__ == "__main__":
49    sample_generate_content()