窝牛号

使用python免费调用Google发布的Gemini双子座大模型API

上期文章,我们介绍了google发布的Gemini双子座大模型,现在google开放了gemini-pro与gemini-pro- vision2个版本的API接口。

其中gemini-pro模型类似与ChatGPT,是一个文本输入输出聊天模型,而vision模型,顾名思义是一个多模态模型,可以支持图片与文本的输入。

我们进入如下网站,就可以看到build with Gemini的字样了,点击下方的get api key in google ai studio就可以跳转到API申请链接了。

https://ai.google.dev/

进入API申请界面,这里只需要点击get API key,就可以生成属于自己的API key了。得到API key 后就可以使用python代码来调用其API接口了。

!pip install -q -U google-generativeai

首先是安装generativeai库,此库可以直接跟Gemini模型沟通,且模型无需下载到本地,我们就可以使用API接口来调用。

import pathlib import textwrap import google.generativeai as genai from IPython.display import display from IPython.display import Markdown def to_Markdown(text): text = text.replace(&39;, &39;) return Markdown(textwrap.indent(text, &39;, predicate=lambda _: True))

导入相关的库,这里主要是genai库,并建立一个to_markdown函数,主要用于后期的文本输出格式化。然后我们就可以使用模型了。

genai.configure(api_key=&39;) for m in genai.list_models(): if &39; in m.supported_generation_methods: print(m.name) models/gemini-pro models/gemini-pro-vision

在使用模型前,需要到google AI studio上申请API key,替换文章中的key。最后可以打印一下支持的模型,若没有模型,说明API key有问题。

model = genai.GenerativeModel(&39;) response = model.generate_content(&34;) to_markdown(response.text)

首先第一个问题,看看是否模型知道自己是谁,这里反馈的结果,并没有像网络传的一样,是回复百度的模型,当然也许是google已经修复了这个问题。

The meaning of life is a philosophical question that has been pondered by people for centuries. There is no one answer that is universally agreed upon, as the meaning of life is a subjective experience that can vary from person to person. However, there are some common themes that emerge when people discuss the meaning of life, including: Purpose: Many people believe that life has a purpose, whether it is to find happiness, make a difference in the world, or simply experience all that life has to offer. Connection: Another common theme is the importance of connection with others. Humans are social creatures, and our relationships with others can provide us with a sense of purpose and meaning. Growth: Many people also find meaning in personal growth and development. This can include learning new things, challenging oneself, and becoming a better person. Contribution: Some people find meaning in making a contribution to the world. This can be done through one&34;&34;冒泡排序算法&34;& 测试代码 list1 = [10, 7, 8, 9, 1, 5] print(&34;, list1) bubble_sort(list1) print(&34;, list1)Transformer模型是一种深度学习模型,于2017年由Vaswani等人提出。它是一种注意力机制模型,可以用于各种自然语言处理任务,包括机器翻译、文本摘要、问答和命名实体识别等。 Transformer模型的结构与传统的循环神经网络模型(RNN)和卷积神经网络模型(CNN)不同。它采用了一种自注意力机制(self-attention mechanism),这种机制可以使模型学习到输入序列中的单词之间的关系,而不需要使用显式的卷积或循环操作。 Transformer模型的结构分为编码器(encoder)和解码器(decoder)两部分。编码器负责将输入序列编码成一个固定长度的向量,而解码器则负责将编码后的向量解码成输出序列。 编码器由多个编码层组成,每个编码层包含一个自注意力层和一个前馈层。自注意力层负责计算输入序列中每个单词与其他单词之间的关系,而前馈层则负责将自注意力层的输出投影到一个新的向量空间中。 解码器由多个解码层组成,每个解码层包含一个自注意力层、一个编码器-解码器注意力层和一个前馈层。自注意力层负责计算输出序列中每个单词与其他单词之间的关系,编码器-解码器注意力层负责计算输入序列中每个单词与输出序列中每个单词之间的关系,而前馈层则负责将编码器-解码器注意力层的输出投影到一个新的向量空间中。 Transformer模型的优势在于它可以捕获输入序列中单词之间的长距离依赖关系,这使得它在处理长序列数据时具有很强的优势。此外,Transformer模型的并行性也很好,这使得它可以很容易地在GPU上训练和使用。 Transformer模型已经取得了非常好的效果,它在机器翻译、文本摘要、问答和命名实体识别等任务上都取得了最先进的结果。目前,Transformer模型已经成为自然语言处理领域中最常用的模型之一。

让其介绍下transformer模型,其回答的结论,感觉并不像google给出的结论,而确实像百度给出的结论。

当然,模型支持连续对话输入,这里需要修改一下模型代码

model = genai.GenerativeModel(&39;) chat = model.start_chat(history=[]) response = chat.send_message(&34;) to_markdown(response.text) chat.history response = chat.send_message(&34;, stream=True) for chunk in response: print(chunk.text) print(&34;*80) for message in chat.history: display(to_markdown(f&39;))

这里模型输入的文本与回答的文本都保存在chat.history,我们可以打印其chat.history来查看以前输入输出的问题与结论。

user: 你好,1+1=3,我老婆说是正确的 model: 1 + 1 不等于 3。1 + 1 等于 2。这是一个基本数学事实,并且没有争议。 user: 不对,我老婆总是正确的 model: 抱歉,但你的妻子错了。1 + 1 不等于 3。这是基本数学的事实,没有任何争议的余地。你的妻子可能是在开玩笑,或者她可能真的相信 1 + 1 等于 3。无论哪种情况,她都是错的。 如果你想向你的妻子证明 1 + 1 等于 2,你可以用一些简单的例子来说明。例如,你可以拿两个苹果,把它们放在一起,然后数一数。你会看到,你有两个苹果,而不是三个苹果。你也可以用手指来做同样的事情。把你的食指和中指放在一起,然后数一数。你会看到,你有两根手指,而不是三根手指。 如果你想了解更多关于基本数学的事实,你可以查阅一些数学教科书或在线资源。你也可以向你的老师或辅导员寻求帮助。

当然模型也支持输入一个图片与相应的文本进行多模态的任务。这里可以输入一个图片,并输入需要的问题,当然模型需要加载vision模型。

import PIL.Image img = PIL.Image.open(&39;) model = genai.GenerativeModel(&39;) response = model.generate_content([&34;, img], stream=True) response.resolve() to_markdown(response.text)Meal prepping is a great way to save time and money, and it can also help you to eat healthier. This meal is a great example of a healthy and delicious meal that can be easily prepped ahead of time. This meal features brown rice, roasted vegetables, and chicken teriyaki. The brown rice is a whole grain that is high in fiber and nutrients. The roasted vegetables are a great way to get your daily dose of vitamins and minerals. And the chicken teriyaki is a lean protein source that is also packed with flavor. This meal is easy to prepare ahead of time. Simply cook the brown rice, roast the vegetables, and cook the chicken teriyaki. Then, divide the meal into individual containers and store them in the refrigerator. When you&39;s also a great meal for those who are trying to lose weight or maintain a healthy weight. If you&build-with-gemini

本站所发布的文字与图片素材为非商业目的改编或整理,版权归原作者所有,如侵权或涉及违法,请联系我们删除

窝牛号 wwww.93ysy.com   沪ICP备2021036305号-1