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

= = =

相关推荐
Lifeng666666669 小时前
chatgpt是怎么诞生的,详解GPT1到GPT4的演化之路及相关背景知识
论文阅读·人工智能·语言模型·chatgpt
大模型开发12 小时前
5分钟带你搞懂从0打造一个ChatGPT
chatgpt·程序员·llm
“初生”13 小时前
利用低汇率国家苹果订阅,120 元开通 ChatGPT Plus
chatgpt
mortimer16 小时前
用Gemini攻克小语种语音识别,生成广播级SRT字幕
人工智能·gemini
武子康16 小时前
AI炼丹日志-30-新发布【1T 万亿】参数量大模型!Kimi‑K2开源大模型解读与实践
人工智能·gpt·ai·语言模型·chatgpt·架构·开源
彬彬醤17 小时前
ChatGPT无法登陆?分步排查指南与解决方案
服务器·网络·数据库·网络协议·chatgpt
欧阳码农1 天前
5分钟带你搞懂从0打造一个ChatGPT
人工智能·机器学习·chatgpt
程序员爱钓鱼1 天前
Go语言实战案例-判断回文字符串-是不是正着念反着念都一样?
后端·google·go
码农明明1 天前
Google Play 应用上架二三事
android·google
Swift社区2 天前
日志不再孤立!用 Jaeger + TraceId 实现链路级定位
人工智能·chatgpt