调用谷歌大模型API出现的错误解决

该文章已生成可运行项目,
Timeout of 600.0s exceeded, last exception: 503 failed to connect to all addresses; last error: UNAVAILABLE: ipv4:172.217.14.234:443: ConnectEx: Connection timed out (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

出现了错误:

Timeout of 600.0s exceeded, last exception: 503 failed to connect to all addresses; last error: UNAVAILABLE: ipv4:172.217.14.234:443: ConnectEx: Connection timed out (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

最近在尝试使用 Google 最新的 Gemini 模型进行一些开发测试。自然地,我选择了官方提供的 Python SDK google-generativeai。然后就出现了这个错误,挂了系统代理,但是应该是这个库不走这个系统代理.

解决办法很简单,,用request即可,放弃原生库

本文章已经生成可运行项目
要使用Python调用谷歌Gemini大模型API,可按以下步骤实现: ### 1. 安装必要的库 首先,要安装Google AI Python客户端库,可通过pip命令进行安装: ```bash pip install google-generativeai ``` ### 2. 设置API密钥 需在Google AI平台获取API密钥,然后在代码里设置环境变量: ```python import os os.environ["GOOGLE_API_KEY"] = "your_api_key" ``` ### 3. 调用Gemini API 以下是一个简单的示例代码,展示了如何使用Python调用谷歌Gemini API: ```python import google.generativeai as genai # 配置API密钥 genai.configure(api_key=os.environ["GOOGLE_API_KEY"]) # 选择模型 model = genai.GenerativeModel('gemini-pro') # 定义输入的提示信息 prompt = "请介绍一下Python编程语言" # 调用API生成响应 response = model.generate_content(prompt) # 输出响应内容 print(response.text) ``` ### 4. 详细解释 - **导入库**:`import google.generativeai as genai` 导入Google AI Python客户端库。 - **配置API密钥**:`genai.configure(api_key=os.environ["GOOGLE_API_KEY"])` 使用之前设置的API密钥配置客户端。 - **选择模型**:`model = genai.GenerativeModel('gemini-pro')` 选择要使用的Gemini模型,这里使用的是 `gemini-pro`。 - **定义提示信息**:`prompt = "请介绍一下Python编程语言"` 定义向模型输入的提示信息。 - **调用API**:`response = model.generate_content(prompt)` 调用API并传入提示信息,获取模型的响应。 - **输出响应内容**:`print(response.text)` 打印模型生成的文本响应。 ### 注意事项 - 要确保API密钥的安全性,不要在公开代码或存储库中暴露API密钥。 - 调用API可能会产生费用,需关注Google AI的定价政策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值