ChatGPT链接本地数据源实战测试

发布时间 2023-07-17 08:39:29作者: 古道轻风

本文探究ChatGpt等AI机器人能否帮助老板快速的做数据分析?用自然语言同老板进行沟通,满足老板的所有数据分析的诉求?

一、背景

设想这样一个场景:你是某贸易公司的老板,公司所有的日常运转数据都在私域的进销存系统,包括客户、供应商、销售、库存、进货、商品等,你每天需要关注公司运营情况,并且希望商业分析师对当前运营数据给出分析和建议,对公司的运营策略进行及时调整。ChatGpt等AI机器人能否帮助老板快速的做数据分析?用自然语言同老板进行沟通,满足老板的所有数据分析的诉求?

二、工作原理

众所周知,ChatGpt的数据截止只至2021年9月且无法直接联同互联网和局域网(虽然plus账户可以使用插件来扩展chatgpt能力比如联网,读取本地文档等,但输入和输出tokens有限制且月20刀的费用让人望而却步),那我们该如何把chatgpt能力同本地能力结合呢?
为了满足上述的需求,利用LangChain(语言模型驱动的应用程序的开发框架,具体可问ChatGpt)的数据感知能力(将语言模型同其他数据源链接)和代理能力(使语言模型能够与其环境进行交互),让老板通过自然语言同引擎进行交互,用老板思维提问,返回给老板各种维度的数据甚至对应的可视化表达。(下图是网上拿来的LangChain链接本地文件和语言模型的示意图,大致可以说明本次实验的原理)。

图片

三、构造数据模型和测试数据

利用ChatGpt给出进销存系统的数据模型和测试数据,模拟某贸易公司的日常运营数据。

CREATE TABLE Product (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(255),
    ProductDescription TEXT,
    CostPrice DECIMAL(10,2),
    SalePrice DECIMAL(10,2)
);

CREATE TABLE Supplier (
    SupplierID INT PRIMARY KEY,
    SupplierName VARCHAR(255),
    ContactPerson VARCHAR(255),
    ContactPhone VARCHAR(20),
    Address TEXT
);

CREATE TABLE Customer (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(255),
    ContactPerson VARCHAR(255),
    ContactPhone VARCHAR(20),
    Address TEXT
);

CREATE TABLE Inventory (
    InventoryID INT PRIMARY KEY,
    ProductID INT,
    Quantity INT,
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

CREATE TABLE PurchaseOrder (
    PurchaseOrderID INT PRIMARY KEY,
    SupplierID INT,
    OrderDate DATE,
    FOREIGN KEY (SupplierID) REFERENCES Supplier(SupplierID)
);

CREATE TABLE SalesOrder (
    SalesOrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);

CREATE TABLE PurchaseOrderDetail (
    PurchaseOrderDetailID INT PRIMARY KEY,
    PurchaseOrderID INT,
    ProductID INT,
    Quantity INT,
    Price DECIMAL(10,2),
    FOREIGN KEY (PurchaseOrderID) REFERENCES PurchaseOrder(PurchaseOrderID),
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

CREATE TABLE SalesOrderDetail (
    SalesOrderDetailID INT PRIMARY KEY,
    SalesOrderID INT,
    ProductID INT,
    Quantity INT,
    Price DECIMAL(10,2),
    FOREIGN KEY (SalesOrderID) REFERENCES SalesOrder(SalesOrderID),
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);
利用ChatGpt自动构造测试数据,检查了主键外键均能一一对应,给ChatGpt点赞。
-- Insert into Product
INSERT INTO Product (ProductID, ProductName, ProductDescription, CostPrice, SalePrice) VALUES
(1, 'Product1', 'Description1', 10.00, 20.00),
(2, 'Product2', 'Description2', 15.00, 25.00),
(3, 'Product3', 'Description3', 20.00, 30.00),
(4, 'Product4', 'Description4', 25.00, 35.00),
(5, 'Product5', 'Description5', 30.00, 40.00),
(6, 'Product6', 'Description6', 35.00, 45.00),
(7, 'Product7', 'Description7', 40.00, 50.00),
(8, 'Product8', 'Description8', 45.00, 55.00),
(9, 'Product9', 'Description9', 50.00, 60.00),
(10, 'Product10', 'Description10', 55.00, 65.00);

-- Insert into Supplier
INSERT INTO Supplier (SupplierID, SupplierName, ContactPerson, ContactPhone, Address) VALUES
(1, 'Supplier1', 'Contact1', '1234567890', 'Address1'),
(2, 'Supplier2', 'Contact2', '1234567890', 'Address2'),
(3, 'Supplier3', 'Contact3', '1234567890', 'Address3'),
(4, 'Supplier4', 'Contact4', '1234567890', 'Address4'),
(5, 'Supplier5', 'Contact5', '1234567890', 'Address5'),
(6, 'Supplier6', 'Contact6', '1234567890', 'Address6'),
(7, 'Supplier7', 'Contact7', '1234567890', 'Address7'),
(8, 'Supplier8', 'Contact8', '1234567890', 'Address8'),
(9, 'Supplier9', 'Contact9', '1234567890', 'Address9'),
(10, 'Supplier10', 'Contact10', '1234567890', 'Address10');

-- Insert into Customer
INSERT INTO Customer (CustomerID, CustomerName, ContactPerson, ContactPhone, Address) VALUES
(1, 'Customer1', 'Contact1', '1234567890', 'Address1'),
(2, 'Customer2', 'Contact2', '1234567890', 'Address2'),
(3, 'Customer3', 'Contact3', '1234567890', 'Address3'),
(4, 'Customer4', 'Contact4', '1234567890', 'Address4'),
(5, 'Customer5', 'Contact5', '1234567890', 'Address5'),
(6, 'Customer6', 'Contact6', '1234567890', 'Address6'),
(7, 'Customer7', 'Contact7', '1234567890', 'Address7'),
(8, 'Customer8', 'Contact8', '1234567890', 'Address8'),
(9, 'Customer9', 'Contact9', '1234567890', 'Address9'),
(10, 'Customer10', 'Contact10', '1234567890', 'Address10');

-- Insert into Inventory
INSERT INTO Inventory (InventoryID, ProductID, Quantity) VALUES
(1, 1, 100),
(2, 2, 200),
(3, 3, 300),
(4, 4, 400),
(5, 5, 500),
(6, 6, 600),
(7, 7, 700),
(8, 8, 800),
(9, 9, 900),
(10, 10, 1000);

-- Insert into PurchaseOrder
INSERT INTO PurchaseOrder (PurchaseOrderID, SupplierID, OrderDate) VALUES
(1, 1, '2023-01-01'),
(2, 2, '2023-02-01'),
(3, 3, '2023-03-01'),
(4, 4, '2023-04-01'),
(5, 5, '2023-05-01'),
(6, 6, '2023-06-01'),
(7, 7, '2023-07-01'),
(8, 8, '2023-08-01'),
(9, 9, '2023-09-01'),
(10, 10, '2023-10-01');

-- Insert into SalesOrder
INSERT INTO SalesOrder (SalesOrderID, CustomerID, OrderDate) VALUES
(1, 1, '2023-01-01'),
(2, 2, '2023-02-01'),
(3, 3, '2023-03-01'),
(4, 4, '2023-04-01'),
(5, 5, '2023-05-01'),
(6, 6, '2023-06-01'),
(7, 7, '2023-07-01'),
(8, 8, '2023-08-01'),
(9, 9, '2023-09-01'),
(10, 10, '2023-10-01');

-- Insert into PurchaseOrderDetail
INSERT INTO PurchaseOrderDetail (PurchaseOrderDetailID, PurchaseOrderID, ProductID, Quantity, Price) VALUES
(1, 1, 1, 10, 10.00),
(2, 2, 2, 20, 15.00),
(3, 3, 3, 30, 20.00),
(4, 4, 4, 40, 25.00),
(5, 5, 5, 50, 30.00),
(6, 6, 6, 60, 35.00),
(7, 7, 7, 70, 40.00),
(8, 8, 8, 80, 45.00),
(9, 9, 9, 90, 50.00),
(10, 10, 10, 100, 55.00);

-- Insert into SalesOrderDetail
INSERT INTO SalesOrderDetail (SalesOrderDetailID, SalesOrderID, ProductID, Quantity, Price) VALUES
(1, 1, 1, 10, 20.00),
(2, 2, 2, 20, 25.00),
(3, 3, 3, 30, 30.00),
(4, 4, 4, 40, 35.00),
(5, 5, 5, 50, 40.00),
(6, 6, 6, 60, 45.00),
(7, 7, 7, 70, 50.00),
(8, 8, 8, 80, 55.00),
(9, 9, 9, 90, 60.00),
(10, 10, 10, 100, 65.00);

 

四、测试环境搭建

1、数据库采用本地搭建的mysql 8.0 ;
2、大语言模型采用OpenAI,GPT3 模型;
3、LangChain 0.0.191;
3、数据用ChatGpt自动构造的测试数据,故不存在数据安全和合规问题;

 

五、实验测试过程

本次实验测试过程取老板在日常中比较常用的场景进行测试,比如查供应商、商品、库存、销量、营收和利润等。

场景1 :

输入:我们总共有多少个供应商,给我们供应了多少种商品?

生成sql:

SELECT COUNT(DISTINCT SupplierID) AS 'Number of Suppliers', COUNT(DISTINCT ProductID) AS 'Number of Products' FROM Supplier, Product

输出:Final Answer: There are 10 suppliers and 10 products.

结论:答案正确✅

引擎分析的过程如下:

图片

场景2 :

输入:每一种商品的库存有多少?
生成的sql:
SELECT ProductID, Quantity FROM Inventory

输出:Final Answer: The quantity of each product in the Inventory table is: ProductID 1 has 100, ProductID 2 has 200, ProductID 3 has 300, ProductID 4 has 400, ProductID 5 has 500, ProductID 6 has 600, ProductID 7 has 700, ProductID 8 has 800, ProductID 9 has 900, ProductID 10 has 1000.

结论:答案正确✅
引擎分析的过程如下:

      图片

场景3 :

输入:按月给出所有商品的销量和库存?

生成的sql:  
SELECT MONTH(sod.SalesOrderID) AS Month, p.ProductName, SUM(sod.Quantity) AS Sales, i.Quantity AS Inventory FROM SalesOrderDetail sod INNERJOIN Product p ON sod.ProductID = p.ProductID INNER JOIN Inventory i ON p.ProductID = i.ProductID GROUP BY Month, p.ProductName, i.InventoryID ORDER BY Month ASC LIMIT 10 

输出:Final Answer: The sales and inventory for each product by month are: Product1 (10 sales, 100 inventory), Product2 (20 sales, 200 inventory), Product3 (30 sales, 300 inventory), Product4 (40 sales, 400 inventory), Product5 (50 sales, 500 inventory), Product6 (60 sales, 600 inventory), Product7 (70 sales, 700 inventory), Product8 (80 sales, 800 inventory), Product9 (90 sales, 900 inventory), Product10 (100 sales, 1000 inventory).   

结论:月度日期不对,部分正确

引擎分析的过程如下:                   

图片

                 

图片

            

场景4 :

输入:给出每个月的销量和销售额?

生成的sql:
SELECT MONTH(OrderDate) AS Month, SUM(Quantity) AS SalesQuantity, SUM(Quantity * Price) AS SalesAmount FROM SalesOrder JOIN SalesOrderDetail ON SalesOrder.SalesOrderID = SalesOrderDetail.SalesOrderID GROUP BY MONTH(OrderDate) ORDER BY Month ASC LIMIT 10
 

结论:答案正确✅

引擎分析的过程如下:                   

图片

                                   

场景5 :

输入:给出每个月的销量,营收和利润?
生成的sql:
SELECT MONTH(OrderDate) AS Month, SUM(Quantity) AS Sales, SUM(Quantity * Price) AS Revenue, SUM(Quantity * Price) - SUM(Quantity * CostPrice) AS Profit FROM SalesOrder JOIN SalesOrderDetail ON SalesOrder.SalesOrderID = SalesOrderDetail.SalesOrderID JOIN ProductON SalesOrderDetail.ProductID = Product.ProductID GROUP BY MONTH(OrderDate) ORDER BY Month ASC LIMIT 10 
输出:Final Answer: The sales, revenue, and profit for each month are: Month 1: Sales 10, Revenue 200.00, Profit 100.00; Month 2: Sales 20, Revenue 500.00, Profit 200.00; Month 3: Sales 30, Revenue 900.00, Profit 300.00; Month 4: Sales 40, Revenue 1400.00, Profit 400.00; Month 5: Sales 50, Revenue 2000.00, Profit 500.00; Month 6: Sales 60, Revenue 2700.00, Profit 600.00; Month 7: Sales 70, Revenue 3500.00, Profit 700.00; Month 8: Sales 80, Revenue 4400.00, Profit 800.00; Month 9: Sales 90, Revenue 5400.00, Profit 900.00; Month 10: Sales 100, Revenue 6500.00, Profit 1000.00.
结论:答案正确✅

引擎分析过程:

图片
                 

图片

                               

六、总结

LangChain能够链接本地数据源到大语言模型,并且利用大语言模型完成指定任务,准确率较高,通过引擎的分析思考过程来看,ChatGpt的思维逻辑类似人类,也会写错sql报错,然后不停重试直至得到最终结果,但仍有一些风险和改进点:
1、数据安全和合规风险,这里直接链接了本地数据库,我这里是构造的测试数据,真正的业务场景不需要,可以等通义千问的开放。
2、由于ChatGptd的API的输入输出有4100个Tokens的限制,针对复杂任务,容易超过限制被终端,因此不能一次输入过多的问题,这个可以通过prompt template来设定一些常用模板来解决。
3、这次实验最终的输出是英文,这个可以设定引擎的语言参数以及输出结果进行结构化返回,前端页面实现可视化图形展现,这次主要目的为了测试SQL生成能力,展示就不赘述。
翻译|慈修