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

= = =

相关推荐
胡耀超1 天前
13.快速构建领域知识库的完整指南:结合 ChatGPT 与 Python 提升效率
开发语言·python·chatgpt·知识图谱·知识库
AIGC大时代1 天前
ChatGPT从数据分析到内容写作建议相关的46个提示词分享!
人工智能·chatgpt
m0_748230941 天前
打造专业级ChatGPT风格聊天界面:SpringBoot与Vue实现动态打字机效果,附完整前后端源码
vue.js·spring boot·chatgpt
uncle_ll2 天前
ChatGPT大模型极简应用开发-CH4-GPT-4 和 ChatGPT 的高级技巧
人工智能·gpt·chatgpt·llm·微调
不是吧这都有重名2 天前
[Datawheel]利用Zigent框架编写智能体-2
人工智能·pytorch·windows·学习·机器学习·语言模型·chatgpt
掉头发的王富贵2 天前
【2024年终总结】深圳工作生活评测
笔记·chatgpt·生活·年度总结·2024
大懒猫软件2 天前
如何写出优秀的提示词?ChatGPT官方的六种方法
ai·chatgpt·prompt·命令模式
FreeBuf_3 天前
ChatGPT被曝存在爬虫漏洞,OpenAI未公开承认
爬虫·chatgpt
AIGC大时代3 天前
分享14分数据分析相关ChatGPT提示词
人工智能·chatgpt·数据分析
井底哇哇3 天前
ChatGPT是强人工智能吗?
人工智能·chatgpt