
使用 ASP.NET Web API 構建 RESTful API
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可以生成從文本到圖像等各種內容。這就為創意項目、研究工作和創新應用提供了令人興奮的可能性,而無需承擔經濟負擔。
冪簡集成API HUB也為大家匯集了很多Google系列API,若您有需要請訪問我們!
原文鏈接:Mastering the Google Gemini API