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

调用OpenAI text-davinci-003 API返回无关随机结果的排查求助

问题描述

我通过Node脚本,用Axios抓取eater.com网页,再用Cheerio解析DOM提取文本。编写的Prompt在OpenAI Playground中运行完全正常,但通过API调用时,每次都会返回逻辑通顺但完全无关的随机内容,比如品牌营销文案、Swift代码片段等。

示例返回结果:

[
    {
        "text": "In a world of ever-increasing competition, Branding is the key to success.\n\nBranding is the process of creating a unique image or identity for a product, service, or company. It is a way of distinguishing one company or product from another in the minds of consumers. Branding is essential in today’s world as it helps businesses stand out from the competition and gain an edge in the marketplace. A good brand can create a lasting impression on customers, differentiate a product or service from its competitors, and increase customer loyalty. It also serves to create an emotional connection between customers and the brand. Branding can help a business build a strong reputation, increase customer loyalty, and generate more revenue. It is also an important tool for communicating a company’s values and mission. Branding can also be used to increase credibility and trust in a company’s products and services. It can help a company establish a presence in a particular market and create a sense of familiarity with customers.",
        "index": 0,
        "logprobs": null,
        "finish_reason": "stop"
    }
]

可能的原因

  • 参数配置不一致:Playground会自动填充temperature、max_tokens、stop等默认参数,但API调用时如果未显式设置这些参数,可能使用不同的默认值。比如temperature过高会提升输出随机性,stop参数缺失可能导致模型无法正确终止任务。
  • 提取文本格式异常:Cheerio提取的DOM文本可能存在隐藏特殊字符、换行混乱或编码错误,传递给API后,模型无法区分任务指令与输入文本的边界,进而偏离任务生成无关内容。
  • Prompt结构不清晰:Playground可能自动维护会话上下文,但API单次请求需确保Prompt完整包含任务指令和输入文本,且用明确分隔符(如---)区分两者。若脚本拼接Prompt时格式混乱,模型会误解任务目标。
  • 模型或端点不匹配:API调用的模型版本(如是否为text-davinci-003)、请求端点是否与Playground一致,不同模型或端点的行为存在差异。
  • 输入长度超限:提取的网页文本过长,超过模型上下文窗口限制(text-davinci-003最多支持4096 tokens),导致任务指令被截断,模型转而生成随机内容。

排查建议

  1. 对齐参数设置:将Playground中的所有参数(temperature、max_tokens、stop等)完全复制到API调用的参数配置中,确保两者参数一致。
  2. 清洗提取文本:在脚本中添加日志,输出提取后的文本内容,检查是否存在乱码、多余空白或特殊字符,必要时做格式化处理(如去除冗余换行、转义特殊字符)。
  3. 规范Prompt格式:用明确分隔符区分任务指令和输入文本,示例格式如下:
    请处理以下网页文本,提取关键信息:
    ---
    [提取的网页文本内容]
    
  4. 验证模型与端点:确认API请求的model参数为text-davinci-003,请求端点为https://api.openai.com/v1/completions。
  5. 控制输入长度:对提取的网页文本做截断或摘要处理,确保总输入长度在模型上下文窗口限制内。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 15:00:42