mariadb(MariaDB 矢量版:专为 AI 设计)

时间:2025-01-26 23:06:09 阅读:8

MariaDB 矢量版:专为 AI 计划

在专门构建的人工智能数据库年代,像 MariaDB 如此的传统数据库怎样重塑本身以坚持干系性?在这篇中找出答案。

作为一名在干系数据库体系方面拥有二十多年履历的处理方案架构师,我迩来开头探究 MariaDB 的新矢量版本,看看它对否可以处理我们面临的一些人工智能数据挑唆。快速欣赏一下仿佛十分有压服力,尤其是它怎样将人工智能邪术直接带入常规数据库设置中。但是,我想用一个简便的用例来测试它,看看它在实践中的体现怎样。

在本文中,我将经过运转一个简便的用例来分享我对 MariaDB向量功效的实践履历和察看。具体来说,我将把示例客户批评加载到 MariaDB 中,并实行快速相似性搜刮来查找干系批评。

情况设置

  • Python 3.10 或更高版本
  • Docker 桌面

我的实行从使用包含矢量功效的 MariaDB最新版本 (11.6)设置Docker容器开头。

# Pull the latest release docker pull quay.io/mariadb-foundation/mariadb-devel:11.6-vector-preview # Update password docker run -d --name mariadb_vector -e MYSQL_ROOT_PASSWORD=<replace_password> quay.io/mariadb-foundation/mariadb-devel:11.6-vector-preview

如今,创建一个表并加载示例客户批评,此中包含每个批评的心情评分和嵌入。为了天生文本嵌入,我使用SentenceTransformer ,它允许您使用事后练习的模子。具体来说,我决定使用一个名为 paraphrase-MiniLM-L6-v2 的模子,该模子获取我们的客户批评并将其映射到 384 维空间。

import mysql.connector import numpy as np from sentence_transformers import SentenceTransformer model = SentenceTransformer('paraphrase-MiniLM-L6-v2') # I already have a database created with a name vectordb connection = mysql.connector.connect( host="localhost", user="root", password="<password>", # Replace me database="vectordb" ) cursor = connection.cursor() # Create a table to store customer reviews with sentiment score and embeddings. cursor.execute(""" CREATE TABLE IF NOT EXISTS customer_reviews ( id INT PRIMARY KEY AUTO_INCREMENT, product_name INT, customer_review TEXT, customer_sentiment_score FLOAT, customer_review_embedding BLOB, INDEX vector_idx (customer_review_embedding) USING HNSW ) ENGINE=ColumnStore; """) # Sample reviews reviews = [ (1, "This product exceeded my expectations. Highly recommended!", 0.9), (1, "Decent quality, but pricey.", 0.6), (2, "Terrible experience. The product does not work.", 0.1), (2, "Average product, ok ok", 0.5), (3, "Absolutely love it! Best purchase I have made this year.", 1.0) ] # Load sample reviews into vector DB for product_id, review_text, sentiment_score in reviews: embedding = model.encode(review_text) cursor.execute( "INSERT INTO customer_reviews (product_id, review_text, sentiment_score, review_embedding) VALUES (%s, %s, %s, %s)", (product_id, review_text, sentiment_score, embedding.tobytes())) connection.commit() connection.close()

如今,让我们使用 MariaDB 的矢量功效来查找相似的批评。这更像是在问“其他主顾也说过相似的批评吗? ”。在底下的示例中,我将找到相似于“我十分满意! ”的客户批评的前 2 条批评。为此,我使用最新版本中提供的矢量函数 ( VEC_Distance_Euclidean ) 之一。

# Convert the target customer review into vector target_review_embedding = model.encode("I am super satisfied!") # Find top 2 similar reviews using MariaDB's VEC_Distance_Euclidean function cursor.execute(""" SELECT review_text, sentiment_score, VEC_Distance_Euclidean(review_embedding, %s) AS similarity FROM customer_reviews ORDER BY similarity LIMIT %s """, (target_review_embedding.tobytes(), 2)) similar_reviews = cursor.fetchall()

察看后果

  • 它很容易设置,我们可以将布局化数据(如产物 ID 和心情分数)、非布局化数据(批评文本)及其向量表现情势组合在一个表中。
  • 我喜好它使用 SQL 语法和向量运算的才能,这使得以前熟习干系数据库的团队可以轻松使用。以下是此版本中支持的向量函数的完备列表。
  • HNSW 索引提高了我迄今为止实验过的较大数据集的相似性搜刮查询的功能。

结论

总的来说,我印象深入! MariaDB 的矢量版将简化某些人工智能驱动的架构。它弥合了传统数据库天下与人工智能东西不休提高的需求之间的差距。在接下去的几个月中,我渴望看到这项武艺怎样成熟以及社区如安在实践使用中接纳它。

版权声明:本文来自互联网整理发布,如有侵权,联系删除

原文链接:https://www.yigezhs.comhttps://www.yigezhs.com/shenghuojineng/57964.html


Copyright © 2021-2022 All Rights Reserved 备案编号:闽ICP备2023009674号 网站地图 联系:dhh0407@outlook.com