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

= = =

相关推荐
陈敬雷-充电了么-CEO兼CTO8 小时前
DeepSeek核心算法解析:如何打造比肩ChatGPT的国产大模型
人工智能·神经网络·自然语言处理·chatgpt·大模型·aigc·deepseek
我们的五年1 天前
DeepSeek 和 ChatGPT 在特定任务中的表现:逻辑推理与创意生成
人工智能·chatgpt·ai作画·deepseek
浮华落定1 天前
DeepSeek+即梦 做AI视频
人工智能·chatgpt·音视频
AskHarries1 天前
利用acme.sh 申请 Google 免费证书
后端·google
奇偶变不变2 天前
过于依赖chatgpt编程会有哪些弊端?
人工智能·chatgpt
mygodalien2 天前
Win7编译GPU版llama.cpp部署deepseek-r1等大模型记录
人工智能·机器学习·chatgpt·llama
赵钰老师2 天前
【科研创新与智能化转型】AI智能体开发与大语言模型的本地化部署、优化技术
人工智能·语言模型·自然语言处理·chatgpt·数据分析
qq_433502182 天前
Flowith.io 初探:DeepSeek-R1免费用,用画布式 AI 提升效率和创意
人工智能·经验分享·chatgpt·学习方法·ai写作·知识库·画布
月光技术杂谈2 天前
关于DeepSeek与ChatGPT等模型的原始训练数据
chatgpt·语料库·训练数据·ai模型·数据分类·deepseek·训练样本