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

用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:

= = =

相关推荐
hunteritself8 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
测试者家园16 小时前
ChatGPT生成接口文档的方法与实践
软件测试·chatgpt·测试用例·接口测试·接口文档·ai赋能·用chatgpt做软件测试
小虚竹20 小时前
如何利用ChatGPT生成不同类型的文章大纲
chatgpt
AI小欧同学1 天前
【AIGC-ChatGPT进阶副业提示词】育儿锦囊:化解日常育儿难题的实用指南
chatgpt·aigc
测试者家园1 天前
ChatGPT接口测试用例生成的流程
软件测试·chatgpt·测试用例·接口测试·测试图书·质量效能·用chatgpt做测试
m0_748256561 天前
如何使用Python WebDriver爬取ChatGPT内容(完整教程)
开发语言·python·chatgpt
测试者家园2 天前
ChatGPT与接口测试工具的协作
软件测试·测试工具·chatgpt·接口测试·ai赋能·用chatgpt做软件测试·测试图书
SomeB1oody2 天前
获取OpenAI官方给ChatGPT的系统定义Prompt
人工智能·语言模型·chatgpt·prompt
Bruce_Liuxiaowei2 天前
谷歌量子计算机:开启计算新时代
google·量子计算
hunteritself2 天前
OpenAI直播发布第11天:ChatGPT桌面客户端升级,就这?
人工智能·gpt·chatgpt·语音识别·claude