GPT-3.5 生成 Fabric Cypher

news2024/11/14 1:47:58

GPT-3.5 生成 Fabric Cypher

  • GPT-3.5 生成 Fabric Cypher
    • Fabric 架构图数据库节点设计
    • 图数据模型设计
    • Fabric 快速开始指南
      • 下载必要资源
      • 配置
    • 构建数据
      • 在`graph-node-1`节点构建股票图谱
      • 在`graph-node-2`节点构建高管图谱
    • GPT-3.5 生成 Fabric Cypher

Here’s the table of contents:

GPT-3.5 生成 Fabric Cypher

Fabric 架构图数据库节点设计

  在之前已经测试过 GPT4生成Cypher 和 GPT3.5生成Cypher 的能力,为了让生成Cypher的能力应用在更大规模的图数据查询,本次测试使用图数据库的Fabric架构。
测试用Fabric架构包含一个Fabric Node,两个Data Node
Fabric Node不存储任何数据,只负责请求的路由转发。
Data Node存储数据,负责响应数据查询请求。

案例 Fabric 架构

//导入 https://ongdb-contrib.github.io/graphene/app/ 查看可视化效果
{"data":{"nodes":[{"x":480,"y":504,"color":"#d19d7b","label":"用户","properties":[{"id":"a2b632be","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"a78b598d","isSelected":false,"isNode":true},{"x":719,"y":496,"color":"#f54e42","label":"Fabric Node","properties":[{"id":"aa887cb0","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"ac8263bb","isSelected":false,"isNode":true},{"x":988,"y":386,"color":"#56b6e6","label":"Data Node-1","properties":[{"id":"a9b1709f","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"a5a6c1a2","isSelected":false,"isNode":true},{"x":979,"y":617,"color":"#56b6e6","label":"Data Node-2","properties":[{"id":"a8b8689e","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"ae8ff082","isSelected":false,"isNode":true}],"edges":[{"startNodeId":"a78b598d","endNodeId":"ac8263bb","middlePointOffset":[18.5,15],"properties":[{"id":"a5944abd","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"请求","id":"a7b119b0","isSelected":false,"isEdge":true,"color":"#f54e42","pathStrokeDasharray":"2 5"},{"startNodeId":"ac8263bb","endNodeId":"a78b598d","middlePointOffset":[19.5,-15],"properties":[{"id":"aabff3b4","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"响应","id":"ac9212a6","isSelected":false,"isEdge":true,"color":"#56b6e6","pathStrokeDasharray":"2 5"},{"startNodeId":"ac8263bb","endNodeId":"a5a6c1a2","middlePointOffset":[37.5,31],"properties":[{"id":"a98616a8","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"路由Cypher","id":"ad91b79b","isSelected":false,"isEdge":true,"color":"#f54e42","pathStrokeDasharray":"15 5"},{"startNodeId":"ac8263bb","endNodeId":"ae8ff082","middlePointOffset":[40,-35.5],"properties":[{"id":"ab88619c","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"路由Cypher","id":"afbdf2af","isSelected":false,"isEdge":true,"color":"#f54e42","pathStrokeDasharray":"15 5"},{"startNodeId":"a5a6c1a2","endNodeId":"ac8263bb","middlePointOffset":[3,-3],"properties":[{"id":"a49517ac","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"响应查询","id":"ac879ab6","isSelected":false,"isEdge":true,"color":"#56b6e6","pathStrokeDasharray":"15 5"},{"startNodeId":"ae8ff082","endNodeId":"ac8263bb","middlePointOffset":[-3,-3.5],"properties":[{"id":"ada420a8","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"响应查询","id":"a481e3ac","isSelected":false,"isEdge":true,"color":"#56b6e6","pathStrokeDasharray":"15 5"}]}}

图数据模型设计

  基于Fabric架构设计图数据模型,Data Node-1负责保存股票图谱,Data Node-2负责保存高管图谱。
数据参考
graph-qabot-demo ,基于该样例数据源拆分为股票图谱和高管图谱。
红色部分表示股票图谱,蓝色部分表示高管图谱,股票图谱中同时存储高管任职于关系(股票图谱中的高管节点只保存跨图关联的md5即可)。

数据模型

//导入 https://ongdb-contrib.github.io/graphene/app/ 查看可视化效果
{"data":{"nodes":[{"x":593,"y":317,"color":"#e60000","label":"股票代码","properties":[{"id":"a2aeffb2","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"afb48f90","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"a1b949a0","isSelected":false,"isNode":true},{"x":658,"y":477,"color":"#e60000","label":"股票","properties":[{"id":"a4a8beb4","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a9bf77ad","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"a5844bbd","isSelected":false,"isNode":true},{"x":393,"y":401,"color":"#e60000","label":"股票名称","properties":[{"id":"a08f768e","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a09d0ab9","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"a78c159d","isSelected":false,"isNode":true},{"x":406,"y":508,"color":"#e60000","label":"地域","properties":[{"id":"a59d20a1","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a28d19bb","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"a49f8ebc","isSelected":false,"isNode":true},{"x":461,"y":590,"color":"#e60000","label":"行业","properties":[{"id":"aea1c5b2","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"aaa4b082","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"a381f688","isSelected":false,"isNode":true},{"x":694,"y":602,"color":"#e60000","label":"上市日期","properties":[{"id":"a284f1aa","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a0b444a4","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"ac81ffb4","isSelected":false,"isNode":true},{"x":814,"y":413,"color":"#236ce1","label":"高管","properties":[{"id":"a4b02384","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"a2ade2ad","isSelected":false,"isNode":true},{"x":870,"y":299,"color":"#236ce1","label":"性别","properties":[{"id":"ad9cab82","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a2aa0f86","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"af9a0f97","isSelected":false,"isNode":true},{"x":957,"y":480,"color":"#236ce1","label":"学历","properties":[{"id":"a090f498","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""},{"id":"a8a0e1b4","key":"value","type":"","defaultValue":"","limitMin":"","limitMax":"","isRequired":false,"isAutoGenerated":false,"isSystem":false,"description":"值"}],"id":"ad931890","isSelected":false,"isNode":true},{"x":981,"y":213,"color":"#236ce1","label":"性别_别名","properties":[{"id":"a3980888","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"id":"aa8b5199","isSelected":false,"isNode":true}],"edges":[{"startNodeId":"a2ade2ad","endNodeId":"a5844bbd","middlePointOffset":[0,0],"properties":[{"id":"adaf73aa","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"任职于","id":"a3bcec8c","isSelected":false,"isEdge":true,"color":"#d3b68d","pathStrokeDasharray":"2 5"},{"startNodeId":"a5844bbd","endNodeId":"a1b949a0","middlePointOffset":[0,0],"properties":[{"id":"a3bd83a0","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"股票代码","id":"ae88d4aa","isSelected":false,"isEdge":true,"color":"#E60000","pathStrokeDasharray":"none"},{"startNodeId":"a5844bbd","endNodeId":"a78c159d","middlePointOffset":[0,0],"properties":[{"id":"a08a7485","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"股票名称","id":"a5a68b93","isSelected":false,"isEdge":true,"color":"#E60000","pathStrokeDasharray":"none"},{"startNodeId":"a5844bbd","endNodeId":"a49f8ebc","middlePointOffset":[0,0],"properties":[{"id":"a79e37b3","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"地域","id":"acab7b92","isSelected":false,"isEdge":true,"color":"#E60000","pathStrokeDasharray":"none"},{"startNodeId":"a5844bbd","endNodeId":"a381f688","middlePointOffset":[0,0],"properties":[{"id":"af98c1a2","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"所属行业","id":"a9af63b6","isSelected":false,"isEdge":true,"color":"#E60000","pathStrokeDasharray":"none"},{"startNodeId":"a5844bbd","endNodeId":"ac81ffb4","middlePointOffset":[0,0],"properties":[{"id":"a5b64896","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"上市日期","id":"a0922fb7","isSelected":false,"isEdge":true,"color":"#E60000","pathStrokeDasharray":"none"},{"startNodeId":"a2ade2ad","endNodeId":"af9a0f97","middlePointOffset":[0,0],"properties":[{"id":"a19e1692","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"性别","id":"ada854ae","isSelected":false,"isEdge":true,"color":"#236CE1","pathStrokeDasharray":"none"},{"startNodeId":"a2ade2ad","endNodeId":"ad931890","middlePointOffset":[0,0],"properties":[{"id":"aeb6ae9e","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"学历","id":"acb3848a","isSelected":false,"isEdge":true,"color":"#236CE1","pathStrokeDasharray":"none"},{"startNodeId":"af9a0f97","endNodeId":"aa8b5199","middlePointOffset":[0,0],"properties":[{"id":"ab919ead","key":"id","type":"ID","defaultValue":"","limitMin":"","limitMax":"","isRequired":true,"isAutoGenerated":true,"isSystem":true,"description":""}],"label":"别名","id":"a398fe92","isSelected":false,"isEdge":true,"color":"#236CE1","pathStrokeDasharray":"none"}]}}

Fabric 快速开始指南

  本次测试案例运行在win10系统,可按照具体情况下载对应软件安装包。
在win10系统合适的地方创建ongdb\fabric目录(方便个人文件管理),用于安装服务节点。

下载必要资源

  1. 下载ONgDB
  2. 下载ONgDB Fabric
  3. 下载ONgDB Apoc

配置

  开始配置前,在ongdb\fabric目录下,创建graph-node-1graph-node-2fabric-node-1三个文件夹。
并将下载的ONgDB压缩文件解压后文件分别完整放置在三个目录下。
另外也需要将ONgDB Apoc组件放置graph-node-1graph-node-2根目录下的plugins目录。
保存好文件后在各个节点安装目录下修改conf\ongdb.conf配置,修改方式如下:

  • 修改graph-node-1配置
dbms.connector.bolt.listen_address=:7681
dbms.connector.http.listen_address=:7471
dbms.connector.https.listen_address=:7461
apoc.import.file.enabled=true
dbms.security.procedures.unrestricted=apoc.*
  • 修改graph-node-2配置
dbms.connector.bolt.listen_address=:7682
dbms.connector.http.listen_address=:7472
dbms.connector.https.listen_address=:7462
apoc.import.file.enabled=true
dbms.security.procedures.unrestricted=apoc.*
  • 修改fabric-node-1配置
dbms.connector.bolt.listen_address=:7281
dbms.connector.http.listen_address=:7171
dbms.connector.https.listen_address=:7161
dbms.security.procedures.unrestricted=c*

  修改好配置后,将ONgDB Fabric组件放置在fabric-node-1安装目录下的plugins文件夹,并在根目录下创建fabric\fabric.properties配置。
文件fabric.properties的配置如下:

c1=bolt://fabric:pssfabric@localhost:7681
c2=bolt://fabric:pssfabric@localhost:7682

  现在可以在各个节点根目录下,运行下面命令启动节点:

bin\ongdb.bat console

  启动节点后,分别在节点上创建以下用户账号。

编号用户名密码备注
graph-node-1ongdb123456HTTP访问页面:http://localhost:7471
graph-node-2ongdb123456HTTP访问页面:http://localhost:7472
fabric-node-1ongdb123456HTTP访问页面:http://localhost:7171
graph-node-1fabricpssfabric【只读】提供给Fabric服务使用
graph-node-2fabricpssfabric【只读】提供给Fabric服务使用

构建数据

graph-node-1节点构建股票图谱

  将graph-qabot-demostocks.csvmanagers.csv的文件放置在graph-node-1根目录下的import目录,并运行下面脚本构建数据。

CREATE CONSTRAINT ON (n:股票代码) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:股票) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:股票名称) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:地域) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:行业) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:上市日期) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:高管) ASSERT (n.md5) IS NODE KEY;

LOAD CSV WITH HEADERS FROM 'file:/stocks.csv' AS row
WITH row.ts_code AS fval,row.symbol AS tval
WHERE tval IS NOT NULL AND tval<>''
MERGE (f:股票 {value:fval})
MERGE (t:股票代码 {value:tval})
MERGE (f)-[:股票代码]->(t);

LOAD CSV WITH HEADERS FROM 'file:/stocks.csv' AS row
WITH row.ts_code AS fval,row.name AS tval
WHERE tval IS NOT NULL AND tval<>''
MERGE (f:股票 {value:fval})
MERGE (t:股票名称 {value:tval})
MERGE (f)-[:股票名称]->(t);

LOAD CSV WITH HEADERS FROM 'file:/stocks.csv' AS row
WITH row.ts_code AS fval,row.area AS tval
WHERE tval IS NOT NULL AND tval<>''
MERGE (f:股票 {value:fval})
MERGE (t:地域 {value:tval})
MERGE (f)-[:地域]->(t);

LOAD CSV WITH HEADERS FROM 'file:/stocks.csv' AS row
WITH row.ts_code AS fval,row.industry AS tval
WHERE tval IS NOT NULL AND tval<>''
MERGE (f:股票 {value:fval})
MERGE (t:行业 {value:tval})
MERGE (f)-[:所属行业]->(t);

LOAD CSV WITH HEADERS FROM 'file:/stocks.csv' AS row
WITH row.ts_code AS fval,row.list_date AS tval
WHERE tval IS NOT NULL AND tval<>''
MERGE (f:股票 {value:fval})
MERGE (t:上市日期 {value:TOINTEGER(tval)})
MERGE (f)-[:上市日期]->(t);

LOAD CSV WITH HEADERS FROM 'file:/managers.csv' AS row
WITH row,apoc.util.md5([row.name,row.gender,row.birthday]) AS fval,row.ts_code AS tval
WHERE tval IS NOT NULL AND tval<>'' AND fval IS NOT NULL AND fval<>'' AND (row.end_date IS NULL OR row.end_date='')
MERGE (f:高管 {md5:fval})
MERGE (t:股票 {value:tval})
MERGE (f)-[:任职于]->(t);

graph-node-2节点构建高管图谱

  将graph-qabot-demomanagers.csv文件放置在graph-node-2根目录下的import目录,并运行下面脚本构建数据。

CREATE CONSTRAINT ON (n:高管) ASSERT (n.md5) IS NODE KEY;
CREATE INDEX ON :高管(value);
CREATE CONSTRAINT ON (n:性别) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:学历) ASSERT (n.value) IS NODE KEY;
CREATE CONSTRAINT ON (n:性别_别名) ASSERT (n.value) IS NODE KEY;

LOAD CSV WITH HEADERS FROM 'file:/managers.csv' AS row
WITH row,apoc.util.md5([row.name,row.gender,row.birthday]) AS fval,row.ts_code AS tval
WHERE tval IS NOT NULL AND tval<>'' AND fval IS NOT NULL AND fval<>'' AND (row.end_date IS NULL OR row.end_date='')
MERGE (f:高管 {md5:fval}) SET f+={value:row.name}
MERGE (t:股票 {value:tval})
MERGE (f)-[:任职于]->(t);

LOAD CSV WITH HEADERS FROM 'file:/managers.csv' AS row
WITH row,apoc.util.md5([row.name,row.gender,row.birthday]) AS fval,row.gender AS tval
WHERE tval IS NOT NULL AND tval<>'' AND fval IS NOT NULL AND fval<>'' AND (row.end_date IS NULL OR row.end_date='')
MERGE (f:高管 {md5:fval}) SET f+={value:row.name}
MERGE (t:性别 {value:tval})
MERGE (f)-[:性别]->(t);

LOAD CSV WITH HEADERS FROM 'file:/managers.csv' AS row
WITH row,apoc.util.md5([row.name,row.gender,row.birthday]) AS fval,row.edu AS tval
WHERE tval IS NOT NULL AND tval<>'' AND fval IS NOT NULL AND fval<>'' AND (row.end_date IS NULL OR row.end_date='')
MERGE (f:高管 {md5:fval}) SET f+={value:row.name}
MERGE (t:学历 {value:tval})
MERGE (f)-[:学历]->(t);

WITH ['女性','女'] AS list
UNWIND list AS wd
WITH wd
MATCH (n:性别) WHERE n.value='F' WITH n,wd
MERGE (t:性别_别名 {value:wd})
MERGE (n)-[:别名]->(t);

WITH ['男性','男'] AS list
UNWIND list AS wd
WITH wd
MATCH (n:性别) WHERE n.value='M' WITH n,wd
MERGE (t:性别_别名 {value:wd})
MERGE (n)-[:别名]->(t);

GPT-3.5 生成 Fabric Cypher

  使用 GPT-3.5 生成 Fabric Cypher 时,需要注意 Fabric Cypher 样例不要使用转义符,GPT 生成转义符不太友好(会丢弃转义符,添加任何其它Prompt都没有用)。
如果丢弃了转义符,生成的 Fabric Cypher 直接执行会报错。
除了转义符需要注意外,其它样例覆盖的情况,Fabric Cypher 生成效果都比较好。
下面可以看一下,一些 LLM Generate Cypher | 使用大语言模型生成Cypher 测试结果:

import requests
import Levenshtein


# 使用HTTP封装的GPT-3.5接口,请根据具体接口情况替换
def gpt3_5(msg):
    # 设置请求头
    headers = {"Content-type": "application/json"}
    url = 'http://localhost:8080/cgpt-api/push_question'
    data = {
        "ask_str": msg,
    }

    response = requests.post(url, json=data, headers=headers)
    return response.json()['data']['content']


def example_list():
    ex_list = [{
        'qa': '女性高管任职的上市公司有多少家?',
        'cypher': '''
            CALL c2(
	            '
                MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
                WHERE xb.value=$xb
                RETURN COLLECT(gg.md5) AS gg_md5;
                ',
            {xb:'女性'}
            ) YIELD value WITH value.gg_md5 AS gg_md5
            CALL c1(
	            '
                MATCH (stk:股票)<-[:任职于]-(gg:高管)
                WHERE gg.md5 IN $gg_md5
                RETURN COUNT(DISTINCT stk) AS count;
                ',
                {gg_md5:gg_md5}
            ) YIELD value RETURN value.count AS count;
        '''
    }, {
        'qa': '上市公司女性高管比例是多少?',
        'cypher': '''
            CALL c2(
	            '
                MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
                WHERE xb.value=$xb
                RETURN COUNT(gg) AS v_ct;
                ',
                {xb:'女性'}
            ) YIELD value WITH value.v_ct AS v_ct
            CALL c2(
	            '
                MATCH (gg:高管)
                WITH COUNT(gg) AS t_ct
                RETURN TOFLOAT(apoc.number.exact.div(TOSTRING($v_ct),TOSTRING(t_ct),3,$mk_half)) AS val
                ',
                {v_ct:v_ct,mk_half:'HALF_DOWN'}
            ) YIELD value RETURN value.val AS val;
        '''
    }, {
        'qa': '北京的上市公司中有多少位女性高管?',
        'cypher': '''
            CALL c2(
	            '
                MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
                WHERE xb.value=$xb
                RETURN COLLECT(gg.md5) AS gg_md5;
                ',
                {xb:'女性'}
            ) YIELD value WITH value.gg_md5 AS gg_md5
            CALL c1(
	            '
                MATCH (dy:地域)<-[:地域]-(stk:股票)<-[:任职于]-(gg:高管)
                WHERE gg.md5 IN $gg_md5 AND dy.value=$dy
                RETURN COUNT(DISTINCT stk) AS count;
                ',
                {dy:'北京',gg_md5:gg_md5}
            ) YIELD value RETURN value.count AS count;
        '''
    }, {
        'qa': '软件服务行业硕士学历的女性高管有多少位?',
        'cypher': '''
            CALL c2(
	            '
                MATCH (xl:学历)<-[:学历]-(gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
                WHERE xb.value=$xb AND xl.value=$xl
                RETURN COLLECT(gg.md5) AS gg_md5;
                ',
                {xb:'女性',xl:'硕士'}
            ) YIELD value WITH value.gg_md5 AS gg_md5
            CALL c1(
	            '
                MATCH (hy:行业)<-[:所属行业]-(stk:股票)<-[:任职于]-(gg:高管)
                WHERE gg.md5 IN $gg_md5 AND hy.value=$hy
                RETURN COUNT(DISTINCT stk) AS count;
                ',
                {hy:'软件服务',gg_md5:gg_md5}
            ) YIELD value RETURN value.count AS count;
        '''
    }, {
        'qa': '在海南由博士学历的男性高管任职的上市公司,都属于哪些行业?',
        'cypher': '''
            CALL c2(
	            '
                MATCH (xl:学历)<-[:学历]-(gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
                WHERE xb.value=$xb AND xl.value=$xl
                RETURN COLLECT(gg.md5) AS gg_md5;
                ',
                {xb:'男性',xl:'博士'}
            ) YIELD value WITH value.gg_md5 AS gg_md5
            CALL c1(
	            '
                MATCH (hy:行业)<-[:所属行业]-(stk:股票)<-[:任职于]-(gg:高管),
                	  (dy:地域)<-[:地域]-(stk)
                WHERE gg.md5 IN $gg_md5 AND dy.value=$dy
                RETURN DISTINCT hy.value AS hy;
                ',
                {dy:'海南',gg_md5:gg_md5}
            ) YIELD value RETURN value.hy AS hy;
        '''
    }]
    return ex_list


def examples(ask):
    examples_str = ''
    examples_list = []
    for index, map in enumerate(example_list()):
        qa = map['qa']
        cypher = map['cypher']
        dis = Levenshtein.distance(ask, qa)
        examples_list.append({'qa': qa, 'cypher': cypher, 'dis': dis})

    sorted_list = sorted(examples_list, key=lambda map: map['dis'])

    for map in sorted_list:
        qa = map['qa']
        cypher = map['cypher']
        ex = f'''
          # {qa}
          {cypher}
          '''
        # `prompt(ask)` Prompt Length 300
        if len(examples_str + ex) + 300 <= 2048:
            examples_str += ex

    return examples_str


def prompt(ask):
    return f"""
        您是一名助手,能够根据示例Cypher查询生成Cypher查询。
        示例Cypher查询有:\n {examples(ask)} \n
        除了Cypher查询之外,不要回复任何解释或任何其他信息。
        您永远不要为你的不准确回复感到抱歉,并严格根据提供的cypher示例生成cypher语句。
        不要提供任何无法从Cypher示例中推断出的Cypher语句。
        当由于缺少对话上下文而无法推断密码语句时,通知用户,并说明缺少的上下文是什么。
        现在请为这个查询生成Cypher:
        # {ask}
        """


if __name__ == '__main__':
    # 输入问题
    ask = '在北京由硕士学历的女性高管任职的上市公司,都属于哪些行业?'

    # This model's maximum context length is 4097 tokens.
    # However, you requested 4364 tokens (2316 in the messages, 2048 in the completion).
    # Please reduce the length of the messages or completion.
    print(gpt3_5(prompt(ask)))


# Test Result:

# QA:你好
# RE-GPT3.5(正确生成):无法根据示例Cypher查询推断出生成Cypher查询的特定上下文。请提供更具体的问题或信息。

# QA:男性高管任职的上市公司有多少家?
# RE-GPT3.5(正确生成):
#     CALL c2(
#         '
#         MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
#         WHERE xb.value=$xb
#         RETURN COLLECT(gg.md5) AS gg_md5;
#         ',
#         {xb:'男性'}
#     ) YIELD value WITH value.gg_md5 AS gg_md5
#     CALL c1(
#         '
#         MATCH (stk:股票)<-[:任职于]-(gg:高管)
#         WHERE gg.md5 IN $gg_md5
#         RETURN COUNT(DISTINCT stk) AS count;
#         ',
#         {gg_md5:gg_md5}
#     ) YIELD value RETURN value.count AS count;


# QA:上市公司男性高管比例是多少?
# RE-GPT3.5(正确生成):
#      CALL c2(
#          '
#          MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
#          WHERE xb.value=$xb
#          RETURN COUNT(gg) AS v_ct;
#          ',
#          {xb:'男性'}
#      ) YIELD value WITH value.v_ct AS v_ct
#      CALL c2(
#          '
#          MATCH (gg:高管)
#          WITH COUNT(gg) AS t_ct
#          RETURN TOFLOAT(apoc.number.exact.div(TOSTRING($v_ct),TOSTRING(t_ct),3,$mk_half)) AS val
#          ',
#          {v_ct:v_ct,mk_half:'HALF_DOWN'}
#      ) YIELD value RETURN value.val AS val;


# QA:上海的上市公司中有多少位男性高管?
# RE-GPT3.5(正确生成):
#     CALL c2(
#         '
#         MATCH (gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
#         WHERE xb.value=$xb
#         RETURN COLLECT(gg.md5) AS gg_md5;
#         ',
#         {xb:'男性'}
#     ) YIELD value WITH value.gg_md5 AS gg_md5
#     CALL c1(
#         '
#         MATCH (dy:地域)<-[:地域]-(stk:股票)<-[:任职于]-(gg:高管)
#         WHERE gg.md5 IN $gg_md5 AND dy.value=$dy
#         RETURN COUNT(DISTINCT stk) AS count;
#         ',
#         {dy:'上海',gg_md5:gg_md5}
#     ) YIELD value RETURN value.count AS count;


# QA:水泥行业博士学历的男性高管有多少位?
# RE-GPT3.5:
#     CALL c2(
#         '
#         MATCH (xl:学历)<-[:学历]-(gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
#         WHERE xb.value=$xb AND xl.value=$xl
#         RETURN COLLECT(gg.md5) AS gg_md5;
#         ',
#         {xb:'男性',xl:'博士'}
#     ) YIELD value WITH value.gg_md5 AS gg_md5
#     CALL c1(
#         '
#         MATCH (hy:行业)<-[:所属行业]-(stk:股票)<-[:任职于]-(gg:高管)
#         WHERE gg.md5 IN $gg_md5 AND hy.value=$hy
#         RETURN COUNT(DISTINCT gg) AS count;
#         ',
#         {hy:'水泥',gg_md5:gg_md5}
#     ) YIELD value RETURN value.count AS count;


# QA:在北京由硕士学历的女性高管任职的上市公司,都属于哪些行业?
# RE-GPT3.5:
#     CALL c2(
#         '
#         MATCH (xl:学历)<-[:学历]-(gg:高管)-[:性别]->(:性别)-[:别名]->(xb:性别_别名)
#         WHERE xb.value=$xb AND xl.value=$xl
#         RETURN COLLECT(gg.md5) AS gg_md5;
#         ',
#         {xb:'女性',xl:'硕士'}
#     ) YIELD value WITH value.gg_md5 AS gg_md5
#     CALL c1(
#         '
#         MATCH (hy:行业)<-[:所属行业]-(stk:股票)<-[:任职于]-(gg:高管),
#               (dy:地域)<-[:地域]-(stk)
#         WHERE gg.md5 IN $gg_md5 AND dy.value=$dy
#         RETURN DISTINCT hy.value AS hy;
#         ',
#         {dy:'北京',gg_md5:gg_md5}
#     ) YIELD value RETURN value.hy AS hy;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/464969.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Java】『蓝桥杯』10道编程题及答案(四)

系列文章 【Java】『蓝桥杯』10道编程题及答案&#xff08;一&#xff09; 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/130223115 【Java】『蓝桥杯』10道编程题及答案&#xff08;二&#xff09; 本文链接&#xff1a;https://blog.csdn.net/y…

五种PDF转Word免费的软件!(附带转换效果对比!)

本文将介绍种PDF转Word免费软件&#xff0c;让您快速将PDF文件转换为可编辑的Word文件。 随着现代科学技术的飞速发展&#xff0c;PDF格式已经成为我们工作和生活中常用的文档格式之一。PDF文件的格式是固定的&#xff0c;不容易被篡改&#xff0c;因此经常被用来制作各种关键文…

1691_python学习笔记之week3_递归

全部学习汇总&#xff1a; GreyZhang/python_basic: My learning notes about python. (github.com) 最初接触递归的时候觉得这个有点不好理解&#xff0c;怎么能够有这种思维方式&#xff1f;这完全不同于之前自己所能够了解的那种一加一等于二的方式。相对于能够看得见的那种…

企业采购都在用什么系统管理采购流程?ERP够用了吗?

ERP&#xff08;企业资源计划&#xff09;和采购管理系统都是企业信息化系统的一部分&#xff0c;它们在企业中扮演着不同的角色。下面是它们之间的区别&#xff1a;区别 功能不同&#xff1a;ERP系统是一个综合性的企业管理系统&#xff0c;覆盖范围广&#xff0c;包括财务、人…

Android12 material you 动态配色

动态配色功能是 Material You 设计的核心 一、完整的动态配色流程包括四个步骤&#xff0c;如下所示&#xff1a; 用户通过 OEM 选择器更改壁纸或主题。AOSP 逻辑会自动从所选壁纸中提取单一源颜色。将源颜色扩展到颜色API&#xff0c;AOSP 将单一源颜色扩展为 5 &#xff0c;…

JS实现拼音(字母)匹配(搜索)汉字(姓名)

这就是个模糊查询&#xff0c;我们平常做的都是直接输入汉字去把对应的值过滤出来&#xff0c;但我还真是第一次通过拼音去查询&#xff08;当然不只是拼音&#xff0c;汉字也是可以的&#xff09;&#xff0c;以前还真没注意这个。唉&#xff0c;这可咋搞&#xff0c;我怎么知…

python:面向对象编程(知识点+代码)

文章目录 一、类和对象1、对象属性的默认值设置2、对象属性的添加、修改与删除3 、类属性 二、类的继承 引言&#xff1a;面向对象编程时一门编程语言重要的功能&#xff0c;我们之前所学的 c&#xff0c;java都为面向对象编程语言&#xff0c;这里给大家拓展一下&#xff0c;…

getaddrinfo调用crash 的debug过程

前两天&#xff0c;产线报一例crash问题。拿到core文件后&#xff0c;栈展开信息&#xff1a; (gdb) bt #0 0x00007f64a3651aff in raise () from /lib64/libc.so.6 #1 0x00007f64a3624ea5 in abort () from /lib64/libc.so.6 #2 0x00007f64a3694097 in __libc_message () …

【论文阅读-ICSE2023】预训练目标对代码相关任务的影响

目录 简介有哪些预训练预训练的影响回答RQ1回答RQ2 总结 简介 Title: Automating Code-Related Tasks Through Transformers: The Impact of Pre-training1 Author: Rosalia Tufano, Luca Pascarella, Gabriele Bavota Published: ICSE2023 Abstract: 尽管现在很多研究能够证…

OSS的STS模式授权案例

OSS的STS模式授权 某云提供的权限管理系统主要包含两部分&#xff0c;RAM&#xff08;资源访问管理&#xff09;和STS&#xff08;安全认证服务&#xff09;&#xff0c;以满足不暴露主账号AK\SK的情况下安全的授权别人访问的需求&#xff0c;STS提供的是一种临时访问授权。通…

XSS知识总结

XSS基础 跨站脚本&#xff08;英语&#xff1a;Cross-site scripting&#xff0c;通常简称为&#xff1a;XSS&#xff09;是一种网站应用程序的安全漏洞攻击&#xff0c;是代码注入的一种。它允许恶意用户将代码注入到网页上&#xff0c;其他用户在观看网页时就会受到影响。这…

楼层滚动效果(超级简单,易懂)

系列文章目录 文章目录 系列文章目录一、楼层滚动效果图如下1. 下图是纯Css实现的楼层滚动2.通过Js优化后的楼层滚动如下图(&#x1f339;&#x1f339;) 二、楼层滚动&#xff08;Css实现&#xff09;1.滚动原理2.代码如下 三、楼层滚动&#xff08;JsCss优化后的楼层滚动&…

FreeRTOS学习笔记(二)——内核机制

文章目录 0x01 临界段Cortex-M内核快速关中断指令关中断开中断进入和退出临界段的宏进入临界段退出临界段临界段代码应用 0x02 空闲任务与阻塞延时的实现空闲任务的创建实现阻塞延时阻塞等待总结 0x03 多优先级通用方法&#xff1a;taskRECORD_READY_PRIORITY()taskSELECT_HIGH…

微服务学习——服务异步通讯

实用篇-RabbitMQ 初识MQ 同步调用的问题 微服务间基于Feign的调用就属于同步方式&#xff0c;存在一些问题。 耦合度高 每次加入新的需求&#xff0c;都要修改原来的代码性能下降 调用者需要等待服务提供者响应&#xff0c;如果调用链过长则响应时间等于每次调用的时间之和…

【数据库】事务的隔离级别以及实现原理

文章目录 前言一、事务什么是事务&#xff1f;事务的四大特性分别是 二、事务并发存在的问题脏读可重复读不可重复读幻读 三、以MYSQL数据库来分析四种隔离级别第一种隔离级别&#xff1a;Read uncommitted(读未提交)第二种隔离级别&#xff1a;Read committed(读提交)第三种隔…

此环境变量太大,此对话框允许将值设置为最长 2047 个字符

背景: 自己是一个计算机爱好者&#xff0c;又大体精通计算机各种编程语言&#xff0c;所以电脑上安装的各种编译或者编程软件比较多&#xff0c;有一天需要安装一个多版本的python环境&#xff0c;发现无法添加环境变量&#xff0c;于是乎&#xff0c;自己去查看原因并解决这个…

C语言 sizeof, size_t, strlen

C语言 sizeof, size_t, strlen 文章目录 C语言 sizeof, size_t, strlen一. sizeof1.1 返回结构体长度 二. size_t三. sizeof 和 strlen 一. sizeof 返回一个结构体或者类型所占的内存字节数 1.1 返回结构体长度 这里我编写了2个结构体&#xff0c;区别在于数组问题 #include …

智慧园区gis三维数字孪生可视化平台辅助管理者提升安全管控效力

随着信息技术的发展,3D可视化已经成为交互式管理的主流。智慧3D可视化交互管理平台能够帮助企业实现高效、灵活的管理。 1&#xff1a;智慧3D可视化交互管理平台的基本特性 智慧3D可视化交互管理平台能够实时显示工厂中生产车间的设备和生产线的运行状态&#xff0c;并通过交互…

火焰图怎么看

性能平台 通过该平台可以使用pprof采集线上的服务指标数据生成CPU占用数据并自动绘制出火焰图&#xff0c;Top&#xff0c;以及函数调用图等。 1.1 火焰图 火焰图的深度代表了函数的调用栈&#xff0c;火焰图最深处就是正在执行的函数&#xff0c;上方的都是它的父函数&#x…

C++线程的简单学习及了解

此篇文章只是线程的简单了解。 文章目录 前言一、线程的优缺点二、C线程库 1.thread类的简单介绍2.线程函数参数总结 前言 什么是线程&#xff1f; 在一个程序里的一个执行路线就叫做线程&#xff08;thread&#xff09;。更准确的定义是&#xff1a;线程是“一个进程内部的控…