from enum import Enumfrom typing import Unionfrom pydantic import BaseModelimport openaifrom openai import OpenAI
class Table(str, Enum):
orders = "orders"
customers = "customers"
products = "products"class Column(str, Enum):
id = "id"
status = "status"
expected_delivery_date = "expected_delivery_date"
delivered_at = "delivered_at"
shipped_at = "shipped_at"
ordered_at = "ordered_at"
canceled_at = "canceled_at"class Operator(str, Enum):
eq = "="
gt = ">"
lt = "<"
le = "<="
ge = ">="
ne = "!="class OrderBy(str, Enum): asc = "asc"
desc = "desc"class DynamicValue(BaseModel):
column_name: str
class Condition(BaseModel): column: str
operator: Operator value: Union[str, int, DynamicValue]class Query(BaseModel):
table_name: Table
columns: list[Column]
conditions: list[Condition]
order_by: OrderBy
client = OpenAI()completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{ "role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
},
{ "role": "user",
"content": "look up all my orders in may of last year that were fulfilled but not delivered on time",
},
],
tools=[
openai.pydantic_function_tool(Query),
],)print(completion.choices[0].message.tool_calls[0].function.parsed_arguments)1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.
而且,本机结构化输出支持也可用于response_format 。
from pydantic import BaseModelfrom openai import OpenAI
class Step(BaseModel):
explanation: str
output: str
class MathResponse(BaseModel):
steps: list[Step]
final_answer: str
client = OpenAI()completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "solve 8x + 31 = 2"},
],
response_format=MathResponse,)message = completion.choices[0].messageif message.parsed: print(message.parsed.steps)
print(message.parsed.final_answer)else: print(message.refusal)1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.
{ "model": "gpt-4o-2024-08-06",
"messages": [
{ "role": "system",
"content": "You are a helpful assistant"
},
{ "role": "user",
"content": "9.11 and 9.9 -- which is bigger?"
} ],
"response_format": { "type": "json_schema",
"json_schema": { "name": "reasoning_schema",
"strict": true,
"schema": { "type": "object",
"properties": { "reasoning_steps": { "type": "array",
"items": { "type": "string"
},
"description": "The reasoning steps leading to the final conclusion."
},
"answer": { "type": "string",
"description": "The final answer, taking into account the reasoning steps."
}
},
"required": ["reasoning_steps", "answer"],
"additionalProperties": false
}
}
}
}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.
结构化输出:
{
"reasoning_steps": [
"First step is to compare the numbers 9.11 and 9.9.",
"Both numbers have the same whole number part, which is 9.",
"To compare the decimal parts, convert them to the same number of decimal places.",
"9.11 has two decimal places: it is 9.11.",
"9.9 has one decimal place: it can be rewritten as 9.90.",
"Now, compare 9.11 and 9.90 by looking at the decimal parts.",
"Compare 11 with 90.",
"90 is greater than 11, so 9.90 is greater than 9.11."
],
"answer": "9.9 is bigger than 9.11."
}1.2.3.4.5.6.7.8.9.10.11.12.13.
- 从非结构化数据中提取结构化数据
例如,指示模型从会议记录中提取待办事项、截止日期和作业等内容。
请求:
POST /v1/chat/completions
{ "model": "gpt-4o-2024-08-06",
"messages": [
{ "role": "system",
"content": "Extract action items, due dates, and owners from meeting notes."
},
{ "role": "user",
"content": "...meeting notes go here..."
} ],
"response_format": { "type": "json_schema",
"json_schema": { "name": "action_items",
"strict": true,
"schema": { "type": "object",
"properties": { "action_items": { "type": "array",
"items": { "type": "object",
"properties": { "description": { "type": "string",
"description": "Description of the action item."
},
"due_date": { "type": ["string", "null"],
"description": "Due date for the action item, can be null if not specified."
},
"owner": { "type": ["string", "null"],
"description": "Owner responsible for the action item, can be null if not specified."
}
},
"required": ["description", "due_date", "owner"],
"additionalProperties": false
},
"description": "List of action items from the meeting."
}
},
"required": ["action_items"],
"additionalProperties": false
}
}
}
}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.
结构化输出:
{ "action_items": [
{ "description": "Collaborate on optimizing the path planning algorithm",
"due_date": "2024-06-30",
"owner": "Jason Li"
},
{ "description": "Reach out to industry partners for additional datasets",
"due_date": "2024-06-25",
"owner": "Aisha Patel"
},
{ "description": "Explore alternative LIDAR sensor configurations and report findings",
"due_date": "2024-06-27",
"owner": "Kevin Nguyen"
},
{ "description": "Schedule extended stress tests for the integrated navigation system",
"due_date": "2024-06-28",
"owner": "Emily Chen"
},
{ "description": "Retest the system after bug fixes and update the team",
"due_date": "2024-07-01",
"owner": "David Park"
} ]}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.
还没有评论,来说两句吧...