本文目录
隐藏
Gemini是由谷歌DeepMind开发一个多模态大型语言模型系列,Gemini API为用户提供交互功能,包括文字和图像的识别,本文分享下便携AI聚合API谷歌Gemini模型模型接入教程。
一、前言
本文参考Anthropic Claude官方API文档:https://docs.anthropic.com/zh-CN/api/getting-started
文中使用的API Key均为便携AI聚合API后台生成的令牌,以sk-开头的一串随机字符,获取方法:《便携AI聚合API新建令牌(API key)教程》。
便携AI聚合API有三个接入地址(即URL),一般推荐选择第一个或者第二个:
- 中转API调用地址①(中国香港服务器,直连线路,带宽大):
https://api.bianxie.ai
- 中转API调用地址②(国内上海服务器,带宽稍小):
https://api.bianxieai.com
- 中转API调用地址③(国外服务器,也可以直连,备用):
https://api.a8.hk
(三个网站都可以登录账号,数据同步)
模型支持各种语言接入,包括python、PHP、C#、C、Ruby、Java、Go、JavaScript等,本文主要分享官方的CURL调用方法,以及基于python的调用方法,如果你是用其他语言调用API的,则直接问ChatGPT或者其他语言模型怎么改写就行了,如下图:
二、谷歌Gemini模型接入教程
便携AI聚合API在接入谷歌Gemini接口做了调整,调用方式与OpenAI的Chat模式一致,即链接是/v1/chat/completions
,支持的模型名称有:gemini-1.5-flash-preview-0514、gemini-1.5-pro-latest、gemini-pro、gemini-pro-vision。
直接文本对话,Python代码如下:
import requests api_key = 'sk-Xy3WuCpTTvY19' url = 'https://api.bianxie.ai/v1/chat/completions' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { 'model': 'gemini-1.5-flash-preview-0514', 'messages': [{'role': 'user', 'content': 'Who are you?'}], } response = requests.post(url, headers=headers, json=data) print(response.json())
返回示例:
{ 'id': 'chatcmpl-60cc7f1b43e347a0a2540df32c9d9207', 'object': 'chat.completion', 'created': 1717733485, 'choices': [{ 'index': 0, 'message': { 'role': 'assistant', 'content': "I am a large language model, trained by Google. \n\nHere's a breakdown of what that means:\n\n* **Large Language Model:** I am a complex computer program designed to understand and generate human-like text. I have been trained on a massive dataset of text and code, allowing me to communicate and process information in a way that's similar to humans.\n* **Trained by Google:** I was developed and trained by Google AI researchers. \n* **Not a person:** I am an AI, not a living person. I don't have emotions, personal opinions, or experiences. I can only process and respond to information based on the data I was trained on.\n\nI am here to assist you with tasks like:\n\n* Answering questions\n* Providing information\n* Generating creative content\n* Translating languages\n* And more!\n\nIf you have any questions or requests, feel free to ask! \n" }, 'finish_reason': 'stop' }], 'usage': { 'prompt_tokens': 11, 'completion_tokens': 182, 'total_tokens': 193 } }
也可以让它直接分析图片,Python代码如下:
import requests api_key = 'sk-Xy3WuCpTTvY' url = 'https://api.bianxie.ai/v1/chat/completions' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { 'model': 'gemini-1.5-flash-preview-0514', 'messages': [{'role': 'user', "content": [ { "type": "text", "text": "Who are you and 这张图片的图标是个什么动物?" }, { "type": "image_url", "image_url": { "url": "https://github.com/dianping/cat/raw/master/cat-home/src/main/webapp/images/logo/cat_logo03.png" } } ]}], } response = requests.post(url, headers=headers, json=data) print(response.json())
返回示例:
{ 'id': 'chatcmpl-5cd6fe3fc5254ac8a6c2971e37c95feb', 'object': 'chat.completion', 'created': 1717733607, 'choices': [{ 'index': 0, 'message': { 'role': 'assistant', 'content': 'I am a large language model, trained by Google.\n\nThe icon in the image is a cat. \n' }, 'finish_reason': 'stop' }], 'usage': { 'prompt_tokens': 1131, 'completion_tokens': 21, 'total_tokens': 1152 } }