V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RaftingPoint
V2EX  ›  程序员

请问哪个大模型 api 能支持输出标准严谨的 json 格式

  •  
  •   RaftingPoint · 47 天前 · 2926 次点击
    这是一个创建于 47 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近有个任务需要从前端抓取一些数据转换为 json ,由于格式多变,所以打算让大模型来自动化转换成 json 。试了下文心一言的 api 发现无论如何都会有输出格式不严谨的情况,导致不能从字符串转为 json. 请问有哪个大模型版本在这方面表现是比较稳定的吗

    30 条回复    2024-07-23 11:01:10 +08:00
    NoobNoob030
        1
    NoobNoob030  
       47 天前
    格式比较固定的话,用 function call ,国内的 LLM 基本都支持
    matrix1010
        2
    matrix1010  
       47 天前
    Gemini 1.5 flash/pro 用 JSON mode ,根据我几千次的测试来看 100%返回 JSON, 大约 0.5%的情况下会出现返回数组而不是 object 的情况,不过这可能跟我的 prompt 有关。GPT 4o/4o mini 虽然我没试但应该也差不多,理论上应该更好。

    估计原生支持 JSON mode 的 LLM 都没问题,针对 JSON 进行过专门优化
    vacuitym
        3
    vacuitym  
       47 天前
    openai 的模型也支持 json 模式
    Oats
        4
    Oats  
       47 天前
    从测试来看,openai 的最严谨,如果要通用的话最好还是 function call
    maocat
        5
    maocat  
       47 天前 via iPhone
    你可能需要 Langchain
    RaftingPoint
        6
    RaftingPoint  
    OP
       47 天前
    @NoobNoob030 格式不太固定,每一条抓取的键值对可能有不同程度的嵌套关系,好像不太适合这种方式
    RaftingPoint
        7
    RaftingPoint  
    OP
       47 天前
    @maocat 谢谢,我也看到这个,打算试一下
    crackidz
        8
    crackidz  
       47 天前
    LLM 不可能完全绝对严格,有几种做法:
    1. 换一个更好的模型...
    2. 选择验证相对松散的验证,当然也仍旧会有概率有问题,但会比 JSON 低
    3. 提升 prompt 尝试保证更强的输出,当然这对底模有要求,底模也要单独优化过
    RaftingPoint
        9
    RaftingPoint  
    OP
       47 天前
    @matrix1010 谢谢!我看了下我的文本并不复杂,按理模型转成 json 难度应该不大,prompt 的关系估计比较小,或许我换个模型试试
    varxo
        10
    varxo  
       47 天前
    我使用文心提示返回信息“必须返回 Json 格式”, 不加必须有时候返回的就不是 json
    LeslieLeung
        11
    LeslieLeung  
       47 天前
    可以尝试给 prompt 里加上 JSON schema ,我写了一篇文章可以看看 https://ameow.xyz/archives/llm-structural-output

    另外,据说 finetuning 给他几个 JSON 的例子也能让它更稳定地输出。
    DjvuLee
        12
    DjvuLee  
       47 天前
    @matrix1010 flash 啥时候支持 JSON Mode 了?只有 pro 支持。
    DjvuLee
        13
    DjvuLee  
       47 天前
    实测最好是是 GPT 系列,其他的都不稳定。指定 json mode 就可以。
    RaftingPoint
        14
    RaftingPoint  
    OP
       47 天前
    @varxo 我也加了,返回的 json 字符串是放在 markdown 格式里的,不知道是否和编码有关系
    matrix1010
        15
    matrix1010  
       47 天前
    @DjvuLee https://ai.google.dev/gemini-api/docs/json-mode?lang=python "This approach works with both Gemini 1.5 Flash and Gemini 1.5 Pro. Since the model gets the format specification from text in the prompt, you may have some flexibility in how you represent the specification. Any reasonable format for representing a JSON schema may work."
    DjvuLee
        16
    DjvuLee  
       47 天前
    @matrix1010 我看到的是下面原文开头是这样说的。我认为的是在 model 层面可以直接指定的这种。不过可能你说的是对的,实际上 flash 可以 work 。

    > In your prompt, you can ask Gemini to produce JSON-formatted output, but note that Google can't guarantee that it will produce JSON and nothing but JSON. However, if you use Gemini 1.5 Pro, you can pass a specific JSON schema in a response_schema field so that Gemini always responds with an expected structure.


    > As a structured schema supplied through model configuration. This approach works with Gemini 1.5 Pro but not Gemini 1.5 Flash.
    qweruiop
        17
    qweruiop  
       47 天前
    最好是用 function call ,实在不行就重试,目前是这样解决的。。。
    winterbells
        18
    winterbells  
       47 天前 via Android
    openai 的用过一段时间,很稳定,但还是得加校验
    NoobNoob030
        19
    NoobNoob030  
       47 天前
    复杂 json 场景,目前我用的解决方案是 few shot 里掺了点 markdown 的 json ,并且加了重试机制( 2-3 次) ,格式通过率在 99%,主要在处理中文符号和 \" 的时候会报错,中后期开发基本没有格式问题的烦恼
    joyce95
        20
    joyce95  
       47 天前
    我是偷懒党,不行就让重试。

    但是 openai 用提示词基本能靠谱返回,但也不稳定。
    unco020511
        21
    unco020511  
       47 天前
    一个非常简单的办法,那就是出错自动重试,不要把问题想复杂了
    nomagick
        22
    nomagick  
       47 天前
    模型输出错误,有几种模式,只要针对这几个模式处理其实就可以

    1 . 字符串中出现未转义的换行\n
    2. 输出 JSON 之前废话
    3. ```json 前缀和```尾缀

    更加复杂可以流式解析 json, 根据有限状态选择性抛弃 token , 但对大多数人来讲没必要
    elron
        23
    elron  
       47 天前
    kimi 可以 response_format 参数
    mercurylanded
        24
    mercurylanded  
       47 天前
    gpt 系列用 json schema 指定格式效果挺好的
    Theigrams
        26
    Theigrams  
       47 天前
    GPT4-o 或 4o-mini 都行,启用 json-mode

    https://platform.openai.com/docs/guides/json-mode
    easing
        27
    easing  
       47 天前
    指定 response_format 啊
    charslee013
        28
    charslee013  
       46 天前
    Cohere 的 c4ai-command-r 模型系列,可以直接输出 JSON 没有任何其他废话
    sunnysab
        29
    sunnysab  
       46 天前
    我处理过小文本,也遇到这样的情况:从歌曲文件名和元信息中提取歌手和歌曲名称,转换成 json 格式。在线模型比本地 7B 模型效果要好,另外可以在回答中找 json 信息,把回答中不相干的内容去掉。
    RaftingPoint
        30
    RaftingPoint  
    OP
       46 天前
    谢谢大家的建议, 昨天分析了下,导致转换失败的原因大致有几个:
    1. 输出的 json 里有注释语句之类的废话,里面的内容可能会导致歧义
    2. value 是列表时,里面的符号也会产生歧义,比如 冒号逗号分号 的中英混用,
    3. 还有转义符的问题

    整理了下大家的一些解决方案:
    1. 明确 json 格式的情况,可以用 function call , 这是最严谨稳定的方式,但是如果爬的内容不确定,灵活度就不够;
    2. 用可以声明 response_format 的模型,贴中提到的有 openai 系列,Gemini 1.5 flash/pro,c4ai-command-r 模型系列。
    3. langchain 等第三方工具
    4. 重试与 finetune

    我为了方便尝试了第三个方法,设置了 3 次重试,并且在对话中记录上一次成功的最长 json 串,在重试时加入 prompt 中作为参考 finetune ,这样下来一万条数据出错次数降低到了 3 条。这是我的解决方案,仅供参考
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1053 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:21 · PVG 07:21 · LAX 16:21 · JFK 19:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.