Api接口调用百度千帆大模型提示`unsupported content type`错误。

Api接口调用百度千帆大模型提示unsupported content type错误。

5 回复

确保请求头中Content-Type设置正确,如application/json。


检查请求头中的Content-Type,确保设置为application/json

unsupported content type错误通常是由于请求头中的Content-Type设置不正确导致的。请确保在调用百度千帆大模型的API时,将Content-Type设置为application/json,并且请求体是合法的JSON格式。例如:

POST /your-endpoint HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer your-access-token

{
    "prompt": "你的输入内容"
}

如果问题仍然存在,请检查API文档,确认是否有其他特定的请求头或格式要求。

确保请求头中Content-Type设置正确,如application/json。

unsupported content type 错误通常是由于请求头中的 Content-Type 不正确或不匹配导致的。百度千帆大模型的API接口通常要求请求头的 Content-Typeapplication/json,并且请求体需要是有效的JSON格式。

以下是一个示例代码,展示如何正确调用百度千帆大模型的API接口:

import requests
import json

# 替换为你的API Key和Secret Key
api_key = 'your_api_key'
secret_key = 'your_secret_key'

# 获取Access Token
auth_url = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}'
auth_response = requests.get(auth_url)
access_token = auth_response.json().get('access_token')

# 构建请求URL
request_url = f'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={access_token}'

# 构建请求体
payload = {
    "messages": [
        {"role": "user", "content": "你好,请问你是谁?"}
    ]
}

# 设置请求头
headers = {
    'Content-Type': 'application/json'
}

# 发送POST请求
response = requests.post(request_url, headers=headers, data=json.dumps(payload))

# 打印响应结果
print(response.json())

关键点:

  1. Content-Type: 确保请求头中的 Content-Type 设置为 application/json
  2. 请求体: 确保请求体是有效的JSON格式。
  3. Access Token: 获取并正确传递 access_token

如果仍然遇到问题,请检查:

  • 请求体的JSON格式是否正确。
  • 请求URL是否正确。
  • 你的API Key和Secret Key是否有效。
回到顶部