你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在 Azure Database for PostgreSQL 灵活服务器中使用本地部署的 LLM 生成矢量嵌入(预览版)
先决条件
在内存优化的 VM SKU 上运行的 Azure Database for PostgreSQL 灵活服务器实例。 在此处详细了解 Azure 内存优化 VM:Azure VM 大小 - 内存 - Azure 虚拟机
你可能需要启用矢量扩展,因为它提供了在数据库中存储文本嵌入和高效地为其编制索引的功能。
启用 扩展
你需要将 azure_local_ai
扩展列入允许列表(如允许扩展中所述),然后才能在 Azure Database for PostgreSQL 灵活服务器实例上启用 azure_local_ai。
重要
在数据库中托管语言模型需要占用大量内存。 为了支持此要求,azure_local_ai
仅支持至少具有 4 个 vCore 的内存优化型 Azure VM SKU。
如果你使用的 VM 不符合最低要求,azure_local_ai
扩展将不会显示在 azure.extensions
服务器参数的允许值列表中。
将扩展列入允许列表后,可以遵循创建扩展中提供的说明,在每个要使用扩展的数据库中安装该扩展。
注意
启用 Azure Local AI 会将 multilingual-e5-small 模型部署到 Azure Database for PostgreSQL 灵活服务器实例。 链接的文档提供了来自 e5 团队的许可条款。 其他第三方开放源代码模型可能会持续地可供安装。
安装 azure_local_ai
扩展会创建一个名为 azure_local_ai
的架构,扩展在其中存储表、函数以及实现和公开其功能所需的任何其他 SQL 相关对象。
重要
你可能需要启用矢量扩展,因为在 PostgreSQL 数据库中存储文本嵌入需要用到它。
扩展提供的函数
azure_local_ai
扩展提供了一组函数。 使用这些函数,你可以从文本数据创建矢量嵌入,从而更轻松地开发生成式 AI 应用程序。 该扩展提供了用于创建嵌入的函数、用于获取设置的函数,以及其他函数。 使用这些函数,你无需对托管在 PostgreSQL 边界之外的 AI 嵌入模型进行额外的远程 API 调用,因此可以简化开发过程并降低延迟。
架构 | 名称 | 结果数据类型 | 参数数据类型 |
---|---|---|---|
azure_local_ai | create_embeddings | TABLE(embedding real[]) | model_uri text, inputs text[], batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | create_embeddings | real[] | model_uri text, input text, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | get_setting | jsonb | keys text[] DEFAULT ARRAY[]::text[], timeout_ms integer DEFAULT 3600000 |
azure_local_ai | get_setting | text | key text, timeout_ms integer DEFAULT 3600000 |
azure_local_ai | model_metadata | jsonb | model_uri text |
可以使用以下 psql 元命令显示这些函数:
\df azure_local_ai.*
azure_local_ai.create_embeddings
使用 azure_local_ai
扩展,可以用标量和批处理格式创建和更新嵌入,从而调用本地部署的 LLM。
azure_local_ai.create_embeddings(model_uri text, input text, batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000);
azure_local_ai.create_embeddings(model_uri text, array[inputs [text]], batch_size bigint DEFAULT 128, timeout_ms integer DEFAULT 3600000);
参数
model_uri
调用后可以创建嵌入项的文本嵌入模型的 text
名称。
input
text
或 text[]
单个文本或文本数组,具体取决于为其创建嵌入的所用函数的重载。
batch_size
bigint DEFAULT 128
一次要处理的记录数(仅适用于参数 input
为 text[]
类型的函数的重载)。
timeout_ms
操作停止之前的 integer DEFAULT 3600000
超时(以毫秒为单位)。
azure_local_ai.get_setting
用于获取配置选项的当前值。
SELECT azure_local_ai.get_setting(key TEXT)
azure_local_ai
支持在 ONNX 运行时服务中查看 ONNX 运行时线程池的配置参数。 目前不允许进行更改。 请参阅 ONNX 运行时性能优化。
参数
密钥
有效值为:
intra_op_parallelism
:设置由 ONNX 运行时线程池用来并行化单个运算符的线程的总数。 默认情况下,我们会尽可能最大化操作内线程的数量,因为它可以大大提高整体吞吐量(默认情况下所有可用的CPU)。inter_op_parallelism
:设置由 ONNX 运行时线程池用来并行计算多个运算符的线程的总数。 默认情况下,我们将其设置为最小可能线程数,即 1。 由于线程之间存在频繁的上下文切换,增加它通常会损害性能。spin_control
:切换 ONNX 运行时线程池的请求旋转。 禁用时,它使用较少的 CPU,因此会导致更高的延迟。 它默认设置为 true(启用)。
返回类型
TEXT
表示所选设置的当前值。
示例
从现有文本创建嵌入内容
下面是你可以在自己的环境中使用的示例,它们用于测试使用本地部署的 multilingual-e5 模型进行的嵌入内容生成。
-- Create documents table
CREATE TABLE documents(doc_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, doc_contents TEXT NOT NULL, last_update TIMESTAMPTZ DEFAULT now());
--Insert data into the docs table
INSERT INTO documents(doc_contents) VALUES
('Create in-database embeddings with azure_local_ai extension.'),
('Enable RAG patterns with in-database embeddings and vectors on Azure Database for PostgreSQL - Flexible server.'),
('Generate vector embeddings in PostgreSQL with azure_local_ai extension.'),
('Generate text embeddings in PostgreSQL for retrieval augmented generation (RAG) patterns with azure_local_ai extension and locally deployed LLM.'),
('Use vector indexes and Azure OpenAI embeddings in PostgreSQL for retrieval augmented generation.');
-- Add a vector column and generate vector embeddings from locally deployed model
ALTER TABLE documents
ADD COLUMN doc_vector vector(384) -- multilingual-e5 embeddings are 384 dimensions
GENERATED ALWAYS AS (azure_local_ai.create_embeddings('multilingual-e5-small:v1', doc_contents)::vector) STORED; -- TEXT string sent to local model
--View floating point entries in the doc_vector column
SELECT doc_vector FROM documents;
-- Add a single record to the documents table and the vector embedding using azure_local_ai and locally deployed model will be automatically generated
INSERT INTO documents(doc_contents) VALUES
('Semantic Search with Azure Database for PostgreSQL - Flexible Server and Azure OpenAI');
--View all document entries, their contents, embeddings and last time the row was updated
SELECT doc_contents, doc_vector, last_update FROM documents;
-- The following command leverages the overload of azure_local_ai.create_embeddings function which accepts and array of TEXT
-- and produces a table for which each row contains the embedding of one element in the input array
SELECT azure_local_ai.create_embeddings('multilingual-e5-small:v1', array['Recommendation System with Azure Database for PostgreSQL - Flexible Server and Azure OpenAI.', 'Generative AI with Azure Database for PostgreSQL - Flexible Server.']);
在插入新文本时生成嵌入内容
下面是你可以在自己的环境中使用的示例,它们用于测试使用本地部署的 multilingual-e5 模型进行的嵌入内容生成。
-- Create documents table
CREATE TABLE documents(doc_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, doc_contents TEXT NOT NULL, last_update TIMESTAMPTZ DEFAULT now(), doc_vector vector(384) GENERATED ALWAYS AS (azure_local_ai.create_embeddings('multilingual-e5-small:v1', doc_contents)::vector) STORED);
-- Insert data into the documents table
INSERT INTO documents(doc_contents) VALUES
('Create in-database embeddings with azure_local_ai extension.'),
('Enable RAG patterns with in-database embeddings and vectors on Azure Database for PostgreSQL - Flexible server.'),
('Generate vector embeddings in PostgreSQL with azure_local_ai extension.'),
('Generate text embeddings in PostgreSQL for retrieval augmented generation (RAG) patterns with azure_local_ai extension and locally deployed LLM.'),
('Use vector indexes and Azure OpenAI embeddings in PostgreSQL for retrieval augmented generation.');
-- Query embedding text, list results by descending similarity score
WITH all_documents AS (
SELECT doc_id, doc_contents, doc_vector FROM documents
),
target_documents AS (
SELECT azure_local_ai.create_embeddings('multilingual-e5-small:v1', 'Generate text embeddings in PostgreSQL.') doc_vector
)
SELECT all_documents.doc_id, all_docs.doc_contents , 1 - (all_documents.doc_vector::vector <=> target_documents.doc_vector::vector) AS similarity
FROM target_documents, all_documents
ORDER BY similarity DESC
LIMIT 2;
相关内容
- 将 Azure Database for PostgreSQL 灵活服务器与 Azure 认知服务集成。
- 将 Azure Database for PostgreSQL 与 Azure 机器学习服务集成。
- 在 Azure Database for PostgreSQL 灵活服务器中使用 Azure OpenAI 生成矢量嵌入。
- Azure Database for PostgreSQL 灵活服务器中的 Azure AI 扩展。
- 使用 Azure Database for PostgreSQL 灵活服务器的生成式 AI。
- 使用 Azure Database for PostgreSQL 灵活服务器和 Azure OpenAI 的建议系统。
- 使用 Azure Database for PostgreSQL 灵活服务器和 Azure OpenAI 进行语义搜索。
- 在 Azure Database for PostgreSQL 灵活服务器中启用并使用 pgvector。