Structured Output Example
This example demonstrates how to convert content into JSON format for easier program processing.
Task
Convert news content into a structured JSON format with key information extracted.
Prompt
The user will provide a news article. Please analyze the content and extract key information in JSON format. The output JSON should follow this format:
{
"entity": <news entity>,
"time": <news time in YYYY-mm-dd HH:MM:SS format, use null if not available>,
"summary": <news content summary>
}
Example
User Input
On August 31, a Falcon 9 rocket launched from Cape Canaveral, Florida at 3:43 AM Eastern Time, delivering 21 Starlink satellites into orbit. Shortly after, at 4:48 AM Eastern Time on the same day, another Falcon 9 rocket launched from Vandenberg Space Force Base in California, successfully delivering another 21 Starlink satellites into orbit. The 65-minute interval between the two launches set a record for the shortest launch interval for Falcon 9 rockets.
The Federal Aviation Administration stated on August 30 that despite ongoing investigations into SpaceX, they have authorized the resumption of Falcon 9 rocket launches. Currently, neither party has disclosed detailed information about the booster landing failure incident on August 28. Although launches have resumed, the Polaris Dawn mission, which was planned for five days of space activities, has been postponed. SpaceX is actively preparing for this mission and awaiting final approval from the FAA to proceed with the launch.
Assistant Output
{
"entity": "Falcon 9 rocket",
"time": "2023-08-31 03:43:00",
"summary": "On August 31, two Falcon 9 rockets successfully launched, delivering 42 Starlink satellites into orbit, setting a record for shortest launch interval. While the FAA has authorized launch resumption, the planned Polaris Dawn mission has been postponed pending final approval."
}
Code Implementation
from openai import OpenAI
client = OpenAI(
base_url="https://api.deepseek.com/",
api_key="<YOUR_API_KEY>"
)
completion = client.chat.completions.create(
model="deepseek-chat",
messages=[
{
"role": "system",
"content": "The user will provide a news article. Please analyze the content and extract key information in JSON format. The output JSON should follow this format:\n\n{\n \"entity\": <news entity>,\n \"time\": <news time in YYYY-mm-dd HH:MM:SS format, use null if not available>,\n \"summary\": <news content summary>\n}"
},
{
"role": "user",
"content": "On August 31, a Falcon 9 rocket launched from Cape Canaveral, Florida at 3:43 AM Eastern Time, delivering 21 Starlink satellites into orbit. Shortly after, at 4:48 AM Eastern Time on the same day, another Falcon 9 rocket launched from Vandenberg Space Force Base in California, successfully delivering another 21 Starlink satellites into orbit. The 65-minute interval between the two launches set a record for the shortest launch interval for Falcon 9 rockets.\n\nThe Federal Aviation Administration stated on August 30 that despite ongoing investigations into SpaceX, they have authorized the resumption of Falcon 9 rocket launches. Currently, neither party has disclosed detailed information about the booster landing failure incident on August 28. Although launches have resumed, the Polaris Dawn mission, which was planned for five days of space activities, has been postponed. SpaceX is actively preparing for this mission and awaiting final approval from the FAA to proceed with the launch."
}
]
)
print(completion.choices[0].message.content)