
如何快速实现REST API集成以优化业务流程
今天介绍下Gemini Pro API在colab和本地python运行的方案。
代码实现了以下功能:
auth.authenticate_user()
进行Google Cloud服务的身份验证。google.colab.auth
库,该代码确保用户已经通过Google账户登录并授权访问Google Cloud资源。!pip install
命令安装了google-generativeai
库,这是用于访问Google的Gemini模型的必要库。drive.mount('/content/drive')
将用户的Google Drive挂载到Colab环境中。这使得代码可以直接读写Google Drive中的文件。google.generativeai
和google.colab
模块。这些模块提供访问和操作Gemini模型以及用户数据的功能。gemini_api_secret_name
来存储API密钥的名称。userdata.get
方法从Colab的用户数据中获取Gemini API密钥。genai.GenerativeModel('gemini-pro')
初始化Gemini模型,允许代码进行自然语言生成操作。messages
来存储聊天信息。messages
列表中。chat_log_path
,用于在Google Drive中保存聊天日志。# Authenticate and grant access to Google Cloud servicesfrom google.colab import authauth.authenticate_user()
# Install necessary libraries!pip install -q -U google-generativeai
# Mount Google Drivefrom google.colab import drivedrive.mount('/content/drive')
# Import necessary librariesimport google.generativeai as genaifrom google.colab import userdata
# Configure Gemini API keygemini_api_secret_name = 'GOOGLE_API_KEY' # Change this if your secret name is different
try: # Retrieve the Gemini API key from Colab's secrets GOOGLE_API_KEY = userdata.get(gemini_api_secret_name) genai.configure(api_key=GOOGLE_API_KEY)except userdata.SecretNotFoundError as e: print(f"Secret not found: Ensure you have a secret named {gemini_api_secret_name} in Colab.") raise eexcept Exception as e: print("There was an error retrieving the API key.") raise e
# Initialize the Gemini modelmodel = genai.GenerativeModel('gemini-pro')
# Start a chatmessages = []
# Specify the path for the chat log file in your Google Drivechat_log_path = '/content/drive/My Drive/chat_log.txt'
while True: message = input("You: ") messages.append({ "role": "user", "parts": [message], })
response = model.generate_content(messages)
# Check if response is valid if not response.parts or not response.parts[0].text: print("Gemini: [No response or the response does not contain text]") continue
response_text = response.parts[0].text
messages.append({ "role": "model", "parts": [response_text], })
print("Gemini: " + response_text)
# Append the conversation to the file with open(chat_log_path, 'a') as file: file.write(f"You: {message}\n") file.write(f"Gemini: {response_text}\n")
上面的代码支持持续对话,且可以帮对话保存到谷歌云盘里。
代码的主要功能是使用Google Colab环境和Gemini模型对上传的图像进行分析。具体步骤和功能如下:
auth.authenticate_user()
进行Google Cloud服务的身份验证,确保用户已经通过Google账户登录并授权访问Google Cloud资源。google-generativeai
和pillow
库。google-generativeai
用于访问Google的Gemini模型,而pillow
是处理图像的Python库。google.generativeai
、PIL.Image
、google.colab.userdata
和google.colab.files
。genai.GenerativeModel('gemini-pro-vision')
初始化了Gemini模型的视觉处理版,允许代码对图像进行分析和生成操作。files.upload()
从本地文件系统上传图像。PIL.Image.open
读取上传的图像文件。!ls -lh
命令列出当前目录的内容,以验证文件是否已上传。IPython.display.Image
显示上传的图像,确保图像正确上传并可以在Notebook中查看。# Authenticate and grant access to Google Cloud servicesfrom google.colab import authauth.authenticate_user()
# Install necessary libraries!pip install -q -U google-generativeai pillow
# Import necessary librariesimport google.generativeai as genaiimport PIL.Imagefrom google.colab import userdatafrom google.colab import files
# Configure Gemini API keygemini_api_secret_name = 'GOOGLE_API_KEY' # Change this if your secret name is different
try: # Retrieve the Gemini API key from Colab's secrets GOOGLE_API_KEY = userdata.get(gemini_api_secret_name) genai.configure(api_key=GOOGLE_API_KEY)except userdata.SecretNotFoundError as e: print(f"Secret not found: Ensure you have a secret named {gemini_api_secret_name} in Colab.") raise eexcept Exception as e: print("There was an error retrieving the API key.") raise e
# Initialize the Gemini model for visionmodel = genai.GenerativeModel('gemini-pro-vision')
# Upload an image from your local file systemuploaded = files.upload()image_name = next(iter(uploaded))
# Read the image directly using the filenametry: img = PIL.Image.open(image_name)except PIL.UnidentifiedImageError: print("Error: The file uploaded is not a recognizable image. Please upload a valid image file.") raiseexcept Exception as e: print(f"An unexpected error occurred: {e}") raise
# Generate content based on the imageresponse = model.generate_content(["Tell me the content of the pic, please be detailed", img])
# Print the responseprint(response.text)# List the content of the directory to verify the file is uploaded!ls -lh
# Optionally: Display the image using a different library to check if it's correctly uploadedfrom IPython.display import Image, displaydisplay(Image(filename=image_name))
直接复制上述代码到colab即可,在左侧钥匙图标处,添加自己的谷歌API key。
在本地运行Python使用Gemini Pro API
我找到了一个github仓库:
https://github.com/unconv/gemini-testing作者从易到难给了好几个示例,直接git clone下来到本地学习即可。
我在本地运行py文件时先遇到了这个问题:
RetryError: Deadline of 60.0s exceeded while calling target function, last exception: 503 failed to connect to all addresses; last error: UNKNOWN:
解决办法是设置环境变量$http-proxy 和$https_proxy,可以直接把报错内容,你电脑的代理服务器、端口发给GPT4,它会给你详细指导。
解决好环境问题后,出现新的问题。
设置ADC地址:https://cloud.google.com/docs/authentication/provide-credentials-adc?hl=zh-cn#how-to
虽然它这里写了使用的API密钥,无需设置ADC。
但是它在本地运行时出现的消息是让我设置ADC。
谷歌的文档很细致,于是我跟着文档一步步操作下去。
安装后,我测试发现还有问题。
于是,接着下面的操作。
Gradio运行Gemini Pro API:
import google.generativeai as genaiimport gradio as grimport os
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat (history=[])
def chat_fun(message, history): return chat.send_message(message).text
gr.ChatInterface(chat_fun).queue().launch()
gradio会提供一个webui链接(在运行结果里出现)。
在这篇文章中,我探索了如何在不同环境下使用Gemini Pro API——从Google Colab的自动聊天和日志记录功能,到图像分析处理,再到本地Python环境中的实践应用。我还分享了可能遇到的问题及其解决方案,并提供了通过Gradio运行Gemini Pro API的示例代码。通过这些步骤,希望你能够顺利地使用Gemini Pro API。
文章转自微信公众号@kate人不错