api接口调用百度千帆大模型提示错误码336007:“prompt can not be empty”如何解决?

api接口调用百度千帆大模型提示错误码336007:“prompt can not be empty”如何解决?

5 回复

确保请求中包含非空的提示信息。


错误码336007表示提示词不能为空。请检查并确保调用API时提供了有效的prompt参数。

错误码336007表示“prompt can not be empty”,即提示词不能为空。解决方法:确保在调用API时,prompt参数已正确填写且不为空。检查请求体中的prompt字段,确保其包含有效的输入内容。

确保请求中提供的(prompt)不为空。

错误码 336007 表示在调用百度千帆大模型的API时,prompt 参数为空。prompt 是生成文本时必需的输入参数,它不能为空。要解决这个问题,请确保在API请求中正确传递了 prompt 参数。以下是一个示例代码片段,展示如何正确调用API:

import requests

# 替换为你的API Key和Secret Key
API_KEY = 'your_api_key'
SECRET_KEY = 'your_secret_key'

# 获取access token
def get_access_token():
    url = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={API_KEY}&client_secret={SECRET_KEY}'
    response = requests.get(url)
    return response.json().get('access_token')

# 调用百度千帆大模型API
def call_baidu_model(prompt):
    access_token = get_access_token()
    url = f'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={access_token}'
    headers = {'Content-Type': 'application/json'}
    data = {
        "messages": [
            {"role": "user", "content": prompt}
        ]
    }
    response = requests.post(url, json=data, headers=headers)
    return response.json()

# 示例调用
prompt = "你好,介绍一下你自己。"  # 确保prompt不为空
response = call_baidu_model(prompt)
print(response)

关键点:

  1. 检查 prompt 参数:确保在请求中传递了一个非空的 prompt 参数。
  2. API请求格式:确保请求体格式正确,特别是 messages 列表中的 content 字段。
  3. 调试:如果问题仍然存在,可以打印出请求体和响应,以便进一步调试。

通过以上步骤,你应该能够解决 336007 错误。

回到顶部