结构化输 出
我们部分模型拥有兼容 openai结构化输出 的能力, 因此支持使用 openai sdk来使用此能力
目前支持的模型包括:
- inf-chat-int-v1
JSON 示例
输入 python 类,输出 python 对象
import os
import json
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI(
base_url="https://api.infly.cn/v1",
api_key=os.getenv("INF_OPENAPI_API_KEY"),
)
class Weather(BaseModel):
location: str
weather_state: str
temperature: int
completion = client.beta.chat.completions.parse(
model="inf-chat-int-v1",
messages=[
{"role": "system", "content": "抽取以下文本中的天气"},
{"role": "user", "content": "上海市徐汇滨江今天晴天,气温25度"},
],
response_format=Weather,
)
weather = completion.choices[0].message.parsed
print(weather)
# 将会打印
# Weather(location='上海市徐汇滨江', weather_state='晴天', temperature=25)
输入 json schema, 输出 json
JSON Schema 是用于定义和验证 JSON 数据结构的强大标准。
import os
import json
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI(
base_url="https://api.infly.cn/v1",
api_key=os.getenv("INF_OPENAPI_API_KEY"),
)
weather_json_schema = {
"type": "object",
"properties": {
"location": {"type": "string"},
"weather_state": {"type": "string"},
"temperature": {"type": "integer"},
},
"required": ["location", "weather_state", "temperature"],
"additionalProperties": False,
}
# or
class Weather(BaseModel):
location: str
weather_state: str
temperature: int
weather_json_schema = Weather.model_json_schema()
completion = client.chat.completions.create(
model="inf-chat-int-v1",
messages=[
{"role": "system", "content": "抽取以下文本中的天气"},
{"role": "user", "content": "上海市徐汇滨江今天晴天,气温25度"},
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "Whether",
"schema": weather_json_schema,
}
}
)
print(completion.choices[0].message.content)
# 将会打印:
# {"location": "上海市徐汇滨江", "weather_state": "晴天", "temperature": 25}
stream 模式
import os
import json
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI(
base_url="https://api.infly.cn/v1",
api_key=os.getenv("INF_OPENAPI_API_KEY"),
)
class Weather(BaseModel):
location: str
weather_state: str
temperature: int
completion = client.chat.completions.create(
model="inf-chat-int-v1",
stream=True,
messages=[
{"role": "system", "content": "抽取以下文 本中的天气"},
{"role": "user", "content": "上海市徐汇滨江今天晴天,气温25度"},
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "Weather",
"schema": Weather.model_json_schema()
}
}
)
final_json = ""
for chunk in completion:
if chunk.choices:
final_json += chunk.choices[0].delta.content or ""
print(final_json)
# 将会打印:
# {"location": "上海市徐汇滨江", "weather_state": "晴天", "temperature": 25}