Gemini — Get Started with API

12 月 13 日,开拓者终于可以通过 API 将 Google 最新、最强大的措辞模型 Gemini 集成到他们的运用程序中。
目前 API 唯一可用的模型尺寸是 Gemini Pro。

好饭不怕晚,本日,我们分享的是:

如何创建 Google API 密钥。
如何利用 Google Generative AI SDK 调用 Gemini API。
如何利用 LangChain 框架集成 Gemini API。

初学者指南若何开始运用 Google Gemini API

创建 Google API 密钥

最大略、最直接的方法是在 Google AI Studio 上创建 API 密钥。

您该当访问 https://makersuite.google.com 并通过“获取 API 密钥”选项创建密钥.

Create Google API Keys on Google AI Studio

利用Google Generative AI SDK调用Gemini API

Google 的 天生式 AI Python SDK 供应对各种天生模型的访问,包括 Gemini API。

安装天生式 AI SDK:

$ pip install -q -U google-generativeai

文本天生(文本补全)示例:python

import google.generativeai as genaigenai.configure(api_key="xxxxx")model = genai.GenerativeModel(model_name="gemini-pro")prompt_parts = [ "写一个 Python 函数并向我阐明",]response = model.generate_content(prompt_parts)print(response.text)

输出结果如下:

```pythondef find_duplicates(list1): """ Finds duplicate elements in a list. Parameters: list1 (list): The list to search for duplicates in. Returns: list: A list of the duplicate elements. """ # Create a set of the elements in the list. set1 = set(list1) # Create a new list to store the duplicate elements. duplicates = [] # Iterate over the list and add any elements that are not in the set to the list of duplicates. for element in list1: if element not in set1: duplicates.append(element) # Return the list of duplicates. return duplicates```This function takes a list as input and returns a list of the duplicate elements in the list.The function works by first creating a set of the elements in the list. A set is a data structure that stores unique elements, so any duplicate elements in the list will not be included in the set.The function then creates a new list to store the duplicate elements.The function then iterates over the list and adds any elements that are not in the set to the list of duplicates.Finally, the function returns the list of duplicates.Here is an example of how the function can be used:```pythonlist1 = [1, 2, 3, 4, 5, 1, 2, 3]duplicates = find_duplicates(list1)print(duplicates)```Output:```[1, 2, 3]```

图像识别示例:

Google 供应单独的天生模型gemini-pro-vision来访问多模式模型。

我们利用Pillow读取图像:

$ pip install pillow

利用的图片来自unsplash作者Melody Zimmerman.

示例代码:

import PIL.Imageimg = PIL.Image.open('coffee-roll.jpg')model = genai.GenerativeModel('gemini-pro-vision')response = model.generate_content( [ "根据这张照片写一篇简短而有吸引力的博文。
文章应包括对照片中物品的描述,并讲述我的东京之旅。
", img ], stream=True)response.resolve()print(response.text)

利用供应的图片,模型根据图片中的元素天生一篇简短的博文。

I've always wanted to visit Tokyo, and I'm so glad I finally had the chance. I spent a week exploring the city, and I was amazed by everything I saw. The food was incredible, the people were friendly, and the culture was fascinating.One of my favorite things about Tokyo was the coffee. I'm a big coffee drinker, and I loved trying all the different types of coffee that Tokyo has to offer. I especially loved the matcha latte, which is a green tea latte. It was so delicious and refreshing.I also loved the pastries in Tokyo. The Japanese are known for their love of sweets, and there are many great bakeries in the city. I tried a variety of pastries, including croissants, pain au chocolat, and eclairs. They were all delicious, but my favorite was the custard bun. It was so soft and fluffy, and the custard filling was just the right amount of sweetness.I had such a great time in Tokyo, and I can't wait to go back someday. I would highly recommend it to anyone who is looking for a unique and exciting travel experience.我一贯想去东京,很高兴终于有机会了。
我花了一周的韶光探索这座城市,我对所看到的统统感到惊异。
食品令人难以置信,人们很友好,文化也很迷人。
东京我最喜好的事情之一便是咖啡。
我是一个咖啡爱好者,我喜好考试测验东京供应的所有不同类型的咖啡。
我特殊喜好抹茶拿铁,这是一种绿茶拿铁。
真是美味又清爽。
我也很喜好东京的糕点。
日本人以喜好甜食而有名,城里有许多很棒的面包店。
我考试测验了各种糕点,包括羊角面包、巧克力面包和闪电泡芙。
它们都很美味,但我最喜好的是奶油面包。
又软又蓬松,蛋奶馅的甜度也恰到好处。
我在东京度过了一段美好的光阴,我迫不及待地想有一天再回去。
我强烈推举它给任何正在探求独特且令人愉快的旅行体验的人。

将 Gemini API 与 LangChain 框架集成

LangChain 为 Google Generative AI 供应了包装类,方便开拓者集成 Gemini API。
这是一个示例代码片段:

from langchain_google_genai import ChatGoogleGenerativeAIllm = ChatGoogleGenerativeAI(model="gemini-pro")result = llm.invoke("What is the best practice to keep fit?")print(result.content)

在实行代码之前,请确保您已设置环境变量:

$ os.environ.setdefault("GOOGLE_API_KEY", "xxxx")

请参阅此 Python 条记本,获取完全的示例代码。

点赞关注 二师兄 talk 获取更多资讯,并在 头条 上阅读我的短篇技能文章