islambaraka90/pinecone-php-client

Pinecone DB 连接的 PHP 客户端

0.1.1 2023-04-10 01:58 UTC

This package is auto-updated.

Last update: 2024-09-10 23:46:55 UTC


README

这是一个处理所有 Pinecone 端点的 PHP 客户端实现。创建这个库是因为没有现成的 Pinecone 矢量数据库 PHP 客户端。

Pinecone 矢量数据库

Pinecone 是一个为高性能相似搜索和其他相似任务设计的矢量数据库。它提供了一个简单的 API 用于插入和查询高维矢量,使得构建能够处理大量数据的智能搜索应用变得容易。

Pinecone 被OpenAI应用作为其长期记忆架构的一部分使用。通过在 Pinecone 中存储高维矢量,OpenAI 应用可以在需要时快速有效地检索它们,从而构建能够从大量数据中学习并做出更准确预测的复杂模型。

Pinecone 的矢量数据库具有高度可扩展性,可以轻松处理数百万甚至数十亿个矢量。它设计用于与各种机器学习框架和库协同工作,使其易于集成到现有的工作流程中。

使用 Pinecone 的一个关键优势是它能够执行快速、准确的相似搜索。这使得它非常适合图像搜索、产品推荐和个性化内容传递等应用。通过使用 Pinecone,您可以构建能够处理大量数据并在实时提供准确结果的智能搜索应用。

总的来说,Pinecone 是任何需要处理高维矢量并希望构建能够处理大量数据的智能搜索应用的人的强大工具。无论是构建推荐引擎、图像搜索应用还是个性化内容传递系统,Pinecone 都可以帮助您快速轻松地实现目标。

重要提示

此库不仅限于 OpenAI 嵌入,也可以与任何其他嵌入一起使用。

在使用此库之前,请确保检查嵌入的维度。

安装

要安装 Pinecone PHP 客户端,您可以使用 Composer。运行以下命令

composer require islambaraka90/pinecone-php-client

用法

初始化

要使用 Pinecone PHP 客户端,您需要首先使用您的 API 密钥和 Pinecone 端点初始化它。您可以创建一个新的 Pinecone 客户端实例来完成此操作

use IslamBaraka90\PineconeClient;

$this->api_key =  $_ENV['PINCONE_KEY'] ?: '';
$this->environment = $_ENV['PINCONE_ENV'] ?: 'us-east1-gcp';
$this->client = new PineconePhpClient( $this->api_key ,  $this->environment );

列出索引

要列出您 Pinecone 实例中所有可用的索引,您可以使用 listIndexes 方法

$response = $this->client->listIndexes();

创建索引

要创建新的索引,您可以使用 createIndex 方法

$name = 'my-index';
$dimension = 1536;
$result = $this->client->createIndex($name, $dimension);

删除索引

要删除索引,您可以使用 deleteIndex 方法

$response = $this->client->deleteIndex('my-index');

描述索引

要获取有关索引的信息,您可以使用 describeIndex 方法

$this->client->describeIndex('my-index');

使用索引

要使用特定索引,您可以使用 Index 方法获取 Index 接口的实例。以下是在 Index 接口上可用的方法的一些示例

描述索引统计信息

要获取有关索引的统计信息,您可以使用 describeIndexStats 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

插入或更新矢量

要在一个索引中插入或更新矢量,您可以使用 upsert 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

// Upsert 20 vectors
$vectorsFile = __DIR__ . '/vectors_objects.json';
$vectors = json_decode(file_get_contents($vectorsFile), true);
$project_id = 'project-0000'.rand(1,99);
$response = $index->upsert($project_id,$vectors['vectors']);

查询矢量

要从一个索引中查询矢量,您可以使用 query 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

// Query vectors
$vectorsFile = __DIR__ . '/query.json';
$vectors = json_decode(file_get_contents($vectorsFile), true);
$response = $index->query('project-000001',$vectors, [],3);
$response = json_decode($response, true);

获取矢量

要从索引中获取向量,您可以使用 fetch 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

// Fetch vectors
$id = "DEeSvRP8XKZwzpWD";
$response = $index->fetch([$id], 'project-000001');
$response = json_decode($response, true);

更新向量

要更新索引中的向量,您可以使用 update 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

// Update vectors
$id = "DEeSvRP8XKZwzpWD";
$response = $index->update($id, 'project-000001',null,null,['testing' => 'testing5']);
$response = json_decode($response, true);

删除向量

要从索引中删除向量,您可以使用 delete 方法

$index = $this->client->Index('my-index');
$state = $index->describeIndexStats();

// Delete vectors
$id = "DEeSvRP8XKZwzpWD";
$response = $index->delete($id, 'project-000001');

贡献

欢迎提交拉取请求。对于重大更改,请先创建一个问题来讨论您想要更改的内容。

请确保适当地更新测试。

许可证

MIT