
如何快速实现REST API集成以优化业务流程
12月14日,谷歌在官网宣布,免费开放GeminiPro以及他的API的支持,并且支持中文。
开发者可以在开发平台Google AI Studio中免费访问这两个API,每分钟可以发出60个请求。
API地址:https:makersuite.google.com/app/apikey
Bard免费体验地址:https://bard.google.com
Gemini 是 Google AI 开发的大型语言模型的名称。这一成就标志着人工智能领域的里程碑,是向前迈出的一大步。
Ai Gemini Google 最大、能力最强的人工智能,可以处理图像、视频、文本、音频和代码。它自信地宣称自己有能力在在线交互领域超越 Chat GPT 和微软的 Bing Chat 等领先的人工智能聊天机器人。
GEMINI 是多式联运。
让 Gemini 真正脱颖而出的是多模态
。Gemini 处理不同类型任务的方式正是您对人工智能系统的期望。这些功能以前在计算机中并不存在。
与其他只能理解文本的人工智能模型不同,Gemini 可以理解并响应来自不同来源的信息,如图片、声音、甚至代码,并为您提供最佳的响应。
Gemini Pro 模型的API请求代码
以python为例
同时请注意,您需要首先通过访问以下链接获取 API,该 API 仍然免费
https://ai.google.dev/pricing
此外,如果您无法获取 API 或运行代码,那么使用 VPN 可能会很有用。
首先您需要安装以下内容
!pip install google.generativeai
import google.generativeai as genaiimport os
genai.configure(api_key=os.getenv('GEMINI_API_KEY') or "YOUR KEY HERE")
此行配置 API 密钥。
for m in genai.list_models(): if 'generateContent' in m.supported_generation_methods: print(m.name)
该代码迭代“genai”模块中可用的模型。它检查每个模型是否支持名为“generateContent”的方法。
model = genai.GenerativeModel('gemini-pro')
此行使用“genai”模块中的“gemini-pro”模型初始化生成模型。
response = model.generate_content("What is the future of AI in one sentence?")print(response.text)print(response.prompt_feedback)print(response.candidates)
本节向“gemini-pro”模型发送一条提示,用一句话询问人工智能的未来。
响应预计包含生成的内容response.text、有关提示的反馈response.prompt_feedback以及候选响应列表response.candidates。
response = model.generate_content(“What is the future of AI in one sentence?”, stream=True)for chunk in response: print(chunk.text) print(“_”*80)
这部分与之前的内容生成类似,但使用流式传输方法(stream=True
)。这对于处理更大的响应或实时数据流可能很有用。
它迭代响应的块,打印每个块,后跟一行下划线以进行分隔。
import google.generativeai as genaiimport os
genai.configure(api_key=os.getenv('GEMINI_API_KEY') or "YOUR KEY HERE")
# will list available modelsfor m in genai.list_models(): if 'generateContent' in m.supported_generation_methods: print(m.name)
model = genai.GenerativeModel('gemini-pro')
response =model.generate_content("What is the future of AI in one sentence?")
print(response.text)print(response.prompt_feedback)print(response.candidates)
response = model.generate_content("What is the future of AI in one sentence?", stream=True)
for chunk in response: print(chunk.text) print("_"*80)
import google.generativeai as genaiimport os
该代码导入“google.generativeai”库和“os”模块以进行操作系统交互。
genai.configure(api_key=os.getenv('GEMINI_API_KEY') or "YOUR KEY HERE")
genai.configure函数设置对生成 AI 服务的请求进行身份验证所需的 API 密钥。
它尝试从环境变量GEMINI_API_KEY检索 API 密钥。如果未找到,它将使用占位符YOUR KEY HERE。
model = genai.GenerativeModel('gemini-pro')
此行从“genai”库初始化一个名为“gemini-pro”的生成模型。
chat = model.start_chat(history=[])
在这里,聊天会话从生成模型开始。history=[]
参数表示聊天开始时没有之前的对话历史记录。
while True: user_input = input("You: ") response = chat.send_message(user_input, stream=True) for chunk in response: print(chunk.text)
该循环迭代流响应的每个块并将其打印出来,提供与人工智能的实时交互。
此代码是基于文本的交互式 AI 聊天机器人的基本实现,展示了如何配置、初始化 Gemini-pro 模型并与之交互。
import google.generativeai as genaiimport os
genai.configure(api_key=os.getenv('GEMINI_API_KEY') or "YOUR KEY HERE")
model = genai.GenerativeModel('gemini-pro')chat = model.start_chat(history=[])
while True: user_input = input("You: ") response = chat.send_message(user_input, stream=True) for chunk in response: print(chunk.text)
使用所提供的图像,您可以期望模型根据图像中的元素生成一篇简短的博客文章。
import PIL.Imageimport google.generativeai as genaiimport os
genai.configure(api_key=os.getenv('GEMINI_API_KEY') or "YOUR KEY HERE")img = PIL.Image.open('your_image.png')model = genai.GenerativeModel('gemini-pro-vision')response = model.generate_content(img)response = model.generate_content(["Write a short, engaging blog artile based on this picture.", img], stream=False)response.resolve()print(response.text)
因此,在这个简短的实践中,我们使用了 Gemini -pro,同时使用此 API 测试更多应用程序会很有趣,因为在撰写本文时它是免费的。
文章转自微信公众号@HELLO程序员