AI 驱动 TDSQL-C Serverless 数据库技术实战营——学习实践全流程记录
实践出真知,我们动手操作一下还是非常有价值的呢,希望本次的体验对大家能有一定的价值。现在活动还在进行时。可以去搞一搞。实战营地址:https://cloud.tencent.com/developer/learning/camp/25
前言
TDSQL这个值得说一说的国产的腾讯自研的新一代关系型数据库,仅凭着纯国产原生的名头就可以让我必须的深入学习一下,而且还有一个训练营就非常的NICE,官方网址在这:云原生数据库 TDSQL-C_云原生数据库_企业级分布式云数据库-腾讯云 ,实战营的地址是:AI驱动的TDSQL-Cserverless实战营学习课程_AI驱动的TDSQL-Cserverless实战营视频教程-腾讯云开发者社区我是需要好好学习一下的,这里我把整个学习的记录都记录在这里,希望能为大家创造一定价值。
目录
正文
我们先来购买一下,后面我们再进行具体的测试。
购买流程
我们这里选择Serverless,我习惯MySQL操作,地域的话就根据自己的地址就行。
这里我选择5.7的,大多数的企业还是没有升级到8.0,常用的还是5.7版本。
选择默认字符集,我这里选择UTF8。
开启公网访问
一定要开启公网访问哦。
本地Navicat链接测试:
这里在上面的图片中能看到获取位置,直接输入就行,变化是端口号不是3306了,需要注意一下。
使用Web登录到数据库:
直接点击登录就行,很方便。
进入到操作面板
新建数据库
我们来具体的实操一下。
输入名称,点击创建。
创建成功:
创建数据表DDL与DML
CREATE TABLE `ecommerce_sales_stats` (
`category_id` int NOT NULL COMMENT '分类ID(主键)',
`category_name` varchar(100) NOT NULL COMMENT '分类名称',
`total_sales` decimal(15,2) NOT NULL COMMENT '总销售额',
`steam_sales` decimal(15,2) NOT NULL COMMENT 'Steam平台销售额',
`offline_sales` decimal(15,2) NOT NULL COMMENT '线下实体销售额',
`official_online_sales` decimal(15,2) NOT NULL COMMENT '官方在线销售额',
PRIMARY KEY (`category_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 COMMENT='电商分类销售统计表';
INSERT INTO `ecommerce_sales_stats` VALUES (1,'电子产品',150000.00,80000.00,30000.00,40000.00),(2,'服装',120000.00,20000.00,60000.00,40000.00),(3,'家居用品',90000.00,10000.00,50000.00,30000.00),(4,'玩具',60000.00,5000.00,30000.00,25000.00),(5,'书籍',45000.00,2000.00,20000.00,23000.00),(6,'运动器材',70000.00,15000.00,25000.00,30000.00),(7,'美容护肤',80000.00,10000.00,30000.00,40000.00),(8,'食品',50000.00,5000.00,25000.00,20000.00),(9,'珠宝首饰',30000.00,2000.00,10000.00,18000.00),(10,'汽车配件',40000.00,10000.00,15000.00,25000.00),(11,'手机配件',75000.00,30000.00,20000.00,25000.00),(12,'电脑配件',85000.00,50000.00,15000.00,20000.00),(13,'摄影器材',50000.00,20000.00,15000.00,15000.00),(14,'家电',120000.00,60000.00,30000.00,30000.00),(15,'宠物用品',30000.00,3000.00,12000.00,16800.00),(16,'母婴用品',70000.00,10000.00,30000.00,30000.00),(17,'旅行用品',40000.00,5000.00,15000.00,20000.00),(18,'艺术品',25000.00,1000.00,10000.00,14000.00),(19,'健康产品',60000.00,8000.00,25000.00,27000.00),(20,'办公用品',55000.00,2000.00,20000.00,33000.00);
CREATE TABLE `users` (
`user_id` int NOT NULL AUTO_INCREMENT COMMENT '用户ID(主键,自增)',
`full_name` varchar(100) NOT NULL COMMENT '用户全名',
`username` varchar(50) NOT NULL COMMENT '用户名',
`email` varchar(100) NOT NULL COMMENT '用户邮箱',
`password_hash` varchar(255) NOT NULL COMMENT '用户密码的哈希值',
`created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_active` tinyint(1) DEFAULT '1' COMMENT '是否激活',
PRIMARY KEY (`user_id`),
UNIQUE KEY `email` (`email`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
INSERT INTO `users` VALUES (1,'张伟','zhangwei','zhangwei@example.com','hashed_password_1','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(2,'李娜','lina','lina@example.com','hashed_password_2','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(3,'王芳','wangfang','wangfang@example.com','hashed_password_3','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(4,'刘洋','liuyang','liuyang@example.com','hashed_password_4','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(5,'陈杰','chenjie','chenjie@example.com','hashed_password_5','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(6,'杨静','yangjing','yangjing@example.com','hashed_password_6','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(7,'赵强','zhaoqiang','zhaoqiang@example.com','hashed_password_7','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(8,'黄丽','huangli','huangli@example.com','hashed_password_8','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(9,'周杰','zhoujie','zhoujie@example.com','hashed_password_9','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(10,'吴敏','wumin','wumin@example.com','hashed_password_10','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(11,'郑伟','zhengwei','zhengwei@example.com','hashed_password_11','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(12,'冯婷','fengting','fengting@example.com','hashed_password_12','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(13,'蔡明','caiming','caiming@example.com','hashed_password_13','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(14,'潘雪','panxue','panxue@example.com','hashed_password_14','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(15,'蒋磊','jianglei','jianglei@example.com','hashed_password_15','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(16,'陆佳','lujia','lujia@example.com','hashed_password_16','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(17,'邓超','dengchao','dengchao@example.com','hashed_password_17','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(18,'任丽','renli','renli@example.com','hashed_password_18','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(19,'彭涛','pengtao','pengtao@example.com','hashed_password_19','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(20,'方圆','fangyuan','fangyuan@example.com','hashed_password_20','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(21,'段飞','duanfei','duanfei@example.com','hashed_password_21','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(22,'雷鸣','leiming','leiming@example.com','hashed_password_22','2024-08-18 04:07:18','2024-08-18 04:07:18',1),(23,'贾玲','jialing','jialing@example.com','hashed_password_23','2024-08-18 04:07:18','2024-08-18 04:07:18',1);
CREATE TABLE `orders` (
`order_id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL,
`order_amount` decimal(10,2) DEFAULT NULL,
`order_status` varchar(20) DEFAULT NULL,
`order_time` datetime DEFAULT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ;
INSERT INTO `orders` VALUES (1,3,150.50,'已支付','2024-08-23 10:01:00'),(2,7,89.20,'待支付','2024-08-23 10:03:15'),(3,12,230.00,'已支付','2024-08-23 10:05:30'),(4,2,99.90,'已发货','2024-08-23 10:07:45'),(5,15,120.00,'待发货','2024-08-23 10:10:00'),(6,21,180.50,'已支付','2024-08-23 10:12:15'),(7,4,105.80,'待支付','2024-08-23 10:14:30'),(8,18,210.00,'已支付','2024-08-23 10:16:45'),(9,6,135.20,'已发货','2024-08-23 10:19:00'),(10,10,160.00,'待发货','2024-08-23 10:21:15'),(11,1,110.50,'已支付','2024-08-23 10:23:30'),(12,22,170.80,'待支付','2024-08-23 10:25:45'),(13,8,145.20,'已发货','2024-08-23 10:28:00'),(14,16,190.00,'待发货','2024-08-23 10:30:15'),(15,11,125.50,'已支付','2024-08-23 10:32:30'),(16,19,165.20,'待支付','2024-08-23 10:34:45'),(17,5,130.00,'已发货','2024-08-23 10:37:00'),(18,20,175.80,'待发货','2024-08-23 10:39:15'),(19,13,140.50,'已支付','2024-08-23 10:41:30'),(20,14,155.20,'待支付','2024-08-23 10:43:45'),(21,9,135.50,'已发货','2024-08-23 10:46:00'),(22,23,185.80,'待发货','2024-08-23 10:48:15'),(23,17,160.50,'已支付','2024-08-23 10:50:30'),(24,12,145.20,'待支付','2024-08-23 10:52:45'),(25,3,130.00,'已发货','2024-08-23 10:55:00'),(26,8,115.50,'已支付','2024-08-23 10:57:15'),(27,19,120.20,'待支付','2024-08-23 10:59:30'),(28,6,145.50,'已发货','2024-08-23 11:01:45'),(29,14,130.20,'待支付','2024-08-23 11:04:00'),(30,5,125.50,'已支付','2024-08-23 11:06:15'),(31,21,135.20,'待支付','2024-08-23 11:08:30'),(32,7,140.50,'已发货','2024-08-23 11:10:45'),(33,16,120.20,'待支付','2024-08-23 11:13:00'),(34,10,135.50,'已支付','2024-08-23 11:15:15'),(35,2,140.20,'待支付','2024-08-23 11:17:30'),(36,12,145.20,'待支付','2024-08-23 12:00:00'),(37,15,130.20,'已支付','2024-08-23 12:02:15'),(38,20,125.50,'待发货','2024-08-23 12:04:30'),(39,17,135.20,'已支付','2024-08-23 12:06:45'),(40,4,140.50,'待支付','2024-08-23 12:09:00'),(41,10,120.20,'已发货','2024-08-23 12:11:15'),(42,13,135.50,'已支付','2024-08-23 12:13:30'),(43,18,145.20,'待支付','2024-08-23 12:15:45'),(44,6,130.20,'已发货','2024-08-23 12:18:00'),(45,11,125.50,'已支付','2024-08-23 12:20:15'),(46,19,135.20,'待支付','2024-08-23 12:22:30'),(47,5,140.50,'已发货','2024-08-23 12:24:45'),(48,20,120.20,'待支付','2024-08-23 12:27:00'),(49,17,135.50,'已支付','2024-08-23 12:29:15'),(50,4,145.20,'待支付','2024-08-23 12:31:30'),(51,10,130.20,'已发货','2024-08-23 12:33:45'),(52,13,125.50,'已支付','2024-08-23 12:36:00'),(53,18,135.20,'待支付','2024-08-23 12:38:15'),(54,6,140.50,'已发货','2024-08-23 12:40:30'),(55,11,120.20,'待支付','2024-08-23 12:42:45'),(56,19,135.50,'已支付','2024-08-23 12:45:00'),(57,5,145.20,'待支付','2024-08-23 12:47:15'),(58,20,130.20,'已发货','2024-08-23 12:49:30'),(59,17,125.50,'已支付','2024-08-23 13:01:45'),(60,4,135.20,'待支付','2024-08-23 13:04:00'),(61,10,140.50,'已发货','2024-08-23 13:06:15'),(62,13,120.20,'待支付','2024-08-23 13:08:30'),(63,18,135.50,'已支付','2024-08-23 13:10:45'),(64,6,145.20,'待支付','2024-08-23 13:13:00'),(65,11,130.20,'已发货','2024-08-23 13:15:15'),(66,19,125.50,'已支付','2024-08-23 13:17:30'),(67,5,135.20,'待支付','2024-08-23 13:19:45'),(68,20,140.50,'已发货','2024-08-23 13:22:00'),(69,17,120.20,'待支付','2024-08-23 13:24:15'),(70,4,135.50,'已支付','2024-08-23 13:26:30'),(71,10,145.20,'待支付','2024-08-23 13:28:45'),(72,13,130.20,'已发货','2024-08-23 13:31:00'),(73,18,125.50,'已支付','2024-08-23 13:33:15'),(74,6,135.20,'待支付','2024-08-23 13:35:30'),(75,11,140.50,'已发货','2024-08-23 13:37:45'),(76,19,120.20,'待支付','2024-08-23 13:40:00'),(77,5,135.50,'已支付','2024-08-23 13:42:15'),(78,20,145.20,'待支付','2024-08-23 13:44:30'),(79,17,130.20,'已发货','2024-08-23 13:46:45'),(80,4,125.50,'已支付','2024-08-23 13:49:00'),(81,10,135.20,'待支付','2024-08-23 13:51:15'),(82,13,140.50,'已发货','2024-08-23 13:53:30'),(83,18,120.20,'待支付','2024-08-23 13:55:45'),(84,6,135.50,'已支付','2024-08-23 13:58:00'),(85,11,145.20,'待支付','2024-08-23 14:00:15'),(86,19,130.20,'已发货','2024-08-23 14:02:30'),(87,5,125.50,'已支付','2024-08-23 14:04:45'),(88,20,135.20,'待支付','2024-08-23 14:07:00'),(89,17,140.50,'已发货','2024-08-23 14:09:15'),(90,4,120.20,'待支付','2024-08-23 14:11:30'),(91,10,135.50,'已支付','2024-08-23 14:13:45'),(92,13,145.20,'待支付','2024-08-23 14:16:00'),(93,18,130.20,'已发货','2024-08-23 14:18:15'),(94,6,125.50,'已支付','2024-08-23 14:20:30'),(95,11,135.20,'待支付','2024-08-23 14:22:45'),(96,19,140.50,'已发货','2024-08-23 14:25:00'),(97,5,120.20,'待支付','2024-08-23 14:27:15'),(98,20,135.50,'已支付','2024-08-23 14:29:30'),(99,17,145.20,'待支付','2024-08-23 14:31:45'),(100,4,130.20,'已发货','2024-08-23 14:34:00'),(101,10,125.50,'已支付','2024-08-23 14:36:15'),(102,13,135.20,'待支付','2024-08-23 14:38:30'),(103,18,140.50,'已发货','2024-08-23 14:40:45'),(104,16,120.20,'待支付','2024-08-23 14:43:00'),(105,12,135.50,'已支付','2024-08-23 14:45:15'),(106,3,145.20,'待支付','2024-08-23 14:47:30'),(107,8,130.20,'已发货','2024-08-23 14:49:45'),(108,19,125.50,'已支付','2024-08-23 14:52:00'),(109,6,135.20,'待支付','2024-08-23 14:54:15'),(110,14,140.50,'已发货','2024-08-23 14:56:30'),(111,10,120.20,'待支付','2024-08-23 14:58:45'),(112,13,135.50,'已支付','2024-08-23 15:01:00'),(113,18,145.20,'待支付','2024-08-23 15:03:15'),(114,6,130.20,'已发货','2024-08-23 15:05:30'),(115,11,125.50,'已支付','2024-08-23 15:07:45'),(116,19,135.20,'待支付','2024-08-23 15:10:00'),(117,5,140.50,'已发货','2024-08-23 15:12:15'),(118,20,120.20,'待支付','2024-08-23 15:14:30'),(119,17,135.50,'已支付','2024-08-23 15:16:45'),(120,4,145.20,'待支付','2024-08-23 15:19:00'),(121,10,130.20,'已发货','2024-08-23 15:21:15'),(122,13,125.50,'已支付','2024-08-23 15:23:30'),(123,18,135.20,'待支付','2024-08-23 15:25:45'),(124,6,140.50,'已发货','2024-08-23 15:28:00'),(125,11,120.20,'待支付','2024-08-23 15:30:15'),(126,19,135.50,'已支付','2024-08-23 15:32:30'),(127,5,145.20,'待支付','2024-08-23 15:34:45'),(128,20,130.20,'已发货','2024-08-23 15:37:00'),(129,17,125.50,'已支付','2024-08-23 15:39:15'),(130,4,135.20,'待支付','2024-08-23 15:41:30'),(131,10,140.50,'已发货','2024-08-23 15:43:45'),(132,13,120.20,'待支付','2024-08-23 15:46:00'),(133,18,135.50,'已支付','2024-08-23 15:48:15'),(134,6,145.20,'待支付','2024-08-23 15:50:30'),(135,11,130.20,'已发货','2024-08-23 15:52:45'),(136,19,125.50,'已支付','2024-08-23 15:55:00'),(137,5,135.20,'待支付','2024-08-23 15:57:15'),(138,20,140.50,'已发货','2024-08-23 15:59:30'),(139,17,120.20,'待支付','2024-08-23 16:01:45'),(140,4,135.50,'已支付','2024-08-23 16:04:00'),(141,10,145.20,'待支付','2024-08-23 16:06:15'),(142,13,130.20,'已发货','2024-08-23 16:08:30'),(143,18,125.50,'已支付','2024-08-23 16:10:45'),(144,6,135.20,'待支付','2024-08-23 16:13:00'),(145,11,140.50,'已发货','2024-08-23 16:15:15'),(146,19,120.20,'待支付','2024-08-23 16:17:30'),(147,5,135.50,'已支付','2024-08-23 16:19:45'),(148,20,145.20,'待支付','2024-08-23 16:22:00'),(149,17,130.20,'已发货','2024-08-23 16:24:15'),(150,4,125.50,'已支付','2024-08-23 16:26:30'),(151,10,135.20,'待支付','2024-08-23 16:28:45'),(152,13,140.50,'已发货','2024-08-23 16:31:00'),(153,18,120.20,'待支付','2024-08-23 16:33:15'),(154,6,135.50,'已支付','2024-08-23 16:35:30'),(155,11,145.20,'待支付','2024-08-23 16:37:45'),(156,19,130.20,'已发货','2024-08-23 16:40:00'),(157,5,125.50,'已支付','2024-08-23 16:42:15'),(158,20,135.20,'待支付','2024-08-23 16:44:30'),(159,17,140.50,'已发货','2024-08-23 16:46:45'),(160,4,120.20,'待支付','2024-08-23 16:49:00'),(161,10,135.50,'已支付','2024-08-23 16:51:15'),(162,13,145.20,'待支付','2024-08-23 16:53:30'),(163,18,130.20,'已发货','2024-08-23 16:55:45'),(164,6,125.50,'已支付','2024-08-23 16:58:00'),(165,11,135.20,'待支付','2024-08-23 17:00:15'),(166,19,140.50,'已发货','2024-08-23 17:02:30'),(167,5,120.20,'待支付','2024-08-23 17:04:45'),(168,20,135.50,'已支付','2024-08-23 17:07:00'),(169,17,145.20,'待支付','2024-08-23 17:09:15'),(170,4,130.20,'已发货','2024-08-23 17:11:30'),(171,10,125.50,'已支付','2024-08-23 17:13:45'),(172,13,135.20,'待支付','2024-08-23 17:16:00'),(173,18,140.50,'已发货','2024-08-23 17:18:15'),(174,6,120.20,'待支付','2024-08-23 17:20:30'),(175,11,135.50,'已支付','2024-08-23 17:22:45'),(176,19,145.20,'待支付','2024-08-23 17:25:00'),(177,5,130.20,'已发货','2024-08-23 17:27:15'),(178,20,125.50,'已支付','2024-08-23 17:29:30'),(179,17,135.20,'待支付','2024-08-23 17:31:45'),(180,4,140.50,'已发货','2024-08-23 17:34:00'),(181,10,120.20,'待支付','2024-08-23 17:36:15'),(182,13,135.50,'已支付','2024-08-23 17:38:30'),(183,18,145.20,'待支付','2024-08-23 17:40:45'),(184,6,130.20,'已发货','2024-08-23 17:43:00'),(185,11,125.50,'已支付','2024-08-23 17:45:15'),(186,19,135.20,'待支付','2024-08-23 17:47:30'),(187,5,140.50,'已发货','2024-08-23 17:49:45'),(188,20,120.20,'待支付','2024-08-23 17:52:00'),(189,17,135.50,'已支付','2024-08-23 17:54:15'),(190,4,145.20,'待支付','2024-08-23 17:56:30'),(191,10,130.20,'已发货','2024-08-23 17:58:45'),(192,13,125.50,'已支付','2024-08-23 18:01:00'),(193,18,135.20,'待支付','2024-08-23 18:03:15'),(194,6,140.50,'已发货','2024-08-23 18:05:30'),(195,11,120.20,'待支付','2024-08-23 18:07:45'),(196,19,135.50,'已支付','2024-08-23 18:10:00'),(197,5,145.20,'待支付','2024-08-23 18:12:15'),(198,20,130.20,'已发货','2024-08-23 18:14:30'),(199,17,125.50,'已支付','2024-08-23 18:16:45'),(200,4,135.20,'待支付','2024-08-23 18:19:00'),(201,10,140.50,'已发货','2024-08-23 18:21:15'),(202,13,120.20,'待支付','2024-08-23 18:23:30'),(203,18,135.50,'已支付','2024-08-23 18:25:45'),(204,6,145.20,'待支付','2024-08-23 18:28:00'),(205,11,130.20,'已发货','2024-08-23 18:30:15'),(206,19,125.50,'已支付','2024-08-23 18:32:30'),(207,5,135.20,'待支付','2024-08-23 18:34:45'),(208,20,140.50,'已发货','2024-08-23 18:37:00'),(209,17,120.20,'待支付','2024-08-23 18:39:15'),(210,4,135.50,'已支付','2024-08-23 18:41:30'),(211,10,145.20,'待支付','2024-08-23 18:43:45');
操作过程:
刷新一下列表。
部署HAI高算力服务器
HAI主页地址:腾讯云HAI高性能应用服务
创建中:
启动完毕后需要检查是否已经默认开放 6399端口,如下状态即是开放。
查看一下,确认我们的环境中有llama3.1.8
Llama访问测试:
本地Python编码
我们本地Python环境肯定有,我就不再累述了。但是需要的环境我这里要说明一下:
pip install openai
pip install langchain
pip install langchain-core
pip install langchain-community
pip install mysql-connector-python
pip install streamlit
pip install plotly
pip install numpy
pip install pandas
pip install watchdog
pip install matplotlib
pip install kaleido
慢慢装载即可。
创建文件:
代码这里就是配置与提问。
配置,按照自己的信息来修改:
database:
db_user: root
db_password: your password
db_host: bj-cynosdbmysql-grp-kjfaeho8.sql.tencentcdb.com
db_port: 22696
db_name: shop
hai:
model: llama3.1:8b
base_url: http://62.234.25.23:6399
from langchain_community.utilities import SQLDatabase
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
import yaml
import mysql.connector
from decimal import Decimal
import plotly.graph_objects as go
import plotly
import pkg_resources
import matplotlib
yaml_file_path = 'config.yaml'
with open(yaml_file_path, 'r') as file:
config_data = yaml.safe_load(file)
#获取所有的已安装的pip包
def get_piplist(p):
return [d.project_name for d in pkg_resources.working_set]
#获取llm用于提供AI交互
ollama = ChatOllama(model=config_data['hai']['model'],base_url=config_data['hai']['base_url'])
db_user = config_data['database']['db_user']
db_password = config_data['database']['db_password']
db_host = config_data['database']['db_host']
db_port= config_data['database']['db_port']
db_name = config_data['database']['db_name']
# 获得schema
def get_schema(db):
schema = mysql_db.get_table_info()
return schema
def getResult(content):
global mysql_db
# 数据库连接
mysql_db = SQLDatabase.from_uri(f"mysql+mysqlconnector://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}")
# 获得 数据库中表的信息
#mysql_db_schema = mysql_db.get_table_info()
#print(mysql_db_schema)
template = """基于下面提供的数据库schema, 根据用户提供的要求编写sql查询语句,要求尽量使用最优sql,每次查询都是独立的问题,不要收到其他查询的干扰:
{schema}
Question: {question}
只返回sql语句,不要任何其他多余的字符,例如markdown的格式字符等:
如果有异常抛出不要显示出来
"""
prompt = ChatPromptTemplate.from_template(template)
text_2_sql_chain = (
RunnablePassthrough.assign(schema=get_schema)
| prompt
| ollama
| StrOutputParser()
)
# 执行langchain 获取操作的sql语句
sql = text_2_sql_chain.invoke({"question": content})
print(sql)
#连接数据库进行数据的获取
# 配置连接信息
conn = mysql.connector.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
database=db_name
)
# 创建游标对象
cursor = conn.cursor()
# 查询数据
cursor.execute(sql.strip("```").strip("```sql"))
info = cursor.fetchall()
# 打印结果
#for row in info:
#print(row)
# 关闭游标和数据库连接
cursor.close()
conn.close()
#根据数据生成对应的图表
print(info)
template2 = """
以下提供当前python环境已经安装的pip包集合:
{installed_packages};
请根据data提供的信息,生成是一个适合展示数据的plotly的图表的可执行代码,要求如下:
1.不要导入没有安装的pip包代码
2.如果存在多个数据类别,尽量使用柱状图,循环生成时图表中对不同数据请使用不同颜色区分,
3.图表要生成图片格式,保存在当前文件夹下即可,名称固定为:图表.png,
4.我需要您生成的代码是没有 Markdown 标记的,纯粹的编程语言代码。
5.生成的代码请注意将所有依赖包提前导入,
6.不要使用iplot等需要特定环境的代码
7.请注意数据之间是否可以转换,使用正确的代码
8.不需要生成注释
data:{data}
这是查询的sql语句与文本:
sql:{sql}
question:{question}
返回数据要求:
仅仅返回python代码,不要有额外的字符
"""
prompt2 = ChatPromptTemplate.from_template(template2)
data_2_code_chain = (
RunnablePassthrough.assign(installed_packages=get_piplist)
| prompt2
| ollama
| StrOutputParser()
)
# 执行langchain 获取操作的sql语句
code = data_2_code_chain.invoke({"data": info,"sql":sql,'question':content})
#删除数据两端可能存在的markdown格式
print(code.strip("```").strip("```python"))
exec(code.strip("```").strip("```python"))
return {"code":code,"SQL":sql,"Query":info}
# 构建展示页面
import streamlit
# 设置页面标题
streamlit.title('AI驱动的数据库TDSQL-C 电商可视化分析小助手')
# 设置对话框
content = streamlit.text_area('请输入想查询的信息', value='', max_chars=None)
# 提问按钮 # 设置点击操作
if streamlit.button('提问'):
#开始ai及langchain操作
if content:
#进行结果获取
result = getResult(content)
#显示操作结果
streamlit.write('AI生成的SQL语句:')
streamlit.write(result['SQL'])
streamlit.write('SQL语句的查询结果:')
streamlit.write(result['Query'])
streamlit.write('plotly图表代码:')
streamlit.write(result['code'])
# 显示图表内容(生成在getResult中)
streamlit.image('./图表.png', width=800)
运行并测试效果
streamlit run text2sql2plotly.py
浏览器查看:
提问测试:
后端效果:
最终看到返回结果,由于超时没返回出来图片,但是给代码了,我们自己运行一下。
import plotly.express as px
from decimal import Decimal
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
data = [
('电子产品', Decimal('150000.00')),
('服装', Decimal('120000.00')),
('家居用品', Decimal('90000.00')),
('玩具', Decimal('60000.00')),
('书籍', Decimal('45000.00')),
('运动器材', Decimal('70000.00')),
('美容护肤', Decimal('80000.00')),
('食品', Decimal('50000.00')),
('珠宝首饰', Decimal('30000.00')),
('汽车配件', Decimal('40000.00')),
('手机配件', Decimal('75000.00')),
('电脑配件', Decimal('85000.00')),
('摄影器材', Decimal('50000.00')),
('家电', Decimal('120000.00')),
('宠物用品', Decimal('30000.00')),
('母婴用品', Decimal('70000.00')),
('旅行用品', Decimal('40000.00')),
('艺术品', Decimal('25000.00')),
('健康产品', Decimal('60000.00')),
('办公用品', Decimal('55000.00'))
]
# 生成DataFrame
df = pd.DataFrame(data, columns=['category_name', 'total_sales'])
# 将数据转换为数字类型
df['total_sales'] = df['total_sales'].astype(float)
# 使用柱状图进行可视化
fig = px.bar(df, x='category_name', y='total_sales', color_discrete_sequence=px.colors.sequential.Plotly3)
fig.update_layout(title='各类商品销售总额',
xaxis_title='类别',
yaxis_title='销售金额')
fig.show()
# 保存图片
plt.savefig('图表.png')
实验完毕,这里说明一下,有的时候配置完毕会卡在结果返回上,等很久也不会有反馈。
总结
实践出真知,我们动手操作一下还是非常有价值的呢,希望本次的体验对大家能有一定的价值。
现在活动还在进行时。可以去搞一搞。实战营地址:AI驱动的TDSQL-Cserverless实战营学习课程_AI驱动的TDSQL-Cserverless实战营视频教程-腾讯云开发者社区
更多推荐
所有评论(0)