
使用 ASP.NET Web API 構(gòu)建 RESTful API
1、安裝:
!pip install google-generativeai
2、導(dǎo)入一些重要的庫
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 密鑰來設(shè)置與 Google Gemini API交互的身份驗(yàn)證,您可以從 Google AI studio 對其進(jìn)行評估,然后將其設(shè)置到您的環(huán)境中。
# 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 會輸出生成的文本,讓你看到生成模型用簡單的語言解釋了量子計算。
此提示的輸出結(jié)果:像 5 歲小孩一樣給我解釋量子計算
Markdown(response.text)
通過附帶 response.text,我們將生成的原始文本轉(zhuǎn)換為 Markdown 格式。
以 Markdown 格式輸出
再次加載 Gemini pro 模型
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
response = chat.send_message('What is Quantum physics')
:
本提示的輸出:什么是量子物理學(xué)
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}"))
這段代碼會遍歷聊天記錄,并以格式化的方式顯示每個提示和交叉回復(fù)。
首先,導(dǎo)入圖像
from PIL import Image
image = Image.open('/content/tesla truck.jpg')
讓我們與谷歌生成式人工智能模型–Gemini-Pro Vision 進(jìn)行互動。
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)
:
模型在圖像上的輸出
我們還可以根據(jù)圖片生成內(nèi)容,如博客文章,要根據(jù)圖片生成內(nèi)容,我們需要通過圖片提示。
response = model.generate_content(['Write a blog post about that image', image])
Markdown(response.text)
我們已經(jīng)探索了 Google Gemini API的功能,并演示了如何在不產(chǎn)生成本的情況下利用它來生成內(nèi)容。Google Gemini API可以生成從文本到圖像等各種內(nèi)容。這就為創(chuàng)意項目、研究工作和創(chuàng)新應(yīng)用提供了令人興奮的可能性,而無需承擔(dān)經(jīng)濟(jì)負(fù)擔(dān)。
冪簡集成API HUB也為大家匯集了很多Google系列API,若您有需要請訪問我們!
原文鏈接:Mastering the Google Gemini API
使用 ASP.NET Web API 構(gòu)建 RESTful API
API安全:基于令牌的驗(yàn)證 vs 基于密鑰的驗(yàn)證,哪種更可靠?
RESTful Web API 設(shè)計中要避免的 6 個常見錯誤
深入解析API Gateway:微服務(wù)架構(gòu)中的關(guān)鍵組件及其重要功能
REST API設(shè)計開源工具:值得推薦的10+款
實(shí)測:阿里云百煉上線「全周期 MCP 服務(wù)」,AI 工具一站式托管
使用.Net構(gòu)建一個RESTful Web API
如何獲取 Seeed 開放平臺 API Key 密鑰(分步指南)
使用LoRA(低秩適應(yīng))微調(diào)大型語言模型的實(shí)用技巧