Gemini - Google AI Studio
前提条件
pip install -q google-generativeai- 获取 API 密钥 - https://aistudio.google.com/
Gemini-Pro
示例用法
from litellm import completion
import os
os.environ['GEMINI_API_KEY'] = ""
response = completion(
model="gemini/gemini-pro",
messages=[{"role": "user", "content": "写代码以从 LiteLLM 打招呼"}]
)
支持的 OpenAI 参数
- temperature
- top_p
- max_tokens
- stream
- tools
- tool_choice
- response_format
- n
- stop
传递 Gemini 特定参数
响应架构
LiteLLM 支持将 response_schema 作为参数发送给 Google AI Studio 上的 Gemini-1.5-Pro。
响应架构
- SDK
- 代理
from litellm import completion
import json
import os
os.environ['GEMINI_API_KEY'] = ""
messages = [
{
"role": "user",
"content": "列出 5 个流行的饼干食谱。"
}
]
response_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
},
},
"required": ["recipe_name"],
},
}
completion(
model="gemini/gemini-1.5-pro",
messages=messages,
response_format={"type": "json_object", "response_schema": response_schema} # 👈 关键更改
)
print(json.loads(completion.choices[0].message.content))
- 将模型添加到 config.yaml
model_list:
- model_name: gemini-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
$ litellm --config /path/to/config.yaml
- 发起请求!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"model": "gemini-pro",
"messages": [
{"role": "user", "content": "列出 5 个流行的饼干食谱。"}
],
"response_format": {"type": "json_object", "response_schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
},
},
"required": ["recipe_name"],
},
}}
}
'
验证架构
要验证 response_schema,请设置 enforce_validation: true。
- SDK
- 代理
from litellm import completion, JSONSchemaValidationError
try:
completion(
model="gemini/gemini-1.5-pro",
messages=messages,
response_format={
"type": "json_object",
"response_schema": response_schema,
"enforce_validation": true # 👈 关键更改
}
)
except JSONSchemaValidationError as e:
print("原始响应: {}".format(e.raw_response))
raise e
- 将模型添加到 config.yaml
model_list:
- model_name: gemini-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
$ litellm --config /path/to/config.yaml
- 发起请求!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"model": "gemini-pro",
"messages": [
{"role": "user", "content": "列出 5 个流行的饼干食谱。"}
],
"response_format": {"type": "json_object", "response_schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
},
},
"required": ["recipe_name"],
},
},
"enforce_validation": true
}
}
'
LiteLLM 将根据架构验证响应,如果响应不符合架构,将引发 JSONSchemaValidationError。
JSONSchemaValidationError 继承自 openai.APIError
使用 e.raw_response 访问原始响应
GenerationConfig 参数
要传递额外的 GenerationConfig 参数 - 例如 topK,只需将其作为键值对传递到请求体中,LiteLLM 将直接通过请求体传递。
查看 Gemini GenerationConfigParams
- SDK
- PROXY
from litellm import completion
import json
import os
os.environ['GEMINI_API_KEY'] = ""
messages = [
{
"role": "user",
"content": "列出5种受欢迎的饼干食谱。"
}
]
completion(
model="gemini/gemini-1.5-pro",
messages=messages,
topK=1 # 👈 关键变化
)
print(json.loads(completion.choices[0].message.content))
- 将模型添加到config.yaml
model_list:
- model_name: gemini-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
$ litellm --config /path/to/config.yaml
- 发起请求!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "gemini-pro",
"messages": [
{"role": "user", "content": "列出5个受欢迎的曲奇饼干食谱。"}
],
"topK": 1 # 👈 关键更改
}
'
验证模式
要验证response_schema,设置 enforce_validation: true。
- SDK
- PROXY
from litellm import completion, JSONSchemaValidationError
try:
completion(
model="gemini/gemini-1.5-pro",
messages=messages,
response_format={
"type": "json_object",
"response_schema": response_schema,
"enforce_validation": true # 👈 关键更改
}
)
except JSONSchemaValidationError as e:
print("原始响应: {}".format(e.raw_response))
raise e
- 将模型添加到config.yaml
model_list:
- model_name: gemini-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
$ litellm --config /path/to/config.yaml
- 发起请求!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "gemini-pro",
"messages": [
{"role": "user", "content": "列出5个受欢迎的曲奇饼干食谱。"}
],
"response_format": {"type": "json_object", "response_schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
},
},
"required": ["recipe_name"],
},
},
"enforce_validation": true
}
}
'
指定安全设置
在某些用例中,您可能需要调用模型并传递与默认值不同的安全设置。为此,只需将 safety_settings 参数传递给 completion 或 acompletion。例如:
response = completion(
model="gemini/gemini-pro",
messages=[{"role": "user", "content": "为LiteLLM写代码以打招呼"}],
safety_settings=[
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
]
)
工具调用
from litellm import completion
import os
# 设置环境变量
os.environ["GEMINI_API_KEY"] = ".."
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取给定位置的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和州,例如:San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [{"role": "user", "content": "今天波士顿的天气怎么样?"}]
response = completion(
model="gemini/gemini-1.5-flash",
messages=messages,
tools=tools,
)
# 添加任何断言,这里检查响应参数
print(response)
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
assert isinstance(
response.choices[0].message.tool_calls[0].function.arguments, str
)
JSON模式
- SDK
- PROXY
from litellm import completion
import json
import os
os.environ['GEMINI_API_KEY'] = ""
messages = [
{
"role": "user",
"content": "列出5个受欢迎的曲奇饼干食谱。"
}
]
completion(
model="gemini/gemini-1.5-pro",
messages=messages,
response_format={"type": "json_object"} # 👈 关键更改
)
print(json.loads(completion.choices[0].message.content))
- 将模型添加到config.yaml
model_list:
- model_name: gemini-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
$ litellm --config /path/to/config.yaml
- 发起请求!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "gemini-pro",
"messages": [
{"role": "user", "content": "列出5种流行的饼干食谱。"}
],
"response_format": {"type": "json_object"}
}
'
示例用法
import os
import litellm
from dotenv import load_dotenv
# 从 .env 文件加载环境变量
load_dotenv()
os.environ["GEMINI_API_KEY"] = os.getenv('GEMINI_API_KEY')
prompt = '用几句话描述这张图片。'
# 注意:你可以直接传递图像的URL或路径。
image_url = 'https://storage.googleapis.com/github-repo/img/gemini/intro/landmark3.jpg'
# 根据文档创建消息负载
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {"url": image_url}
}
]
}
]
# 向Gemini模型发起API调用
response = litellm.completion(
model="gemini/gemini-pro-vision",
messages=messages,
)
# 提取响应内容
content = response.get('choices', [{}])[0].get('message', {}).get('content')
# 打印结果
print(content)
上下文缓存
使用Google AI Studio上下文缓存,支持在消息内容块中添加以下内容:
{
...,
"cache_control": {"type": "ephemeral"}
}
Gemini上下文缓存仅允许缓存1个连续的消息块。
向Gemini发起的原始请求如下所示:
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "请总结这段文字"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
- SDK
- PROXY
from litellm import completion
for _ in range(2):
resp = completion(
model="gemini/gemini-1.5-pro",
messages=[
# 系统消息
{
"role": "system",
"content": [
{
"type": "text",
"text": "这里是复杂法律协议的全文" * 4000,
"cache_control": {"type": "ephemeral"}, # 👈 关键变化
}
],
},
# 使用cache_control参数标记为缓存,以便此检查点可以读取之前的缓存。
{
"role": "user",
"content": [
{
"type": "text",
"text": "这个协议中的关键条款和条件是什么?",
"cache_control": {"type": "ephemeral"},
}
],
}]
)
print(resp.usage) # 👈 第二个使用块将减少,因为使用了缓存令牌
- 设置config.yaml
model_list:
- model_name: gemini-1.5-pro
litellm_params:
model: gemini/gemini-1.5-pro
api_key: os.environ/GEMINI_API_KEY
- 启动代理
litellm --config /path/to/config.yaml
- 测试它!
查看Langchain, OpenAI JS, Llamaindex等示例
- Curl
- OpenAI Python SDK
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "gemini-1.5-pro",
"messages": [
# 系统消息
{
"role": "system",
"content": [
{
"type": "text",
"text": "这里是复杂法律协议的全文" * 4000,
"cache_control": {"type": "ephemeral"}, # 👈 关键变化
}
],
},
# 使用cache_control参数标记为缓存,以便此检查点可以读取之前的缓存。
{
"role": "user",
"content": [
{
"type": "text",
"text": "这个协议中的关键条款和条件是什么?",
"cache_control": {"type": "ephemeral"},
}
],
}],
}'
import openai
client = openai.AsyncOpenAI(
api_key="anything", # litellm代理API密钥
base_url="http://0.0.0.0:4000" # litellm代理基础URL
)
response = await client.chat.completions.create(
model="gemini-1.5-pro",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": "Here is the full text of a complex legal agreement" * 4000,
"cache_control": {"type": "ephemeral"}, # 👈 关键变化
}
],
},
{
"role": "user",
"content": "what are the key terms and conditions in this agreement?",
},
]
)
使用 - PDF / 视频 / 等文件
内联数据(例如音频流)
LiteLLM遵循OpenAI的格式,接受发送内联数据作为base64编码的字符串。
遵循的格式为
data:<mime_type>;base64,<encoded_data>
LITELLM调用
import litellm
from pathlib import Path
import base64
import os
os.environ["GEMINI_API_KEY"] = ""
litellm.set_verbose = True # 👈 查看原始调用
audio_bytes = Path("speech_vertex.mp3").read_bytes()
encoded_data = base64.b64encode(audio_bytes).decode("utf-8")
print("Audio Bytes = {}".format(audio_bytes))
model = "gemini/gemini-1.5-flash"
response = litellm.completion(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please summarize the audio."},
{
"type": "image_url",
"image_url": "data:audio/mp3;base64,{}".format(encoded_data), # 👈 设置MIME类型+数据
},
],
}
],
)
等效的GOOGLE API调用
# 初始化适合您用例的Gemini模型。
model = genai.GenerativeModel('models/gemini-1.5-flash')
# 创建提示。
prompt = "Please summarize the audio."
# 将samplesmall.mp3文件加载到包含音频文件字节的Python Blob对象中,然后将提示和音频传递给Gemini。
response = model.generate_content([
prompt,
{
"mime_type": "audio/mp3",
"data": pathlib.Path('samplesmall.mp3').read_bytes()
}
])
# 输出Gemini对提示和内联音频的响应。
print(response.text)
https:// 文件
import litellm
import os
os.environ["GEMINI_API_KEY"] = ""
litellm.set_verbose = True # 👈 查看原始调用
model = "gemini/gemini-1.5-flash"
response = litellm.completion(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please summarize the file."},
{
"type": "image_url",
"image_url": "https://storage..." # 👈 设置图片URL
},
],
}
],
)
gs:// 文件
import litellm
import os
os.environ["GEMINI_API_KEY"] = ""
litellm.set_verbose = True # 👈 查看原始调用
model = "gemini/gemini-1.5-flash"
response = litellm.completion(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please summarize the file."},
{
"type": "image_url",
"image_url": "gs://..." # 👈 设置云存储桶URL
},
],
}
],
)
聊天模型
我们支持所有Gemini模型,只需在发送litellm请求时将model=gemini/<任何Gemini上的模型>作为前缀
| 模型名称 | 函数调用 | 必需的操作系统变量 |
|---|---|---|
| gemini-pro | completion(model='gemini/gemini-pro', messages) | os.environ['GEMINI_API_KEY'] |
| gemini-1.5-pro-latest | completion(model='gemini/gemini-1.5-pro-latest', messages) | os.environ['GEMINI_API_KEY'] |
| gemini-pro-vision | completion(model='gemini/gemini-pro-vision', messages) | os.environ['GEMINI_API_KEY'] |