
基于 API 的 SaaS:定义、优势和挑战
在本 Google Gemini API使用指南中,我将解释如何使用 Google Gemini API从提示生成文本。 此外,我将演示如何使用 Google Gemini Vision 模型通过传递图像来生成内容。本教程将非常精彩,让我们开始吧!
要利用 Google Gemini Pro 的强大功能处理文本和聊天对话,并利用 Google Gemini Pro Vision 来处理图像,您需要“google-generativeai”软件包。首先使用“!pip install google-generativeai”安装它。 🔥
1、安装:
!pip install google-generativeai
2、导入一些重要的库
import google.generativeai as genai
import os
from google.colab import userdata
from IPython.display import Markdown
#give gemini api to google generative AI
geminiKey = userdata.get('geminiKey')
genai.configure(api_key = geminiKey)
该代码用于通过配置 API 密钥来设置与 Google Gemini API交互的身份验证,您可以从 Google AI studio 对其进行评估,然后将其设置到您的环境中。
# get the model
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('Explain me Quantum Computing like I’m a 5-year-old ')
print(response.text)
打印 response.text 会输出生成的文本,让你看到生成模型用简单的语言解释了量子计算。
此提示的输出结果:像 5 岁小孩一样给我解释量子计算
Markdown(response.text)
通过附带 response.text,我们将生成的原始文本转换为 Markdown 格式。
以 Markdown 格式输出
再次加载 Gemini pro 模型
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
response = chat.send_message('What is Quantum physics')
:
本提示的输出:什么是量子物理学
response = chat.send_message('Explain me LLM in simple words')
Markdown(response.text)
此提示的输出:用简单的话解释一下 LLM
for text in chat.history:
display(Markdown(f"**{text.role}**: {text.parts[0].text}"))
这段代码会遍历聊天记录,并以格式化的方式显示每个提示和交叉回复。
首先,导入图像
from PIL import Image
image = Image.open('/content/tesla truck.jpg')
让我们与谷歌生成式人工智能模型–Gemini-Pro Vision 进行互动。
model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(image)
Markdown(response.text)
model = genai.GenerativeModel('gemini-pro-vision')
:
response = model.generate_content(image)
:
Markdown(response.text)
:
模型在图像上的输出
我们还可以根据图片生成内容,如博客文章,要根据图片生成内容,我们需要通过图片提示。
response = model.generate_content(['Write a blog post about that image', image])
Markdown(response.text)
我们已经探索了 Google Gemini API的功能,并演示了如何在不产生成本的情况下利用它来生成内容。Google Gemini API可以生成从文本到图像等各种内容。这就为创意项目、研究工作和创新应用提供了令人兴奋的可能性,而无需承担经济负担。