百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

聊聊Spring AI Alibaba的JdbcChatMemory

ztj100 2025-05-03 17:52 11 浏览 0 评论

本文主要研究一下Spring AI Alibaba的JdbcChatMemory

JdbcChatMemory

community/memories/spring-ai-alibaba-jdbc-memory/src/main/java/com/alibaba/cloud/ai/memory/jdbc/JdbcChatMemory.java

public abstract class JdbcChatMemory implements ChatMemory, AutoCloseable {

  private static final Logger logger = LoggerFactory.getLogger(JdbcChatMemory.class);

  private static final String DEFAULT_TABLE_NAME = "chat_memory";

  private final Connection connection;

  private final String tableName;

  protected JdbcChatMemory(String username, String password, String jdbcUrl) {
    this(username, password, jdbcUrl, DEFAULT_TABLE_NAME);
  }

  protected JdbcChatMemory(String username, String password, String jdbcUrl, String tableName) {
    this.tableName = tableName;
    try {
      this.connection = DriverManager.getConnection(jdbcUrl, username, password);
      checkAndCreateTable();
    }
    catch (SQLException e) {
      throw new RuntimeException("Error connecting to the database", e);
    }
  }

  protected JdbcChatMemory(Connection connection) {
    this(connection, DEFAULT_TABLE_NAME);
  }

  protected JdbcChatMemory(Connection connection, String tableName) {
    this.connection = connection;
    this.tableName = tableName;
    try {
      checkAndCreateTable();
    }
    catch (SQLException e) {
      throw new RuntimeException("Error checking the database table", e);
    }
  }

  protected abstract String jdbcType();

  protected abstract String hasTableSql(String tableName);

  protected abstract String createTableSql(String tableName);

  /**
   * Generate paginated query SQL based on the database type. Default implementation
   * uses LIMIT clause. Subclasses can override this method if their database uses
   * different pagination syntax.
   * @param tableName The name of the table
   * @param lastN Number of records to return, if greater than 0
   * @return SQL query string with pagination
   */
  protected String generatePaginatedQuerySql(String tableName, int lastN) {
    StringBuilder sqlBuilder = new StringBuilder("SELECT messages,type FROM ").append(tableName)
      .append(" WHERE conversation_id = ?");

    if (lastN > 0) {
      sqlBuilder.append(" LIMIT ?");
    }

    return sqlBuilder.toString();
  }

  private void checkAndCreateTable() throws SQLException {
    String checkTableQuery = hasTableSql(tableName);
    try (Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(checkTableQuery)) {
      if (rs.next()) {
        logger.info("Table {} exists.", tableName);
      }
      else {
        logger.info("Table {} does not exist. Creating table...", tableName);
        createTable();
      }
    }
  }

  private void createTable() {
    try (Statement stmt = connection.createStatement()) {
      stmt.execute(createTableSql(tableName));
      logger.info("Table {} created successfully.", tableName);
    }
    catch (Exception e) {
      throw new RuntimeException("Error creating table " + tableName + " ", e);
    }
  }

  @Override
  public void add(String conversationId, List<Message> messages) {
    try {
      for (Message message : messages) {
        String sql = "INSERT INTO " + tableName + " (messages, conversation_id, type) VALUES (?, ?, ?)";
        try (PreparedStatement stmt = this.connection.prepareStatement(sql)) {
          stmt.setString(1, message.getText());
          stmt.setString(2, conversationId);
          stmt.setString(3, message.getMessageType().name());
          stmt.executeUpdate();
        }
      }
    }
    catch (Exception e) {
      logger.error("Error adding messages to {} chat memory", jdbcType(), e);
      throw new RuntimeException(e);
    }
  }

  @Override
  public List<Message> get(String conversationId, int lastN) {
    return this.selectMessageById(conversationId, lastN);
  }

  @Override
  public void clear(String conversationId) {
    String sql = "DELETE FROM " + tableName + " WHERE conversation_id = ?";
    try (PreparedStatement stmt = connection.prepareStatement(sql)) {
      stmt.setString(1, conversationId);
      stmt.executeUpdate();
    }
    catch (Exception e) {
      logger.error("Error clearing messages from {} chat memory", jdbcType(), e);
      throw new RuntimeException("Error executing delete ", e);
    }

  }

  @Override
  public void close() throws Exception {
    if (connection != null) {
      connection.close();
    }
  }

  public void clearOverLimit(String conversationId, int maxLimit, int deleteSize) {
    try {
      List<Message> all = this.selectMessageById(conversationId);
      if (all.size() >= maxLimit) {
        // Delete oldest messages first
        String sql = "DELETE FROM " + tableName + " WHERE conversation_id = ? ORDER BY ROWID LIMIT ?";
        try (PreparedStatement stmt = this.connection.prepareStatement(sql)) {
          stmt.setString(1, conversationId);
          stmt.setInt(2, deleteSize);
          stmt.executeUpdate();
        }
        catch (SQLException e) {
          // If the database doesn't support ORDER BY in DELETE, fallback to
          // alternative approach
          all = all.stream().skip(Math.max(0, deleteSize)).toList();
          // Clear all messages and reinsert the remaining ones
          clear(conversationId);
          for (Message message : all) {
            add(conversationId, List.of(message));
          }
        }
      }
    }
    catch (Exception e) {
      logger.error("Error clearing messages from {} chat memory", jdbcType(), e);
      throw new RuntimeException(e);
    }
  }

  public List<Message> selectMessageById(String conversationId, int lastN) {
    List<Message> totalMessage = new ArrayList<>();
    String sql = generatePaginatedQuerySql(tableName, lastN);
    try (PreparedStatement stmt = connection.prepareStatement(sql)) {
      // Set conversation ID parameters.
      stmt.setString(1, conversationId);
      // If there is a limit, set the limit parameter.
      if (lastN > 0) {
        stmt.setInt(2, lastN);
      }
      ResultSet resultSet = stmt.executeQuery();
      while (resultSet.next()) {
        var content = resultSet.getString("messages");
        var type = MessageType.valueOf(resultSet.getString("type"));

        var message = switch (type) {
          case USER -> new UserMessage(content);
          case ASSISTANT -> new AssistantMessage(content);
          case SYSTEM -> new SystemMessage(content);
          default -> null;
        };
        totalMessage.add(message);

      }
    }
    catch (SQLException e) {
      logger.error("select message by {} error,sql:{}", jdbcType(), sql, e);
      throw new RuntimeException(e);
    }
    return totalMessage;
  }

  public List<Message> selectMessageById(String conversationId) {
    return this.selectMessageById(conversationId, 0);
  }

}

JdbcChatMemory构造器要求输入String username, String password, String jdbcUrl, String tableName,默认表名为chat_memory,它定义了jdbcType、hasTableSql、createTableSql三个抽象方法;其add方法遍历messages,挨个使用PreparedStatement插入人数据;其get方法则查询指定conversation_id的最近几条记录;其clear方法则删除指定conversation_id的所有记录。

MysqlChatMemory

community/memories/spring-ai-alibaba-jdbc-memory/src/main/java/com/alibaba/cloud/ai/memory/jdbc/MysqlChatMemory.java

public class MysqlChatMemory extends JdbcChatMemory {

  private static final String JDBC_TYPE = "mysql";

  public MysqlChatMemory(String username, String password, String jdbcUrl) {
    super(username, password, jdbcUrl);
  }

  public MysqlChatMemory(String username, String password, String jdbcUrl, String tableName) {
    super(username, password, jdbcUrl, tableName);
  }

  public MysqlChatMemory(Connection connection) {
    super(connection);
  }

  public MysqlChatMemory(Connection connection, String tableName) {
    super(connection, tableName);
  }

  @Override
  protected String jdbcType() {
    return JDBC_TYPE;
  }

  @Override
  protected String hasTableSql(String tableName) {
    return String.format("SHOW TABLES LIKE '%s'", tableName);
  }

  @Override
  protected String createTableSql(String tableName) {
    return String.format(
        "CREATE TABLE %s( id BIGINT AUTO_INCREMENT PRIMARY KEY,conversation_id  VARCHAR(256)  NULL,messages TEXT NULL,type varchar(100) NULL) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;",
        tableName);
  }

}

MysqlChatMemory继承了JdbcChatMemory,其jdbcType为mysql

OracleChatMemory

community/memories/spring-ai-alibaba-jdbc-memory/src/main/java/com/alibaba/cloud/ai/memory/jdbc/OracleChatMemory.java

public class OracleChatMemory extends JdbcChatMemory {

  private static final String JDBC_TYPE = "oracle";

  public OracleChatMemory(String username, String password, String url) {
    super(username, password, url);
  }

  public OracleChatMemory(String username, String password, String url, String tableName) {
    super(username, password, url, tableName);
  }

  public OracleChatMemory(Connection connection) {
    super(connection);
  }

  public OracleChatMemory(Connection connection, String tableName) {
    super(connection, tableName);
  }

  @Override
  protected String jdbcType() {
    return JDBC_TYPE;
  }

  @Override
  protected String hasTableSql(String tableName) {
    return String.format("SELECT table_name FROM user_tables WHERE table_name = '%s'", tableName.toUpperCase());
  }

  @Override
  protected String createTableSql(String tableName) {
    return String.format(
        "CREATE TABLE %s ( id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, conversation_id VARCHAR2(256), messages CLOB, type VARCHAR2(100));",
        tableName);
  }

  @Override
  protected String generatePaginatedQuerySql(String tableName, int lastN) {
    StringBuilder sqlBuilder = new StringBuilder().append("SELECT * FROM (")
      .append("SELECT messages,type FROM ")
      .append(tableName)
      .append(" WHERE conversation_id = ?");

    if (lastN > 0) {
      sqlBuilder.append(" AND ROWNUM <= ?");
    }

    sqlBuilder.append(")");
    return sqlBuilder.toString();
  }

}

OracleChatMemory继承了JdbcChatMemory,其jdbcType为oracle,它还额外覆盖了generatePaginatedQuerySql方法

PostgresChatMemory

public class PostgresChatMemory extends JdbcChatMemory {

  private static final String JDBC_TYPE = "postgresql";

  public PostgresChatMemory(String username, String password, String url) {
    super(username, password, url);
  }

  public PostgresChatMemory(String username, String password, String jdbcUrl, String tableName) {
    super(username, password, jdbcUrl, tableName);
  }

  public PostgresChatMemory(Connection connection) {
    super(connection);
  }

  public PostgresChatMemory(Connection connection, String tableName) {
    super(connection, tableName);
  }

  @Override
  protected String jdbcType() {
    return JDBC_TYPE;
  }

  @Override
  protected String hasTableSql(String tableName) {
    return String.format(
        "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE' AND table_name LIKE '%s'",
        tableName);
  }

  @Override
  protected String createTableSql(String tableName) {
    return String.format(
        "CREATE TABLE %s ( id BIGSERIAL PRIMARY KEY, conversation_id VARCHAR(256), messages TEXT, type VARCHAR(100));",
        tableName);
  }

}

PostgresChatMemory继承了JdbcChatMemory,其jdbcType为postgresql

SQLiteChatMemory

community/memories/spring-ai-alibaba-jdbc-memory/src/main/java/com/alibaba/cloud/ai/memory/jdbc/SQLiteChatMemory.java

public class SQLiteChatMemory extends JdbcChatMemory {

  private static final String JDBC_TYPE = "sqlite";

  public SQLiteChatMemory(String username, String password, String jdbcUrl) {
    super(username, password, jdbcUrl);
  }

  public SQLiteChatMemory(String username, String password, String jdbcUrl, String tableName) {
    super(username, password, jdbcUrl, tableName);
  }

  public SQLiteChatMemory(Connection connection) {
    super(connection);
  }

  public SQLiteChatMemory(Connection connection, String tableName) {
    super(connection, tableName);
  }

  @Override
  protected String jdbcType() {
    return JDBC_TYPE;
  }

  @Override
  protected String hasTableSql(String tableName) {
    return String.format("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%s'", tableName);
  }

  @Override
  protected String createTableSql(String tableName) {
    return String.format(
        "CREATE TABLE IF NOT EXISTS %s ( id INTEGER PRIMARY KEY AUTOINCREMENT, conversation_id TEXT, messages TEXT, type TEXT);",
        tableName);
  }

}

SQLiteChatMemory继承了JdbcChatMemory,其jdbcType为sqlite

SqlServerChatMemory

community/memories/spring-ai-alibaba-jdbc-memory/src/main/java/com/alibaba/cloud/ai/memory/jdbc/SqlServerChatMemory.java

public class SqlServerChatMemory extends JdbcChatMemory {

  private static final String JDBC_TYPE = "sqlserver";

  public SqlServerChatMemory(String username, String password, String url) {
    super(username, password, url);
  }

  public SqlServerChatMemory(String username, String password, String jdbcUrl, String tableName) {
    super(username, password, jdbcUrl, tableName);
  }

  public SqlServerChatMemory(Connection connection) {
    super(connection);
  }

  public SqlServerChatMemory(Connection connection, String tableName) {
    super(connection, tableName);
  }

  @Override
  protected String jdbcType() {
    return JDBC_TYPE;
  }

  @Override
  protected String hasTableSql(String tableName) {
    return String.format("SELECT name FROM sys.tables WHERE name LIKE '%s';", tableName);
  }

  @Override
  protected String createTableSql(String tableName) {
    return String.format(
        "CREATE TABLE %s (id BIGINT IDENTITY(1,1) PRIMARY KEY, conversation_id NVARCHAR(256), messages NVARCHAR(MAX), type VARCHAR(100));",
        tableName);
  }

  @Override
  protected String generatePaginatedQuerySql(String tableName, int lastN) {
    StringBuilder sqlBuilder = new StringBuilder("SELECT ");
    if (lastN > 0) {
      sqlBuilder.append("TOP ").append(lastN).append(" ");
    }
    sqlBuilder.append("messages,type FROM ").append(tableName).append(" WHERE conversation_id = ?");
    return sqlBuilder.toString();
  }

}

SqlServerChatMemory继承了JdbcChatMemory,其jdbcType为sqlserver,它还额外覆盖了generatePaginatedQuerySql方法

示例

community/memories/spring-ai-alibaba-jdbc-memory/src/test/java/com/alibaba/cloud/ai/memory/jdbc/MysqlChatMemoryTest.java

class MysqlChatMemoryTest {

  @Test
  public void test() {
    MysqlChatMemory mock = mock(MysqlChatMemory.class);
    Assertions.assertNotNull(mock);
  }

  // @Test
  public void mysql() {
    MysqlChatMemory chatMemory = new MysqlChatMemory("root", "123456",
        "jdbc:mysql://127.0.0.1:3306/spring_ai_alibaba_chat_memory");
    String apiKey = System.getenv().getOrDefault("AI_DASHSCOPE_API_KEY", "test-api-key");
    ChatClient chatClient = ChatClient.create(new DashScopeChatModel(new DashScopeApi(apiKey)));
    String content1 = chatClient.prompt()
      .advisors(new MessageChatMemoryAdvisor(chatMemory))
      .system("你是一个AI聊天小助手,给人提供情绪价值。")
      .user("我是张三")
      .call()
      .content();
    System.out.println(content1);
    String content2 = chatClient.prompt()
      .advisors(new MessageChatMemoryAdvisor(chatMemory))
      .user("我是谁")
      .call()
      .content();
    System.out.println(content2);
    Assertions.assertTrue(content2.contains("张三"));
  }

小结


spring-ai-alibaba-jdbc-memory提供了ChatMemory的jdbc实现,具体有MysqlChatMemory、OracleChatMemory、PostgresChatMemory、SQLiteChatMemory、SqlServerChatMemory这几种。

doc

  • java2ai

相关推荐

30天学会Python编程:16. Python常用标准库使用教程

16.1collections模块16.1.1高级数据结构16.1.2示例...

强烈推荐!Python 这个宝藏库 re 正则匹配

Python的re模块(RegularExpression正则表达式)提供各种正则表达式的匹配操作。...

Python爬虫中正则表达式的用法,只讲如何应用,不讲原理

Python爬虫:正则的用法(非原理)。大家好,这节课给大家讲正则的实际用法,不讲原理,通俗易懂的讲如何用正则抓取内容。·导入re库,这里是需要从html这段字符串中提取出中间的那几个文字。实例一个对...

Python数据分析实战-正则提取文本的URL网址和邮箱(源码和效果)

实现功能:Python数据分析实战-利用正则表达式提取文本中的URL网址和邮箱...

python爬虫教程之爬取当当网 Top 500 本五星好评书籍

我们使用requests和re来写一个爬虫作为一个爱看书的你(说的跟真的似的)怎么能发现好书呢?所以我们爬取当当网的前500本好五星评书籍怎么样?ok接下来就是学习python的正确姿...

深入理解re模块:Python中的正则表达式神器解析

在Python中,"re"是一个强大的模块,用于处理正则表达式(regularexpressions)。正则表达式是一种强大的文本模式匹配工具,用于在字符串中查找、替换或提取特定模式...

如何使用正则表达式和 Python 匹配不以模式开头的字符串

需要在Python中使用正则表达式来匹配不以给定模式开头的字符串吗?如果是这样,你可以使用下面的语法来查找所有的字符串,除了那些不以https开始的字符串。r"^(?!https).*&...

先Mark后用!8分钟读懂 Python 性能优化

从本文总结了Python开发时,遇到的性能优化问题的定位和解决。概述:性能优化的原则——优化需要优化的部分。性能优化的一般步骤:首先,让你的程序跑起来结果一切正常。然后,运行这个结果正常的代码,看看它...

Python“三步”即可爬取,毋庸置疑

声明:本实例仅供学习,切忌遵守robots协议,请不要使用多线程等方式频繁访问网站。#第一步导入模块importreimportrequests#第二步获取你想爬取的网页地址,发送请求,获取网页内...

简单学Python——re库(正则表达式)2(split、findall、和sub)

1、split():分割字符串,返回列表语法:re.split('分隔符','目标字符串')例如:importrere.split(',','...

Lavazza拉瓦萨再度牵手上海大师赛

阅读此文前,麻烦您点击一下“关注”,方便您进行讨论和分享。Lavazza拉瓦萨再度牵手上海大师赛标题:2024上海大师赛:网球与咖啡的浪漫邂逅在2024年的上海劳力士大师赛上,拉瓦萨咖啡再次成为官...

ArkUI-X构建Android平台AAR及使用

本教程主要讲述如何利用ArkUI-XSDK完成AndroidAAR开发,实现基于ArkTS的声明式开发范式在android平台显示。包括:1.跨平台Library工程开发介绍...

Deepseek写歌详细教程(怎样用deepseek写歌功能)

以下为结合DeepSeek及相关工具实现AI写歌的详细教程,涵盖作词、作曲、演唱全流程:一、核心流程三步法1.AI生成歌词-打开DeepSeek(网页/APP/API),使用结构化提示词生成歌词:...

“AI说唱解说影视”走红,“零基础入行”靠谱吗?本报记者实测

“手里翻找冻鱼,精心的布局;老漠却不言语,脸上带笑意……”《狂飙》剧情被写成歌词,再配上“科目三”背景音乐的演唱,这段1分钟30秒的视频受到了无数网友的点赞。最近一段时间随着AI技术的发展,说唱解说影...

AI音乐制作神器揭秘!3款工具让你秒变高手

在音乐创作的领域里,每个人都有一颗想要成为大师的心。但是面对复杂的乐理知识和繁复的制作过程,许多人的热情被一点点消磨。...

取消回复欢迎 发表评论: