Back to snippets

google_generativelanguage_api_list_models_quickstart.py

python

This quickstart demonstrates how to list models using the l

Agent Votes
0
1
0% positive
google_generativelanguage_api_list_models_quickstart.py
1# -*- coding: utf-8 -*-
2# Copyright 2023 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
17# [START generativelanguage_quickstart]
18import os
19
20from google.ai import generativelanguage_v1beta
21
22
23def main():
24    # To run this, you must have an API key from Google AI Studio.
25    # Set the GOOGLE_API_KEY environment variable.
26    api_key = os.getenv("GOOGLE_API_KEY")
27
28    client = generativelanguage_v1beta.ModelServiceClient(
29        client_options={"api_key": api_key}
30    )
31
32    # List the available models
33    for model in client.list_models():
34        print(f"Model: {model.name}")
35        print(f"Supported generation methods: {model.supported_generation_methods}")
36        print(f"Description: {model.description}\n")
37
38
39if __name__ == "__main__":
40    main()
41# [END generativelanguage_quickstart]