基于SpringAI的电商客服智能体

发布于:2025-05-20 ⋅ 阅读:(15) ⋅ 点赞:(0)

简介

参考了spring-ai-alibaba-examples仓库中的playground-flight-booking项目,制作了一个基于SpringAI的具备RAG检索增强生成,对话记忆,函数调用,MCP服务调用等功能的电商客服智能体。

后端代码链接:https://github.com/16Miku/spring-ai-alibaba-agent

前端代码链接:https://github.com/16Miku/chat2-spring-ai


函数调用功能演示

alt text

成功读取到服务条款文档.

alt text

alt text

询问AI客服有哪些功能.

调用"创建新用户"功能

alt text

alt text

创建一个名为Miku的用户

alt text

日志信息中可以看到成功执行sql语句.

alt text

数据库中也可以看到新用户Miku的信息.


调用"更新用户信息"功能

alt text

将id为21的用户的密码修改为123456

alt text

日志信息中可以看到成功执行sql语句.

alt text

数据库中也可以看到id为21的用户的密码已修改为123456.


调用"根据用户ID获取用户信息"功能

alt text

查询id为1的用户的信息

alt text

日志信息中可以看到成功执行sql语句.

alt text

AI查询的数据符合数据库记录.???


调用"根据商品ID获取商品信息"功能

alt text

alt text

日志信息中可以看到成功执行sql语句.

alt text

AI查询的数据符合数据库记录.???


调用"创建订单"功能

alt text

为id为1的用户下单两件id为1的商品

            : Tool 'createOrder' called with request: createOrderRequest[userId=1, items=[OrderItem(id=null, orderId=null, itemId=1, itemName=null, itemPrice=null, quantity=2, createTime=null, updateTime=null)]]
2025-05-12T20:50:41.198+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.UserServiceImpl       : Fetching user with ID: 1
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
JDBC Connection [HikariProxyConnection@50792400 wrapping com.mysql.cj.jdbc.ConnectionImpl@6d9bc4b3] will be managed by Spring
==>  Preparing: SELECT id, username, password, email, create_time, update_time FROM users WHERE id = ?
==> Parameters: 1(Long)
<==    Columns: id, username, password, email, create_time, update_time
<==        Row: 1, updatedTestUser, newSecurePassword456, updated.test@example.com, 2025-05-11 16:23:24, 2025-05-12 14:43:05
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
2025-05-12T20:50:41.202+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.ItemServiceImpl       : Fetching item with ID: 1
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e] from current transaction
==>  Preparing: SELECT id, name, price, stock, create_time, update_time FROM items WHERE id = ?
==> Parameters: 1(Long)
<==    Columns: id, name, price, stock, create_time, update_time
<==        Row: 1, 笔记本电脑, 5999.00, 97, 2025-05-11 16:23:24, 2025-05-12 14:43:12
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e] from current transaction
==>  Preparing: INSERT INTO orders (user_id, total_amount, status) VALUES (?, ?, ?)
==> Parameters: 1(Long), 11998.00(BigDecimal), PENDING(String)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
2025-05-12T20:50:41.211+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.OrderServiceImpl      : Order created successfully with ID: 5
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e] from current transaction
==>  Preparing: INSERT INTO order_items (order_id, item_id, item_name, item_price, quantity) VALUES (?, ?, ?, ?, ?)
==> Parameters: 5(Long), 1(Long), 笔记本电脑(String), 5999.00(BigDecimal), 2(Integer)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
2025-05-12T20:50:41.225+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.OrderServiceImpl      : Order items inserted successfully for order ID: 5
2025-05-12T20:50:41.225+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.ItemServiceImpl       : Decreasing stock for item ID 1 by 2
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e] from current transaction
==>  Preparing: UPDATE items SET stock = stock - ? WHERE id = ? AND stock >= ?
==> Parameters: 2(Integer), 1(Long), 2(Integer)
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
2025-05-12T20:50:41.229+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.ItemServiceImpl       : Successfully decreased stock for item ID 1
2025-05-12T20:50:41.230+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.impl.OrderServiceImpl      : Stock decreased successfully for items in order ID: 5
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4509b93e]
2025-05-12T20:50:41.233+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-12] c.m.s.service.CustomTools                : Tool 'createOrder' response: createOrderResponse[order=Order(id=5, userId=1, totalAmount=11998.00, status=PENDING, createTime=null, updateTime=null, orderItems=[OrderItem(id=null, orderId=5, itemId=1, itemName=笔记本电脑, itemPrice=5999.00, quantity=2, createTime=null, updateTime=null)])]

日志信息中可以看到成功执行sql语句.

alt text

数据库中也可以看到新订单的信息.


调用"根据订单ID获取订单详情"功能

alt text

查询id为5的订单的信息

Tool 'getOrderById' called with request: getOrderByIdRequest[id=5]
2025-05-12T21:01:13.744+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-14] c.m.s.service.impl.OrderServiceImpl      : Fetching order with ID: 5
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a1efd6c] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1544787482 wrapping com.mysql.cj.jdbc.ConnectionImpl@1b0cc4ad] will not be managed by Spring
==>  Preparing: SELECT id, user_id, total_amount, status, create_time, update_time FROM orders WHERE id = ?
==> Parameters: 5(Long)
<==    Columns: id, user_id, total_amount, status, create_time, update_time
<==        Row: 5, 1, 11998.00, PENDING, 2025-05-12 20:50:41, 2025-05-12 20:50:41
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a1efd6c]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@70baae07] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@819545420 wrapping com.mysql.cj.jdbc.ConnectionImpl@1b0cc4ad] will not be managed by Spring
==>  Preparing: SELECT id, order_id, item_id, item_name, item_price, quantity, create_time, update_time FROM order_items WHERE order_id = ?
==> Parameters: 5(Long)
<==    Columns: id, order_id, item_id, item_name, item_price, quantity, create_time, update_time
<==        Row: 6, 5, 1, 笔记本电脑, 5999.00, 2, 2025-05-12 20:50:41, 2025-05-12 20:50:41
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@70baae07]
2025-05-12T21:01:13.761+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-14] c.m.s.service.CustomTools                : Tool 'getOrderById' response: getOrderByIdResponse[order=Order(id=5, userId=1, totalAmount=11998.00, status=PENDING, createTime=Mon May 12 20:50:41 CST 2025, updateTime=Mon May 12 20:50:41 CST 2025, orderItems=[OrderItem(id=6, orderId=5, itemId=1, itemName=笔记本电脑, itemPrice=5999.00, quantity=2, createTime=Mon May 12 20:50:41 CST 2025, updateTime=Mon May 12 20:50:41 CST 2025)])]

日志信息中可以看到成功执行sql语句.


调用"更新订单状态"功能

alt text

将id为5的订单改为已支付

alt text

日志信息中可以看到成功执行sql语句.

alt text

数据库中也可以看到订单状态由PENDING变为了PAID的信息.


调用"创建支付记录"功能

alt text

为id为5的订单创建支付记录

alt text

  : Tool 'createPayment' called with request: createPaymentRequest[orderId=5, amount=11998.0, paymentMethod=信用卡]
2025-05-12T21:12:26.736+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-16] c.m.s.service.impl.OrderServiceImpl      : Fetching order with ID: 5
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
JDBC Connection [HikariProxyConnection@697353375 wrapping com.mysql.cj.jdbc.ConnectionImpl@1b0cc4ad] will be managed by Spring
==>  Preparing: SELECT id, user_id, total_amount, status, create_time, update_time FROM orders WHERE id = ?
==> Parameters: 5(Long)
<==    Columns: id, user_id, total_amount, status, create_time, update_time
<==        Row: 5, 1, 11998.00, PAID, 2025-05-12 20:50:41, 2025-05-12 21:06:35
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385] from current transaction
==>  Preparing: SELECT id, order_id, item_id, item_name, item_price, quantity, create_time, update_time FROM order_items WHERE order_id = ?
==> Parameters: 5(Long)
<==    Columns: id, order_id, item_id, item_name, item_price, quantity, create_time, update_time
<==        Row: 6, 5, 1, 笔记本电脑, 5999.00, 2, 2025-05-12 20:50:41, 2025-05-12 20:50:41
<==      Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385] from current transaction
==>  Preparing: SELECT id, order_id, amount, status, payment_method, transaction_id, create_time, update_time FROM payments WHERE order_id = ?
==> Parameters: 5(Long)
<==      Total: 0
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385] from current transaction
==>  Preparing: INSERT INTO payments (order_id, amount, status, payment_method, transaction_id) VALUES (?, ?, ?, ?, ?)
==> Parameters: 5(Long), 11998.0(BigDecimal), PENDING(String), 信用卡(String), null
<==    Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
2025-05-12T21:12:26.760+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-16] c.m.s.service.impl.PayServiceImpl        : Payment record created successfully for order ID 5. Payment ID: 4
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e692385]
2025-05-12T21:12:26.763+08:00  INFO 20112 --- [spring-ai-alibaba-agent] [tor-http-nio-16] c.m.s.service.CustomTools                : Tool 'createPayment' response: createPaymentResponse[payment=Payment(id=4, orderId=5, amount=11998.0, status=PENDING, paymentMethod=信用卡, transactionId=null, createTime=null, updateTime=null)]

日志信息中可以看到成功执行sql语句.

alt text

数据库中也可以看到新支付单的信息.


调用"查询支付记录"功能

alt text

查询id为2的订单的支付单信息

alt text

日志信息中可以看到成功执行sql语句.

alt text


调用"更新支付记录"功能

alt text

将id为4的支付单状态改为已支付

alt text

日志信息中可以看到成功执行sql语句.

alt text


MySQL-MCP功能演示

使用的mcp-mysql-server链接:https://github.com/f4ww4z/mcp-mysql-server

connect_db

alt text

alt text

list_tables

alt text

query

alt text

execute

alt text

describe_table

alt text


总结

本文详细展示了如何基于 Spring AI 框架,结合 RAG、对话记忆、函数调用以及 MCP 服务调用等技术,构建一个功能丰富的电商客服智能体。

通过 Spring AI 提供的强大能力,我们能够:

  1. 利用 RAG 增强智能体的知识库,使其能够回答基于特定文档(如服务条款)的复杂问题。
  2. 通过 对话记忆 保持多轮对话的连贯性,提供更自然的交互体验。
  3. 实现 函数调用,让智能体能够执行实际的业务操作,例如创建用户、更新订单状态、查询商品信息等,将 AI 能力与后端服务紧密集成。
  4. 探索 MCP 服务调用,展示了智能体与数据库等外部系统的直接交互能力。

这个项目不仅验证了 Spring AI 在构建复杂智能体应用方面的潜力,也为电商领域提供了一个智能化的客服解决方案原型,能够有效提升用户体验和运营效率。未来的工作可以进一步优化模型的理解和决策能力,扩展更多业务功能,并提升系统的稳定性和可扩展性。



网站公告

今日签到

点亮在社区的每一天
去签到