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

智能也得细细瞧 2023-12-22 09:00:00

上期文章,我们介绍了,现在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 pathlibimport textwrapimport google.generativeai as genaifrom IPython.display import displayfrom IPython.display import Markdowndef to_markdown(text): text = text.replace('•', ' *') return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

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

genai.configure(api_key='AIzaSyDsa0Vzw14g')for m in genai.list_models(): if 'generateContent' in m.supported_generation_methods: print(m.name)models/gemini-promodels/gemini-pro-vision

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

model = genai.GenerativeModel('gemini-pro')response = model.generate_content("你是谁")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's work, volunteerism, or simply by being a kind and compassionate person.Legacy: Some people believe that the meaning of life is to leave a legacy that will continue to impact the world after they are gone. This legacy can be anything from a work of art to a family to a positive impact on society.Ultimately, the meaning of life is a personal question that each individual must answer for themselves. There is no right or wrong answer, and what is meaningful to one person may not be meaningful to another. However, by exploring the common themes that emerge when people discuss the meaning of life, we can begin to understand what it means to live a meaningful life.Here are some additional thoughts on the meaning of life:The meaning of life may change over time. As we grow and change, our priorities and values may also change. What was meaningful to us in our youth may not be meaningful to us in our old age.There is no one right way to live a meaningful life. What is meaningful to one person may not be meaningful to another. The key is to find what works for you and to live your life in a way that is true to yourself.The meaning of life may be different for different cultures. What is considered to be a meaningful life in one culture may not be considered to be meaningful in another culture. This is because different cultures have different values and beliefs.The meaning of life may be a mystery. Some people believe that the meaning of life is something that cannot be fully understood. They believe that the meaning of life is something that we can only experience, not explain.

模型不仅支持英文输入,当然也可以支持中文输入,同样的一个问题,我们可以直接输入中文

自我实现: 在生活中寻找和实现自己的独特潜力和目标。建立有意义的人际关系: 与他人建立紧密的联系和支持网络。对社会做出贡献: 通过自己的技能、才能或知识为社会做出积极的贡献。学习和成长: 不断学习和扩大自己的知识和技能,促进个人的成长和发展。体验生活: 享受生命中简单的快乐,并欣赏周围的美好事物。找到自己的精神归属: 信仰或内在精神需求的实现。克服挑战: 面对困难和挑战时,能以积极的心态应对并从中学习和成长。留下一份遗产: 在生活中创造出某种持久影响或成就,使自己的存在变得更有意义。活在当下: 珍惜每一天,充分利用时间,并对生活保持积极和感恩的态度。追求幸福: 努力寻找和创造能让自己感到快乐和满足的生活。

测试下编程能力,让他用python写一个冒泡排序算法,当然模型可以给算法的代码,也提供了实例,但是模型没有运行此代码,并没有给出排序后的结果,而ChatGPT模型是可以给出排序后的数据的。

def bubble_sort(list1): """冒泡排序算法""" n = len(list1) for i in range(n): for j in range(0, n - i - 1): if list1[j] > list1[j + 1]: list1[j], list1[j + 1] = list1[j + 1], list1[j] return list1# 测试代码list1 = [10, 7, 8, 9, 1, 5]print("排序前:", list1)bubble_sort(list1)print("排序后:", list1)

Transformer模型是一种深度学习模型,于2017年由Vaswani等人提出。它是一种注意力机制模型,可以用于各种自然语言处理任务,包括机器翻译、文本摘要、问答和命名实体识别等。Transformer模型的结构与传统的循环神经网络模型(RNN)和卷积神经网络模型(CNN)不同。它采用了一种自注意力机制(self-attention mechanism),这种机制可以使模型学习到输入序列中的单词之间的关系,而不需要使用显式的卷积或循环操作。Transformer模型的结构分为编码器(encoder)和解码器(decoder)两部分。编码器负责将输入序列编码成一个固定长度的向量,而解码器则负责将编码后的向量解码成输出序列。编码器由多个编码层组成,每个编码层包含一个自注意力层和一个前馈层。自注意力层负责计算输入序列中每个单词与其他单词之间的关系,而前馈层则负责将自注意力层的输出投影到一个新的向量空间中。解码器由多个解码层组成,每个解码层包含一个自注意力层、一个编码器-解码器注意力层和一个前馈层。自注意力层负责计算输出序列中每个单词与其他单词之间的关系,编码器-解码器注意力层负责计算输入序列中每个单词与输出序列中每个单词之间的关系,而前馈层则负责将编码器-解码器注意力层的输出投影到一个新的向量空间中。Transformer模型的优势在于它可以捕获输入序列中单词之间的长距离依赖关系,这使得它在处理长序列数据时具有很强的优势。此外,Transformer模型的并行性也很好,这使得它可以很容易地在GPU上训练和使用。Transformer模型已经取得了非常好的效果,它在机器翻译、文本摘要、问答和命名实体识别等任务上都取得了最先进的结果。目前,Transformer模型已经成为自然语言处理领域中最常用的模型之一。

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

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

model = genai.GenerativeModel('gemini-pro')chat = model.start_chat(history=[])response = chat.send_message("你好,1+1=3,我老婆说是正确的")to_markdown(response.text)chat.historyresponse = chat.send_message("不对,我老婆总是正确的", stream=True)for chunk in response: print(chunk.text) print("_"*80)for message in chat.history: display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))

这里模型输入的文本与回答的文本都保存在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.Imageimg = PIL.Image.open('image.jpg')model = genai.GenerativeModel('gemini-pro-vision')response = model.generate_content(["Write a short, engaging blog post based on this picture. It should include a description of the meal in the photo and talk about my journey meal prepping.", 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're ready to eat, simply grab a container and heat it up.This meal is a great option for busy people who are looking for a healthy and delicious way to eat. It's also a great meal for those who are trying to lose weight or maintain a healthy weight.If you're looking for a healthy and delicious meal that can be easily prepped ahead of time, this meal is a great option. Give it a try today!

https://ai.google.dev/https://ai.google.dev/tutorials/python_quickstarthttps://blog.google/technology/ai/gemini-api-developers-cloud/https://makersuite.google.com/app/prompts/new_freeformhttps://deepmind.google/technologies/gemini/#build-with-gemini
4 阅读:582

智能也得细细瞧

简介:感谢大家的关注