远程调用gemini 3 pro api的完整教程(实战篇)
本文介绍了免费使用Gemini 3 Pro的方法:首先在Google AI Studio官网创建API密钥,然后通过Python环境配置。详细说明了两种代码实现方式,推荐使用环境变量设置API密钥的方法。文章包含具体操作步骤,包括创建conda环境、安装依赖库、编写调用代码等,并附上成功运行示例。最后展示了如何通过API进行简单对话测试,验证了该方法的可行性。
·
第一步:白嫖gemini 3 pro白嫖gemini 3 pro方法-CSDN博客
第二步:官网gemini3 pro创建密钥
官网链接:Google AI Studio
第三步:创建一个ai项目
python创建一个环境(这里我用的是conda)
conda create -n gemini python=3.11 -y
conda activate gemini
引入依赖
pip install -q -U google-genai


然后编写代码:(要替换你的key),而且你环境要导出这个key
export GOOGLE_API_KEY='your_api_key_here'
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash", contents="Explain how AI works in a few words"
)
print(response.text)
又或者改写代码:(同样替换你的key)(这个方式目前没成功过,咱们还是第一个方法)
from google import genai
import os
api_key = os.getenv('GOOGLE_API_KEY')
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash", contents="Explain how AI works in a few words"
)
print(response.text)
set GEMINI_API_KEY=您的API密钥(因为咱们是windows)
当然我试过了,目前只有下面这个方法可以成功。填入key,(然后关闭pycharm,在打开就好了)

运行结果:

然后咱们问一下问题。
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash", contents="夸一下我帅气"
)
print(response.text)

更多推荐


所有评论(0)