【ChatGPT】【Gemini】-用Python调用google的Gemini API

news/2024/9/22 22:22:46/

用Python调用google的Gemini API

以下是Gemini自己给出的具体步骤和代码,我直接参考一次测试通过,大家可以参考。

Goodmao:Python call gemini API

Gemini

Absolutely, calling the Gemini API with Python involves a few steps:

Prerequisites:

  1. API Key: You'll need an API key to access Gemini. You can create one easily in Google AI Studio: [invalid URL removed].

  2. Python library: Install the google-generativeai library using pip:

    Bash

    pip install -U -q google-generativeai
    

    请谨慎使用代码。

Calling the API:

Here's a basic structure for calling the Gemini API for text generation:

Python

import googleapiclient.discovery as discovery# Replace with your API key obtained from AI Studio
API_KEY = "YOUR_API_KEY"# Define the project ID where your Gemini model resides
PROJECT_ID = "YOUR_PROJECT_ID"# Discovery URL for the Gemini API
service = discovery.build("generativeai", "v1beta1", developerKey=API_KEY
)# Text prompt for the model
prompt = "Write a poem about nature."# Define the model you want to use (replace with the actual model name)
model = "your_model_name"# Send the request with prompt and model details
body = {"prompt": prompt, "model": model}
response = service.projects().locations().models().generateText(parent=f"projects/{PROJECT_ID}/locations/global/models/{model}", body=body
)# Access the generated text from the response
generated_text = response["text"]
print(generated_text)

请谨慎使用代码。

Additional functionalities:

  • List Models: Use the service.projects().locations().models().list()method to explore available models.
  • Chat Conversation: Initiate a chat session with chat = model.start_chat()and interact with the model turn-by-turn.
  • Function Calling: Enable function calling during chat by setting enable_automatic_function_calling=True in start_chat().

Resources:

  • Get started with Gemini API (Python): https://ai.google.dev/gemini-api/docs/get-started/python
  • Gemini API: Function calling with Python: https://ai.google.dev/tutorials/function_calling_python_quickstart

= = = 


http://www.ppmy.cn/news/1431146.html

相关文章

php时间人性化展示

在PHP中,可以使用date()函数和strtotime()函数来实现时间的人性化展示。下面是一个示例代码: <?php // 获取当前时间戳 $timestamp = time();// 格式化时间 $formattedTime = date(Y年m月d日 H:i:s, $timestamp);echo

Java面试题:什么是Java中的单元测试以及如何编写单元测试?

Java中的单元测试是一种软件开发方法&#xff0c;用于验证代码中的最小可测试单元&#xff08;通常是方法或类&#xff09;是否按预期工作。单元测试的目标是隔离代码的各个部分&#xff0c;并验证每个部分是否都能正确执行其预期的功能。 在Java中&#xff0c;JUnit是最常用的…

Python 中的return语句用法,返回的数据类型

return 语句在 Python 中用于从一个函数中退出&#xff0c;并将表达式的值&#xff08;如果存在&#xff09;传递回给调用者。 1&#xff0c;return 语句的核心用法 1.1 返回值 当你想从函数中得到一个结果并用这个结果继续在其他地方执行时&#xff0c;可以使用 return 后紧…

怎么通过Javascript脚本实现远程控制一路开关

怎么通过Javascript脚本实现远程控制一路开关呢&#xff1f; 本文描述了使用Javascript脚本调用HTTP接口&#xff0c;实现控制一路开关。一路开关可控制一路照明、排风扇等电器。 可选用产品&#xff1a;可根据实际场景需求&#xff0c;选择对应的规格 序号设备名称1智能WiFi…

设计模式|原型模式(Prototype Pattern)

文章目录 什么是原型模式结构优缺点优点缺点举例代码示例原型模式vs复制(copy)什么是原型模式 原型模式(Prototype Pattern)是一种创建型设计模式,其核心思想是通过复制现有对象来创建新对象,而无需显式地指定它们的类。这种模式通常用于当对象的创建成本较高,或者对象…

码随想录-算法训练营day20【二叉树06:最大二叉树、合并二叉树、二叉搜索树中的搜索、验证二叉搜索树】

代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客 第六章 二叉树 part06 今日内容 ● 654.最大二叉树 ● 617.合并二叉树 ● 700.二叉搜索树中的搜索 ● 98.验证二叉搜索树 详细布置 654.最大二叉树 又是构造二叉树&#xff0c;昨天大家刚刚做完 中序后序确定二叉树…

鸿蒙OpenHarmony【轻量系统编译】 (基于Hi3861开发板)

编译 DevEco Device Tool支持Hi3861V100开发板的源码一键编译功能&#xff0c;提供编译工具链和编译环境依赖的检测及一键安装&#xff0c;简化复杂编译环境的同时&#xff0c;提升了编译的效率。 鸿蒙开发指导文档&#xff1a;gitee.com/li-shizhen-skin/harmony-os/blob/ma…

python中ctypes使用

前段时间接到了一个需求是给一个蓝牙的SDK测试接口的稳定性&#xff0c;将SDK的接口文档给你了&#xff0c;需要每个接口都写一个对应的测试用例&#xff0c;SDK 是用c写的&#xff0c;而我python用的比较熟练些&#xff0c;所有记录下在ctypes库的使用方法。 1 python和c中类…