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

= = =

相关推荐
kakaZhui2 小时前
【多模态大模型】GLM-4-Voice端到端语音交互机器人VoiceAI
人工智能·chatgpt·机器人·aigc·llama
wlsjdszls16 小时前
ChatGPT免费背后的技术暗战 国产数字孪生如何打造“虚实共生”新生态?
人工智能·信息可视化·chatgpt
村中少年17 小时前
使用DeepSeek/chatgpt等AI工具辅助网络协议流量数据包分析
网络协议·chatgpt·wireshark·json·aigc
蜂耘18 小时前
ChatGPT入驻Safari,AI搜索时代加速到来
人工智能·chatgpt·safari
智模睿脑君19 小时前
Ollama部署本地大模型DeepSeek-R1-Distill-Llama-70B
人工智能·深度学习·语言模型·自然语言处理·chatgpt·transformer·llama
赵钰老师1 天前
【SWAT模型应用】AI辅助下基于ArcGIS Pro的SWAT模型全流程高效建模实践与深度进阶应用
人工智能·arcgis·chatgpt·数据分析
CPU NULL2 天前
ChatGPT各模型版本对比分析
ai·语言模型·chatgpt·大语言模型
m0_748234713 天前
AI语言模型的技术之争:DeepSeek与ChatGPT的架构与训练揭秘
人工智能·语言模型·chatgpt
我们的五年3 天前
硬件加速与技术创新双轮驱动:DeepSeek和ChatGPT性能进阶的未来蓝图
人工智能·chatgpt·自动驾驶·汽车·deepseek
小赖同学啊3 天前
jmeter 接入deepseek 或者chatgpt
jmeter·chatgpt