Skip to content

Instantly share code, notes, and snippets.

@wey-gu
Last active September 1, 2022 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wey-gu/728aa22abf4821feb8e64659fffa223b to your computer and use it in GitHub Desktop.
Save wey-gu/728aa22abf4821feb8e64659fffa223b to your computer and use it in GitHub Desktop.
First app with NebulaGraph

前言

像一般的 Web 应用之于数据库一样,一个典型的基于 NebulaGraph 的传统线上应用可以又三部分组成:

  • 前端,作为人机交互界面,调用后端 API
  • 后端,处理基于 Web 的 API,生成 NebulaGraph 图数据库的查询语句,请求 NebulaGraph,处理结果返回 API 调用方
  • 图数据库:NebulaGraph

今天,我们给大家简单演示如何开发一个基于 NebulaGraph 的智能机器人应用。本节课旨在快速将我们在 NebulaGraph 入门课程所学与实践开发落地场景连接起来,不会拘泥所有的开发细节, 然而所有的细节在这个示例项目作者的博客中(地址 https://www.siwei.io/siwi/ )都有更详细介绍,如果同学们感兴趣,欢迎访问博客和它的 Github 代码仓库了解。

数据建模

这个问答助手将关注在单一领域:篮球相关,这里,我们采用 Basketballplayer 这个数据集,它的建模如下:<省略>

应用设计

智能机器人就像小爱机器人、Siri 一样,可以接受人类的问询,来进行问题的回答。

前端

我们用 VueJS 做一个带有文字和语音输入输出的机器人界面,它能接受问题,透过后端处理问题,返回问题的答案。这其中用到了 Vue Bot UI 这个项目和 Chrome 浏览器的 Web Speech API。

这个截图是它跑在浏览器里的样子,我们可以看到问题的发出和回复的文字,以及右边浏览器调试 console 里的返回数据结构:

后端

后端的部分,我们用 Python Flask 作为 Web 框架,用 Nebula-Python 作为后端访问 NebulaGraph 的客户端。

当一个前端的语句通过后端 RESTful API 发送到后端应用的时候,我们要实现的逻辑是:

  1. 对于剧进行意图识别和语义处理,得到问句的意图(Intent,比如”查关联“)与问句涉及的实体(Entities,比如 ”Yao Ming“ 与 ”火箭队“)。

  2. 根据不同意图,再构造图库查询(Intent Actor),调用 Nebula-Python 访问 NebulaGraph,通过 nGQL 查得结果。

  3. 处理结果为人能够理解的答案句子,返回。

架构、流程图如下:

┌────────────────┬──────────────────────────────────────┐
│                │                                      │
│                │  Speech                              │
│     ┌──────────▼──────────┐                           │
│     │            Frontend │   Siwi, /ˈsɪwi/           │
│     │ Web_Speech_API      │   A PoC of                │
│     │                     │   Dialog System           │
│     │ Vue.JS              │   With Graph Database     │
│     │                     │   Backed Knowledge Graph  │
│     └──────────┬──────────┘                           │
│                │  Sentence                            │
│                │                                      │
│   ┌────────────┼──────────────────────────────┐       │
│   │            │                              │       │
│   │            │              Backend         │       │
│   │ ┌──────────▼──────────┐                   │       │
│   │ │ Web API, Flask      │   ./app/          │       │
│   │ └──────────┬──────────┘                   │       │
│   │            │  Sentence    ./bot/          │       │
│   │ ┌──────────▼──────────┐                   │       │
│   │ │                     │                   │       │
│   │ │ Intent matching,    │   ./bot/classifier│       │
│   │ │ Symentic Processing │                   │       │
│   │ │                     │                   │       │
│   │ └──────────┬──────────┘                   │       │
│   │            │  Intent, Entities            │       │
│   │ ┌──────────▼──────────┐                   │       │
│   │ │                     │                   │       │
│   │ │ Intent Actor        │   ./bot/actions   │       │
│   │ │                     │                   │       │
│   └─┴──────────┬──────────┴───────────────────┘       │
│                │  Graph Query                         │
│     ┌──────────▼──────────┐                           │
│     │                     │                           │
│     │ Graph Database      │    Nebula Graph           │
│     │                     │                           │
│     └─────────────────────┘                           │
│                                                       │
│                                                       │
│                                                       │
└───────────────────────────────────────────────────────┘

代码

在本课时,我们不会讲解实际的应用的代码,他们的分布目录如下,如果大家感兴趣,欢迎访问代码仓库了解,如果有问题,可以在论坛、GitHub 里发起讨论。

.
├── README.md
├── src
│   ├── siwi                        # Siwi-API Backend
│   │   ├── app                     # Web Server, take HTTP requests and calls Bot API
│   │   └── bot                     # Bot API
│   │       ├── actions             # Take Intent, Slots, Query Knowledge Graph here
│   │       ├── bot                 # Entrypoint of the Bot API
│   │       ├── classifier          # Symentic Parsing, Intent Matching, Slot Filling
│   │       └── test                # Example Data Source as equivalent/mocked module
│   └── siwi_frontend               # Browser End
│       ├── README.md
│       ├── package.json
│       └── src
│           ├── App.vue             # Listening to user and pass Questions to Siwi-API
│           └── main.js
└── wsgi.py

Demo

To be prepared.

@wey-gu
Copy link
Author

wey-gu commented Sep 1, 2022

First_NebulaGraph_App.mp4
siwi-demo-0.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment