Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: HTTP API 重构 #790

Merged
merged 6 commits into from
May 10, 2023
Merged

refactor: HTTP API 重构 #790

merged 6 commits into from
May 10, 2023

Conversation

lss233
Copy link
Owner

@lss233 lss233 commented May 10, 2023

This refactor is a part of #755 .

进行了以下修改:

  • 将 OneBot、Wecom 和 HTTP API 的 Quart 实例合并为一个
  • 设计了新的 HTTP API,旧的 HTTP API 将会在不久后移除

新 HTTP API

Endpoint

请求方法 URL
POST /v1/conversation

请求体

字段 类型 必填 描述
session_id string Yes 会话 ID。
user_id string Yes 用户 ID。
group_id string No 群组 ID(如果适用)。
nickname string No 用户昵称。
messages Message[] Yes 表示用户输入的消息对象数组。
is_manager boolean No 是否为管理员会话,默认为否。
prefered_format string No 语音的编码格式,支持"wav"(默认), "mp3", "silk", "amr"

Message 对象

字段 类型 必填 描述
type string Yes 消息类型。当前支持的类型包括 "text", "image", 和 "voice"
value string Yes 消息内容。对于不同的消息类型,内容的格式和含义也会不同。

Message 类型

当前支持的消息类型如下:

  • "text":文本消息,内容为纯文本。
  • "image":图片消息,内容为图片的 BASE64 编码字符串。
  • "voice":语音消息,内容为语音的 BASE64 编码字符串以及音频格式和文本转语音的结果(可选)。

对于 "image" 类型的消息,值应该是图片的 BASE64 编码字符串,例如:

{
    "type": "image",
    "value": "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg..."
}

对于 "voice" 类型的消息,值应该是语音的 base64 编码字符串、音频格式以及文本转语音的结果(可选),例如:

{
    "type": "voice",
    "value": "//uQRAA...",
    "format": "wav",
    "text": "你好!"
}

其中,"value" 字段的值是语音的 BASE64 编码字符串,"format" 字段的值是音频格式,例如 "wav",而 "text" 字段的值是文本转语音的结果(可选)。

请求示例:

{
    "session_id": "friend-1",
    "user_id": "233",
    "group_id": "444",
    "nickname": "路人甲",
    "messages": [
        {
            "type": "text",
            "value": "你好?"
        }
    ]
}

响应

服务器会以 Server-Sent Event(SSE)流的形式返回 JSON 对象,代表机器人的响应。每个响应对象可能包含一个或多个消息对象,具体取决于机器人的输出。

响应示例:

{
  "messages": [
    {
      "type": "text",
      "value": "你好!"
    },
    {
      "type": "voice",
      "value": "data:audio/wav;base64,//uQRAA...",
      "format": "wav"
    },
    {
      "type": "image",
      "value": "data:image/png;base64,iVBORw0KGg...",
    }
  ]
}

响应包含一个 JSON 对象,其中包含一个 "messages" 字段,它是一个消息对象数组。每个消息对象都有 "type""value""format" 字段(如果适用)。"value" 字段包含以 Base64 格式编码的消息内容。请注意,响应可能包含多个消息对象,具体取决于机器人的输出。

客户端示例代码

Python

import requests
import json

# 设置 API 端点 URL
url = "http://127.0.0.1:8080/v1/conversation"

# 设置负载(payload)
payload = {
    "session_id": "friend-1",
    "user_id": "233",
    "group_id": "444",
    "nickname": "路人甲",
    "messages": [
        {
            "type": "text",
            "value": "你好?"
        }
    ]
}

# 发送 HTTP 请求
response = requests.post(url, json=payload, stream=True)

# 将响应解析为 JSON 对象
for line in response.iter_lines():
    if line:
        response_json = json.loads(line)
        break

# 打印响应
print(json.dumps(response_json, indent=4, ensure_ascii=False))

JavaScript

// 设置 API 端点 URL
const url = "http://127.0.0.1:8080/v1/conversation";

// 设置负载(payload)
const payload = JSON.stringify({
    session_id: "friend-1",
    user_id: "233",
    group_id: "444",
    nickname: "路人甲",
    messages: [
        {
            type: "text",
            value: "你好?"
        }
    ]
});

// 发送 HTTP 请求并监听 SSE 事件
const eventSource = new EventSource(url, { method: "POST", body: payload });
eventSource.addEventListener("message", (event) => {
    // 将响应解析为 JSON 对象
    const responseJson = JSON.parse(event.data);

    // 记录响应
    console.log(responseJson);
});

Co-authored-by: Sourcery AI <>
@lss233 lss233 merged commit 380810a into refactor-v3 May 10, 2023
@lss233 lss233 deleted the refactor-http-api branch May 10, 2023 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant