DeepSeek类型:人工智能
简介:一款基于深度学习和自然语言处理技术的产品,人气赶超ChatGPT。
在许多应用场景中,用户可能需要让模型严格按照JSON格式输出内容,以实现输出的结构化,从而便于后续的逻辑处理和解析。DeepSeek提供了JSON输出功能,以确保模型输出符合合法的JSON字符串格式。
一、DeepSeek JSON输出功能使用说明
1、设置response_format参数:需要将response_format参数设置为{‘type’:’json_object’},以指定输出为JSON格式。
2、确保prompt包含JSON相关指示:在用户输入的system或userprompt中,必须包含JSON字符,并给出期望的JSON输出格式示例,以指导模型返回合法的JSON数据。
3、设置max_tokens参数:为了避免JSON字符串在生成过程中被截断,建议合理设置max_tokens参数。
4、注意空响应问题:在使用JSON输出功能时,API可能会返回空的content。我们正在积极优化这一问题,您可以尝试调整prompt内容来减轻这一情况。
二、DeepSeek JSON输出功能示例代码
以下是使用DeepSeekJSON输出功能的完整Python示例代码:
from openai import OpenAI
api_key="<your api key>",
base_url="https://api.deepseek.com",
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
Which is the highest mountain in the world? Mount Everest.
"question": "Which is the highest mountain in the world?",
"answer": "Mount Everest"
user_prompt = "Which is the longest river in the world? The Nile River."
messages = [{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}]
response = client.chat.completions.create(
print(json.loads(response.choices[0].message.content))
import json
from openai import OpenAI
client = OpenAI(
api_key="<your api key>",
base_url="https://api.deepseek.com",
)
system_prompt = """
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
EXAMPLE INPUT:
Which is the highest mountain in the world? Mount Everest.
EXAMPLE JSON OUTPUT:
{
"question": "Which is the highest mountain in the world?",
"answer": "Mount Everest"
}
"""
user_prompt = "Which is the longest river in the world? The Nile River."
messages = [{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
response_format={
'type': 'json_object'
}
)
print(json.loads(response.choices[0].message.content))
import json
from openai import OpenAI
client = OpenAI(
api_key="<your api key>",
base_url="https://api.deepseek.com",
)
system_prompt = """
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
EXAMPLE INPUT:
Which is the highest mountain in the world? Mount Everest.
EXAMPLE JSON OUTPUT:
{
"question": "Which is the highest mountain in the world?",
"answer": "Mount Everest"
}
"""
user_prompt = "Which is the longest river in the world? The Nile River."
messages = [{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
response_format={
'type': 'json_object'
}
)
print(json.loads(response.choices[0].message.content))
三、DeepSeek JSON输出
根据以上代码,模型会返回如下格式的JSON输出:
"question": "Which is the longest river in the world?",
"answer": "The Nile River"
{
"question": "Which is the longest river in the world?",
"answer": "The Nile River"
}
{
"question": "Which is the longest river in the world?",
"answer": "The Nile River"
}