Skip to main content

结构化输出 (JSON 模式)

快速开始

from litellm import completion
import os

os.environ["OPENAI_API_KEY"] = ""

response = completion(
model="gpt-4o-mini",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
)
print(response.choices[0].message.content)
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_KEY" \
-d '{
"model": "gpt-4o-mini",
"response_format": { "type": "json_object" },
"messages": [
{
"role": "system",
"content": "You are a helpful assistant designed to output JSON."
},
{
"role": "user",
"content": "Who won the world series in 2020?"
}
]
}'

检查模型支持

调用 litellm.get_supported_openai_params 来检查某个模型/提供者是否支持 response_format

from litellm import get_supported_openai_params

params = get_supported_openai_params(model="anthropic.claude-3", custom_llm_provider="bedrock")

assert "response_format" in params

传递 'json_schema'

要使用结构化输出,只需指定

response_format: { "type": "json_schema", "json_schema": … , "strict": true }

适用于:

  • OpenAI 模型
  • Azure OpenAI 模型
  • Google AI Studio - Gemini 模型
  • Vertex AI 模型 (Gemini + Anthropic)
  • Bedrock 模型
import os
from litellm import completion
from pydantic import BaseModel

# 添加到环境变量
os.environ["OPENAI_API_KEY"] = ""

messages = [{"role": "user", "content": "List 5 important events in the XIX century"}]

class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]

class EventsList(BaseModel):
events: list[CalendarEvent]

resp = completion(
model="gpt-4o-2024-08-06",
messages=messages,
response_format=EventsList
)

print("Received={}".format(resp))
  1. 将 openai 模型添加到 config.yaml
model_list:
- model_name: "gpt-4o"
litellm_params:
model: "gpt-4o-2024-08-06"
  1. 使用 config.yaml 启动代理
litellm --config /path/to/config.yaml
  1. 使用 OpenAI SDK / Curl 调用!

只需替换 openai sdk 中的 'base_url',即可使用 'json_schema' 调用 openai 模型的代理

OpenAI SDK

from pydantic import BaseModel
from openai import OpenAI

client = OpenAI(
api_key="anything", # 👈 PROXY KEY (可以是任何值,如果未设置 master_key)
base_url="http://0.0.0.0:4000" # 👈 PROXY BASE URL
)

class Step(BaseModel):
explanation: str
output: str

class MathReasoning(BaseModel):
steps: list[Step]
final_answer: str

completion = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
{"role": "user", "content": "how can I solve 8x + 7 = -23"}
],
response_format=MathReasoning,
)

math_reasoning = completion.choices[0].message.parsed

Curl

curl -X POST 'http://0.0.0.0:4000/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "math_reasoning",
"schema": {
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
},
"strict": true
}
}
}'

验证 JSON Schema

并非所有 vertex 模型都支持将 json_schema 传递给他们(例如 gemini-1.5-flash)。为了解决这个问题,LiteLLM 支持客户端对 json schema 进行验证。

litellm.enable_json_schema_validation=True

如果设置了 litellm.enable_json_schema_validation=True,LiteLLM 将使用 jsonvalidator 验证 JSON 响应。

查看代码

# !gcloud auth application-default login - 运行此命令以将 vertex 凭据添加到您的环境
import litellm, os
from litellm import completion
from pydantic import BaseModel


messages=[
{"role": "system", "content": "提取事件信息。"},
{"role": "user", "content": "Alice 和 Bob 将在周五去科学展览会。"},
]

litellm.enable_json_schema_validation = True
litellm.set_verbose = True # 查看 litellm 发出的原始请求

class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]

resp = completion(
model="gemini/gemini-1.5-pro",
messages=messages,
response_format=CalendarEvent,
)

print("Received={}".format(resp))
  1. 创建 config.yaml
model_list:
- model_name: "gemini-1.5-flash"
litellm_params:
model: "gemini/gemini-1.5-flash"
api_key: os.environ/GEMINI_API_KEY

litellm_settings:
enable_json_schema_validation: True
  1. 启动代理
litellm --config /path/to/config.yaml
  1. 测试它!
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_API_KEY" \
-d '{
"model": "gemini-1.5-flash",
"messages": [
{"role": "system", "content": "提取事件信息。"},
{"role": "user", "content": "Alice 和 Bob 将在周五去科学展览会。"},
],
"response_format": {
"type": "json_object",
"response_schema": {
"type": "json_schema",
"json_schema": {
"name": "math_reasoning",
"schema": {
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
},
"strict": true
},
}
},
}'
优云智算