暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

青云大模型服务支持 JSON 模式输出功能

青云QingCloud 2025-04-17
95
作为一种轻量级的数据交换格式,JSON 具有简洁、清晰、易于解析和生成的特点。JSON 模式是大模型 API 实现结构化输出的关键功能。当用户在调用大模型 API 时,返回的结果以 JSON 格式呈现,有利于阅读和理解。
青云科技旗下 AI 算力云服务——基石智算CoresHub 大模型服务支持 JSON 模式输出功能,帮助用户在与大模型交互时,获得更加结构化、标准化的数据结果。无论是复杂的文本分析、数据处理,还是智能应用开发,基石智算都能为用户带来更高效、便捷和灵活的数据处理方式。

支持的模型

目前平台上的 DeepSeek-V3 模型支持该功能。

注意事项

用户需提前在 http://docs.coreshub.cn/console/big_model_server/api_key/create_api_key/ 中创建与获取 API KEY 用于替换示例代码中的 api_key 参数。

使用方法

● 将 response_format 参数设置为 {'type': 'json_object'}。

● 用户传入的 system 或 user prompt 中必须含有 json 字样,并给出希望模型输出的 JSON 格式的样例。

● 合理设置 max_tokens 参数,防止 JSON 字符串被中断。

代码示例

示例一
    import json
    from openai import OpenAI
    client = OpenAI(
        api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        base_url="https://openapi.coreshub.cn/v1"
    )
    user_prompt = """Which is the longest river in the world? The Nile River.
    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"
    }
    Don't output begin "```json" and end "```"
    """
    messages = [{"role""user""content": user_prompt}]
    response = client.chat.completions.create(
        model="DeepSeek-V3",
        messages=messages,
        response_format={
            'type''json_object'
        }
    )
    print(json.loads(response.choices[0].message.content))
    代码运行回显示例:
      {'question''Which is the longest river in the world?''answer''The Nile River'}
      示例二
        import json
        from openai import OpenAI
        client = OpenAI(
            api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            base_url="https://openapi.coreshub.cn/v1"
        )
        response = client.chat.completions.create(
            model="DeepSeek-V3",
            messages=[
                    {"role""system""content""You are a helpful assistant designed to output JSON."},
                    {"role""user""content""""? 2020 年世界奥运会乒乓球男子和女子单打冠军分别是谁?
                     Please respond in the format {\"男子冠军\": ..., \"女子冠军\": ...}
                     Don't output begin "```json" and end "```" """}
                ],
            response_format={
                'type''json_object'
            }
        )
        print(json.loads(response.choices[0].message.content))

        代码运行回显示例:

          {'男子冠军''马龙''女子冠军''陈梦'}



          - FIN -

          更多推荐





          点击“阅读原文”了解更多










          文章转载自青云QingCloud,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论