Skip to content

OpenAI

Authentication

参考接入 BytePower

OpenAI Chat

接口功能: OpenAI Chat

Method & Path

  • POST {domain}/bp/ai/chat
  • POST {domain}/bp/server/user/{user_id}/ai/chat

Request

  • content-type: "application/json"
ParametersTypeRequiredDesc
strategystringtruestrategy
response_formatstringfalse返回格式,如果需要返回 json 则为 json_object
parametersobjectfalse用于渲染服务端配置的 system prompt 中的变量
chat_idstringfalse如果在 console 配置保存历史记录则请求服务端会返回,后续可带上
messages[]objecttrue详细格式如下
json
{
  "strategy": "free-normal",
  "response_format": "json_object",
  "parameters": {
    "example_string": "example_value",
    "example_object": {
      "key": "value",
      "key2": "value2"
    }
  },
  "messages": [
    {
      "content": [
        {
          "type": "text",
          "text": "10个字描述一下图片"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
          }
        }
      ],
      "role": "user"
    }
  ]
}
  • 说明 parameters 用于在 BytePower 定义了 System Prompt 的 strategy,例如:
现在你扮演 {{ .example_string }}, 告诉我如何使用 {{ .example_object.key }} 完成 {{ .example_object.key2 }}。

Response

  • HTTP 状态码:200
json
{
  "asset": [
    {
      "last_recovery_time": "2024-07-05T00:00:00Z",
      "name": "ai_credit",
      "quantity": 50,
      "recoverable": true,
      "recovery_period": 1,
      "recovery_quantity": 50,
      "type": "consumable",
      "valid_seconds": 0
    }
  ],
  "chat_id": "CtESPJTMUWJ2R6RDSW",
  "current_count": 7,
  "current_total_count": 56,
  "reply": {
    "role": "assistant",
    "content": "一只黑色的蚂蚁在地上。"
  },
  "tokens": {
    "prompt_tokens": 1224,
    "completion_tokens": 13,
    "total_tokens": 1237
  },
  "user_surplus_count": 93
}
  • HTTP 状态码:4xx
json
{
  "error": {
    "error_type": "invalid_parameter",
    "message": "xxxxx"
  }
}

error_type:

  • item_not_found(找不到条目)
  • config_invalid (console 配置问题)

OpenAI Image

接口功能: OpenAI Image

Method & Path

  • POST {domain}/bp/ai/image
  • POST {domain}/bp/server/user/{user_id}/ai/image

Request

  • content-type: "application/json"
ParametersTypeRequiredDesc
strategystringtruestrategy
promptstringtrueprompt
image_numintfalse生成图片数量,默认为 1
json
{
  "strategy": "openai_image",
  "prompt": "蓝天白云"
}

Response

  • HTTP 状态码:200
json
{
  "created": 1702958735,
  "current_count": 1,
  "current_total_count": 1,
  "data": [
    {
      "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-U5c612E6GbvsGMV2CjelMb5S/user-D1jXQkHEV2qPCgW4UEfpBf8A/img-HmCH6Vknk7O82E8jj1Wd0107.png?st=2023-12-19T03%3A05%3A35Z&se=2023-12-19T05%3A05%3A35Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-12-19T01%3A07%3A54Z&ske=2023-12-20T01%3A07%3A54Z&sks=b&skv=2021-08-06&sig=B7FMxMT1/8UmbDfKp49iyK8cZULqXzTKeIN62KSWwtc%3D"
    }
  ]
}
  • HTTP 状态码:4xx
json
{
  "error": {
    "error_type": "invalid_parameter",
    "message": "xxxxx"
  }
}

error_type:

  • invalid_parameter(无效参数)
  • config_invalid (console 配置问题)

OpenAI Chat History

接口功能: OpenAI Chat History

Method & Path

  • GET {domain}/bp/ai/chat/history
  • GET {domain}/bp/server/user/{user_id}/ai/chat/history

请求 Query

QueryTypeRequiredDesc
chat_idstringtruechat_id
pageintfalsedefault: 1
page_sizeintfalsedefault: 10

Response

  • HTTP 状态码:200
json
{
  "chat_histories": [
    {
      "chat_id": "CtECONAUZEUIWZI2Y6",
      "app_id": "APPYHW3JWTEJRAFE",
      "user_id": "UUD6DSJRTMEXU",
      "token": 49,
      "strategy": "free-normal",
      "role": "",
      "messages": [
        {
          "role": "user",
          "content": "如何形容一个人清高,10个字"
        },
        {
          "role": "assistant",
          "content": "高傲孤傲一德高挺冷傲傲人优越不凡"
        }
      ],
      "sys_created_time": "2023-12-19T04:17:31.940758Z",
      "sys_updated_time": "2023-12-19T04:17:31.940758Z"
    }
  ],
  "total_chat": 1
}
  • HTTP 状态码:4xx
json
{
  "error": {
    "error_type": "invalid_parameter",
    "message": "xxxxx"
  }
}

error_type:

  • item_not_found(找不到条目)
  • config_invalid (console 配置问题)

OpenAI Instruction

预设 Prompt 的单次问答的与 AI 交互的方式。 通过配置一组固定的 Prompt 实现一个具体功能的提问模板,请求服务端时只需要将关键的信息通过参数传递。

  • 参考配置
json
{
  "instruction": [
    {
      "name": "test",
      "response_format": "",
      "model": "gpt-3.5-turbo",
      "max_token": 1000,
      "temperature": 1,
      "messages": [
        {
          "role": "system",
          "content": "you are a professional {{ .role }}."
        },
        {
          "role": "user",
          "multi_content": [
            {
              "type": "text",
              "text": "Tell me about the {{ .question }}. Patient info age: {{ .patient.age }}, gender: {{ .patient.gender }}"
            },
            {
              "type": "image_url",
              "image_url": {
                "url": ""
              }
            }
          ]
        }
      ]
    }
  ]
}
  • 接口功能: OpenAI Instruction

Method & Path

  • POST {domain}/bp/ai/chat/instruction
  • POST {domain}/bp/server/user/{user_id}/ai/chat/instruction

Request

  • Headers

    Content-Type: application/json

  • Body

    ParametersTypeRequiredDesc
    namestringtrueinstruction name
    parametersobjectfalse用于渲染服务端配置的 prompt 中的变量,包含 system, user
json
{
  "name": "test",
  "parameters": {
    "role": "doctor",
    "question": "H1N1",
    "patient": {
      "age": "24",
      "gender": "man"
    }
  }
}

Response

  • HTTP 状态码:200
json
{
  "instruction": {
    "content": "Hello! How can I assist you today?"
  },
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 9,
    "total_tokens": 17
  }
}
  • HTTP 状态码:4xx
json
{
  "error": {
    "error_type": "invalid_parameter",
    "message": "xxxxx"
  }
}
  • error_type

    • item_not_found(找不到条目)
    • config_invalid (console 配置问题)

京ICP备19011570号-2