You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Llama AI API调用异常:返回JSON的content字段始终为NULL求助

问题分析与解决方案

核心问题

调用Llama AI API获取食品行业员工NDA起草常见问题时,返回结果的content字段始终为NULL,核心原因是对API的函数调用机制理解有误,同时代码存在几处关键错误。

错误根源

  1. 误解函数调用机制:你定义的get_nda_questions是需要自行实现的外部工具函数,Llama API仅会返回调用该函数的参数指令,不会自动执行这个函数。原代码中模拟了函数返回结果,但未明确告知模型无需再调用函数。
  2. 参数解析错误:模型返回的function_call.arguments是JSON字符串,原代码直接当作对象访问,导致无法正确获取参数值。
  3. 未禁用函数调用:第二次请求仍保留functions定义,但未设置function_call: "none",模型会继续尝试返回函数调用指令而非自然语言回答。
  4. 笔误问题:变量名flight_info与业务逻辑无关,属于无效命名。

修复方案

方案1:简化请求(无需函数调用)

如果不需要依赖外部工具,直接让模型生成回答,无需定义functions和function_call:

app.get('/contract', async (req, res) => {
    try {
        const { default: LlamaAI } = await import('llamaai');
        const llamaAPI = new LlamaAI(apiToken);

        const requestJson = {
            "model": "llama-70b-chat",
            "messages": [
                { "role": "user", "content": "请列出起草食品行业员工NDA时需要考虑的常见问题" }
            ],
            "stream": false
        };

        const response = await llamaAPI.run(requestJson);
        const content = response['choices'][0]['message']['content'];
        
        res.send({ content });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

方案2:正确使用函数调用流程

若需保留函数调用逻辑,需自行实现工具函数,并在第二次请求中禁用函数调用:

// 自行实现外部工具函数,返回食品行业员工NDA的常见问题
async function get_nda_questions(params) {
    if (params.Industry === "Food" && params.BusinessType === "Employee") {
        return JSON.stringify([
            "员工接触的核心机密信息包含哪些(如配方、供应链数据、客户名单)?",
            "保密义务的期限是多久(离职后是否仍需遵守)?",
            "是否包含竞业限制条款,其范围和期限如何界定?",
            "员工违反保密义务的赔偿标准及追责方式是什么?",
            "哪些信息属于保密例外(如已公开信息、员工自行研发的内容)?",
            "是否允许员工在工作中使用机密信息进行创新,成果归属如何约定?"
        ]);
    }
    return JSON.stringify([]);
}

app.get('/contract', async (req, res) => {
    try {
        const { default: LlamaAI } = await import('llamaai');
        const llamaAPI = new LlamaAI(apiToken);

        // 第一次请求:让模型判断是否需要调用工具并返回参数
        const firstRequest = {
            "model": "llama-70b-chat",
            "messages": [
                { "role": "user", "content": "What are the common questions to ask to create an NDA for a food industry for an employee?" }
            ],
            "functions": [
                {
                    "name": "get_nda_questions",
                    "description": "Get the frequently asked questions to generate an NDA contract",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "Industry": {
                                "type": "string",
                                "description": "The industry to generate this contract in"
                            },
                            "BusinessType": {
                                "type": "string",
                                "description": "NDA for a business or individual"
                            }
                        },
                        "required": ["Industry", "BusinessType"]
                    }
                }
            ],
            "stream": false
        };

        const firstResponse = await llamaAPI.run(firstRequest);
        const firstOutput = firstResponse['choices'][0]['message'];

        if (firstOutput.function_call) {
            // 解析模型返回的参数(注意:arguments是JSON字符串,需解析为对象)
            const funcArgs = JSON.parse(firstOutput.function_call.arguments);
            // 执行自行实现的工具函数
            const toolResult = await get_nda_questions(funcArgs);

            // 第二次请求:传递工具结果,强制模型生成自然语言回答
            const secondRequest = {
                "model": "llama-70b-chat",
                "messages": [
                    { "role": "user", "content": "What are the common questions to ask to create an NDA for a food industry for an employee?" },
                    { "role": "function", "name": firstOutput.function_call.name, "content": toolResult }
                ],
                "stream": false,
                "function_call": "none" // 关键:告知模型无需再调用函数
            };

            const secondResponse = await llamaAPI.run(secondRequest);
            const finalContent = secondResponse['choices'][0]['message']['content'];
            res.send({ content: finalContent });
        } else {
            // 模型直接返回了回答内容
            res.send({ content: firstOutput.content });
        }
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

关键注意事项

  • 所有请求需指定一致的模型(如llama-70b-chat),避免模型不一致导致的行为差异。
  • 若使用函数调用,必须自行实现对应的工具函数,API不会自动执行你定义的函数。
  • 模型返回的function_call.arguments是JSON字符串,必须用JSON.parse()解析后才能访问参数。

内容的提问来源于stack exchange,提问作者user3157674

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:24:51