OpenAI 
Authentication 
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"
 
| Parameters | Type | Required | Desc | 
|---|---|---|---|
| strategy | string | true | strategy | 
| response_format | string | false | 返回格式,如果需要返回 json 则为 json_object | 
| parameters | object | false | 用于渲染服务端配置的 system prompt 中的变量 | 
| chat_id | string | false | 如果在 console 配置保存历史记录则请求服务端会返回,后续可带上 | 
| messages | []object | true | 详细格式如下 | 
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 Chat Stream 
接口功能: OpenAI Chat Stream
Method & Path 
-  
POST {domain}/bp/ai/chat/stream -  
POST {domain}/bp/server/user/{user_id}/ai/chat/stream 
Request 
参考 Chat
Response 
- HTTP 状态码:200
 
json
event: ai_chat_msg
data: {"chat_id":"","content":"如果","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"你","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"指","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的是","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"流","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"媒体","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"(","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"stream","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"ing","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"),","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"我","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"可以","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"提供","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"相关","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的信息","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"和","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"支持","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"。","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"流","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"媒体","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"是","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"指","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"通过","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"互联网","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"实时","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"传","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"输","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"音","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"频","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"、","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"视频","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"等","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"内容","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"技术","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":",","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"用户","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"可以","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"在","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"不","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"需要","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"下载","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"情况下","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"观看","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"或","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"收","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"听","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"。不","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"论","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"是","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"影视","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"、","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"音乐","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"还是","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"直播","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":",","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"流","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"媒体","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"都有","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"着","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"广","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"泛","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"应用","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"。\n\n","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"如果","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"你","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"有","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"具体","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"问题","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"或","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"需要","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"了解","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"特","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"定","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"的","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"流","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"媒体","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"服务","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":",请","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"告诉","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"我","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"!","is_finish":false}
event: ai_chat_msg
data: {"chat_id":"","content":"","is_finish":true}- HTTP 状态码:4xx
 
json
{
  "error": {
    "error_type": "invalid_parameter",
    "message": "xxxxx"
  }
}error_type:
- item_not_found(找不到条目)
 - 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 
| Query | Type | Required | Desc | 
|---|---|---|---|
| chat_id | string | true | chat_id | 
| page | int | false | default: 1 | 
| page_size | int | false | default: 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/jsonBody
Parameters Type Required Desc 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 配置问题)
