分享
Gemini API快速调用
输入“/”快速插入内容
Gemini API快速调用
通过API-Key调用Gemini模型
•
API调用流程测试
代码块
Python
import google.generativeai as genai
from google.generativeai import GenerativeModel
代码块
Python
genai.configure(api_key='AIzaSyDX4AaXpLtsCFMmQSIdmnweCVp8Npbulf4')
代码块
Python
model = GenerativeModel('gemini-pro')
response = model.generate_content('The opposite of hot is')
print(response.text)
代码块
Plain Text
cold
•
多轮对话测试
代码块
Python
from IPython.display import Markdown
from IPython.display import display
import pathlib
import textwrap
def to_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
代码块
Python
chat = model.start_chat()
代码块
Python
to_markdown(chat.send_message("请问什么是机器学习?").text)
机器学习(Machine Learning,简称ML)是人工智能(Artificial Intelligence,简称AI)的一个子领域,它使计算机能够在没有被明确编程的情况下,通过经验学习和适应来执行任务。机器学习算法可以通过分析数据来创建模型,然后使用该模型来做出预测或决定。
机器学习有两种主要类型:
•
**监督学习(Supervised Learning):**在监督学习中,计算机使用标记的数据(即包含正确答案的数据)来训练模型。然后,模型可以使用新数据来做出预测。
•
**无监督学习(Unsupervised Learning):**在无监督学习中,计算机使用未标记的数据来训练模型。模型必须自己找出数据中的模式,然后使用这些模式来做出预测。
机器学习被用于许多领域,包括:
•
**图像识别(Image Recognition):**计算机学习识别图像中的物体。
•
**自然语言处理(Natural Language Processing):**计算机学习理解和生成人类语言。
•
**语音识别(Speech Recognition):**计算机学习理解人类的语音。
•
**推荐系统(Recommendation Systems):**计算机学习向用户推荐产品或服务。
•
**欺诈检测(Fraud Detection):**计算机学习识别欺诈性交易。
•
**医学诊断(Medical Diagnosis):**计算机学习识别疾病,并推荐治疗方案。
机器学习是一个快速发展的领域,新的算法和应用不断涌现。机器学习有望在未来几年内继续改变我们的世界。
希望这个回答对您有所帮助!如果您还有其他问题,请随时问我。
代码块
Python
chat.send_message("我的上一个问题是?").text
代码块
Plain Text
'您的上一个问题是:“机器学习是什么?”\n\n您问这个问题是为了了解机器学习的基本概念和应用。我给出了一个全面的回答,解释了机器学习的定义、类型、算法和应用领域。希望我的回答对您有所帮助。\n\n如果您还有其他问题,请随时问我。'
•
多模态功能测试
代码块
Python
import PIL.Image
image = PIL.Image.open('image.jpg')
代码块
Python
image
代码块
Python
multimodal_model = GenerativeModel('gemini-pro-vision')
response = multimodal_model.generate_content(['请帮我介绍下图片上展示的内容', image])
print(response.text)
代码块
Plain Text
图片上展示了一只可爱的小猫在草地上奔跑。小猫全身都是蓬松的毛发,大大的眼睛炯炯有神,尾巴高高的翘起,看起来非常高兴。它在草地上奔跑,享受着自由自在的感觉。
通过Vertex AI调用Gemini模型
代码块
Python
# 安装google-cloud-aiplatform
!pip install google-cloud-aiplatform
代码块
Python
# 导入生成式AI库
from vertexai.preview.generative_models import GenerativeModel, Image, Content, Part, Tool, FunctionDeclaration, GenerationConfig, HarmCategory, HarmBlockThreshold