导言

现在市面上有许多建立 AI Bot 的渠道和运用,开源的有langchain、flowise、dify、FastGPT 等等。字节之前也推出了 Coze,之前试过 Dify 和 FastGPT,现在感觉 Coze 的插件才能有许多,且易用性方面、建立功率方面也强于其他渠道(例如langchain 或 flowise 需求建立相对复杂的编排逻辑才能完成大模型调用互联网信息的拓宽才能,可是 Coze 则是直接增加 plugin 且不指定任何参数就能完成)。

于是想尝试用 Coze 建立一个 TiDB 文档帮手,顺便研讨厘清 Coze 渠道是如何笼统一些大模型和其他才能来进步易用和建立功率的。

完成原理

首要咱们先抛开 Coze 渠道,在大模型供给才能的基础上如何完成调用文档数据?

这儿给出两种方法:常识库 和 function call。常识库的长处在于对非实时数据有一个相对准确的近似查询,function call的长处在于能够实时取得最新的数据,当然也包含文档数据。

Coze 渠道中的 plugins 完成了function方法,同时也供给了 knowledge 常识库能够管理本地和在线的文档。

embedding + 向量库

咱们先来介绍根据 文本表明模型 (embedding model) + 向量数据库 (vector db) 增强大模型才能的方法。主要分为两个任务:

  • 离线任务(同步原始文档到向量库):
    1. 由于大模型本身会有 token 长度束缚,所以需求现将原始文档进行切片(coze 渠道的常识库才能,主动切割方法下将每块分片内容束缚在最大 800 tokens)。
    2. 运用embedding model 文本表明模型对每个分片进行embedding,将其转换为 向量的方法
    3. 将向量存储在向量数据库中特定的collection

运用 Coze 建立 TiDB 帮手

  • 在线任务(用户发问):
    1. 运用 embedding model 对用户的问题做向量化

    2. 经过用户问题的向量数据,恳求向量数据库做 ANN 近似近邻查询,并指定回来 topK

    3. 拿到对应 topK 分片后,咱们需求结合分片内容和用户问题,拼凑完好的 prompt。示例如下,quote 为文档的分片内容,question为用户的实际问题

      运用 符号中的内容作为你的常识:

      {{quote}}

      答复要求:

      • 假如你不清楚答案,你需求弄清。
      • 防止提及你是从 获取的常识。
      • 保持答案与 中描述的一致。
      • 运用 Markdown 语法优化答复格式。
      • 运用与问题相同的言语答复。

      问题:”{{question}}”

    1. 最后恳求大模型,拿到成果即可

运用 Coze 建立 TiDB 帮手
在这种以常识库为主的方法下,比较要害的是 embedding model 、向量数据库 和 prompt。下面咱们要点说一下 embedding model 和 向量库。

embedding

假如是自己尝试的话,embedding model 主张选 huggingface开源模型,详细的排名 huggingface 上也有,能够看 Massive Text Embedding Benchmark (MTEB) Leaderboard。中文长文本现在排名比较高的是 tao-8k,向量化后的维度是1024,详细的调用示例如下:

def tao_8k_embedding(sentences):
    import torch.nn.functional as F
    from transformers import AutoModel, AutoTokenizer
    model = AutoModel.from_pretrained("tao-8k")
    tokenizer = AutoTokenizer.from_pretrained("tao-8k")
    batch_data = tokenizer(sentences,
                           padding="longest",
                           return_tensors="pt",
                           max_length=8192,
                           # 封闭主动截断。默认为 true,即超过 8192 token 的文本会主动截断
                           truncation="do_not_truncate", )
    outputs = model(**batch_data)
    vectors = outputs.last_hidden_state[:, 0]
    vectors = F.normalize(vectors, p=2, dim=1)

当然除了开源的外,像百川、OPENAI、ChatGLM、文心等等都供给了 embedding API。OPENAI的文档如下:embeddings ,其他的咱们能够自行去官网找文档。

向量库

向量库的挑选也比较多,开源的有:国产分布式架构的 Milvus、standalone单机部署的 Qdrant 和根据local且no-server的 Chroma 等;根据现有数据库系统拓宽了向量才能的有 ElasticsearchPgVectorRedis 等;甚至还有一些向量库的DBaas,比如 zilliz cloud。抛开这些运用,向量库的核心主要是3点:间隔度量挑选、向量维度、索引类型。

以 Qdrant 为例,能够快速运用 docker 构建镜像。向量库的同步、查询等能够看 Qdrant 接口文档

docker pull qdrant/qdrant
docker run -p 6333:6333 -p 6334:6334 
    -v $(pwd)/qdrant_storage:/qdrant/storage:z 
    qdrant/qdrant

system + 插件 (function)

根据常识库的方法很大程度上能够完成文档问答的才能,可是也有缺陷:

  • 需求保护向量库,且假如为了降低成本运用开源embedding,那么需求在本地保护embedding模型。
  • 文档同步实时性问题。文档一旦更新,需求及时同步,否则会拿到旧数据。

这儿介绍别的一种 system人设 + function call 的方法。system比较简单就是用一段描述性prompt来设定模型的布景、才能、目标等等人设相关的信息;function call 是给大模型界说一些拓宽才能,让大模型能够获取自己拿不到的数据。详细如何把他们串联起来,过程如下:

  1. 用户设定 人设 (system) 和 插件 (function),并发问

  2. 服务端合并组合参数,并将用户挑选的插件映射为大模型中的 function 工具,然后恳求大模型

  3. 大模型判断是否需求调用 function

  4. 假如不需求 function,则服务端直接回来大模型成果即可;

    假如需求调用 function,大模型会回来详细的函数和参数值,此刻服务端经过本身的联网才能,履行 function 并将成果反哺给大模型

  5. 大模型拿到 function 的成果后,终究给用户一个明确的答复

运用 Coze 建立 TiDB 帮手

function call

system 这部分就不额定介绍了,主要说说 function call。

前面提到,Coze 渠道的 Plugins 是采用了 function call 的才能,下面以 Github plugin 为例,尝试用OPENAI 界说的 function 的 schema 格式来界说它:

运用 Coze 建立 TiDB 帮手

{
    "type": "function",
    "function": {
        "name": "Github-searchRepositories",
        "description": "search Repositories",
        "parameters": {
            "type": "object",
            "properties": {
                "q": {
                    "type": "string",
                    "description": "format like "keywords+language:js", language can be other dev languages"
                },
                "sort": {
                    "type": "string",
                    "description": "Default: stars, Can be one of: stars, forks, help-wanted-issues, updated",
                    "enum": [
                        "stars",
                        "forks",
                        "help-wanted-issues",
                        "updated"
                    ]
                },
                "order": {
                    "type": "string",
                    "description": "Default: desc, Can be one of: desc, asc",
                    "enum": [
                        "desc",
                        "asc"
                    ]
                }
            },
            "required": [
                "q"
            ]
        }
    }
}

现在咱们知道了,OPENAI 会经过咱们事前界说好的 function 来做判断,假如需求 function 供给的才能,大模型会给咱们一个回调恳求,以 Github-searchRepositories 为例,详细的履行实际是调用Github的OpenAPI,将其成果给到大模型。

运用 Coze 建立 TiDB 帮手

Coze 建立 bot

咱们前面介绍了详细的完成方法,下面咱们在 Coze 渠道快速来建立 TiDB Help Bot。不过再次之前,咱们先参考一下 CloudWeGoHelpBot 的完成方法。

CloudWeGoHelpBot

首要介绍一下建立过程,由于我选用的是文档帮手,所以参考了 coze 渠道在 explore 中的 CloudWeGoHelpBot,来看看它是怎样构建的。

运用 Coze 建立 TiDB 帮手

能够看到这儿主要有三个部分:

  1. Persona & Prompt:给大模型设定了人设、技术、束缚和目标。对应 system 的部分。
  2. Plugins:Github 查询代码库的插件,经过Github的 SearchRepositoriesApi ; Browser 查询网页的插件,能够得到网站的标题、内容和衔接。对应 function 的部分。
  3. Opening Dialog:开场白,个人感觉这部分内容不参与和大模型的交互,功用是帮助用户快速理解Bot的功用和意图。

TiDB Help Bot

现在让咱们来创造一个 TiDB Help Bot!

Plugins

Plugins 设定和 CloudWeGoHelpBot 相似,运用 Github-searchRepositories 和 Browser-browse_raw。

运用 Coze 建立 TiDB 帮手

运用 Coze 建立 TiDB 帮手

Persona & Prompt

Persona & Prompt 内容中需求明确TiDB 的文档地址和代码库地址,这儿直接用的 CloudWeGoHelpBot 的模板,并把相应的信息改成 TiDB,示例如下:

# Role: TiDB Support and Assistance Bot
You're TiDB Help Bot, the  dedicated support for all things TiDB. Whether users are troubleshooting, seeking documentation, or have questions about TiDB, TiKV, PD and other sub-projects, you're here to assist. Utilizing the official TiDB documentation (https://docs.pingcap.com/) and GitHub repositories (https://github.com/pingcap, https://github.com/tikv), you ensure users have access to the most accurate and up-to-date information. You provide a smooth and productive experience.
## Skills
- Proficient in natural language processing to understand and respond to user queries effectively.
- Advanced web scraping capabilities to extract information from the official TiDB documentation (https://docs.pingcap.com/).
- Integration with the official GitHub repositories (https://github.com/pingcap, https://github.com/tikv) for real-time updates and issue tracking.
- Knowledge of TiDB's sub-projects, such as TiDB、TiKV and PD, to provide specialized assistance.
- User-friendly interface for clear communication and easy navigation.
- Regular updates to maintain synchronization with the latest documentation and GitHub repository changes.
## Constraints
- Adhere to copyright laws and terms of use for the TiDB documentation and GitHub repository.
- Respect user privacy by avoiding the collection or storage of personal information.
- Clearly communicate that the bot is a support and information tool, and users should verify details from official sources.
- Avoid promoting or endorsing any form of illegal or unethical activities related to TiDB or its sub-projects.
- Handle user data securely and ensure compliance with relevant privacy and data protection regulations.
## Goals
- Provide prompt and accurate assistance to users with questions or issues related to TiDB and its sub-projects.
- Offer detailed information from the official TiDB documentation for comprehensive support.
- Integrate with the GitHub repository to track and address user-reported issues effectively.
- Foster a positive and collaborative community around TiDB by facilitating discussions and knowledge sharing.
- Ensure the bot contributes to a smooth and productive development experience for TiDB users.
- Establish TiDB Help Bot as a trusted and reliable resource for developers and contributors.
- Encourage user engagement through clear communication and proactive issue resolution.
- Continuously improve the bot's capabilities based on user feedback and evolving needs within the TiDB community.

knowledge

首要需求再主页增加一个 knowledge 常识库,需求注意一点的是,Coze 渠道这儿分为了 text formattable format ,第一种一次只能同步一个文档,第二个能够一次同步多个但需求以 csv 或者 api 回来的 json 格式。

以同步【PingCAP 文档中心 | 主页】为例,咱们直接经过 text format 中的Online data ,贴上主页地址即可。

运用 Coze 建立 TiDB 帮手

opening dialog

开场白和开场问题咱们能够在 Coze 渠道主动生成,生成如下:

I’m TiDB Help Bot, your dedicated support for all things TiDB. Whether you need troubleshooting assistance, documentation, or have questions about TiDB, TiKV, PD, and other sub-projects, I’m here to help. With access to the official TiDB documentation and GitHub repositories, I provide accurate and up-to-date information for a smooth and productive experience.

至此咱们的 TiDB Help Bot 就做好了。

运用 Coze 建立 TiDB 帮手