dco-ai/php-jina

Jina-ai 框架的 PHP 客户端。

v1.1.6 2023-04-18 19:06 UTC

This package is auto-updated.

Last update: 2024-09-18 22:00:47 UTC


README

一个用于通过 PHP 连接到 Jina 的工具。此客户端在没有运行的 Jina 安装的情况下无法工作。

要查看如何设置,请访问此处: Jina 安装

Jina 文档

有关 Jina 的更多信息,请访问此处: Jina

使用 composer 命令安装

    composer require dco-ai/php-jina

使用 composer.json 安装

直接从 GitHub 安装

将其添加到您的 composer.json 文件中,或者创建该文件并将此内容放入其中。

{
  "name":  "dco-ai/php-jina",
  "repositories": [
    {
      "type": "svn",
      "url": "https://github.com/Dco-ai/php-jina.git"
    }
  ],
  "require": {
    "dco-ai/php-jina": "main"
  }
}

或从 Packagist 安装

{
  "require": {
    "dco-ai/php-jina": "v1.*"
  }
}

现在运行 composer update

配置

此客户端需要了解一些关于您的 Jina 项目的信息才能建立连接。

配置是一个关联数组,包含以下字段

用法

一个简单的示例是 src/example.php。这显示了如何加载类,然后创建/更新 Jina 的 Document 和 DocumentArray 结构。

首先在头部包含此包

<?php
use DcoAi\PhpJina\JinaClient;

然后使用您的配置实例化 JinaClient 类

$config = [
    "url" => "localhost", // The URL or endpoint of your Jina installation
    "port" => "1234", // The port used for your Jina Installation
    "endpoints" => [ // These are the active endpoints in your Jina application with the corresponding method
        "/status" => "GET",
        "/post" => "POST",
        "/index" => "POST",
        "/search" => "POST",
        "/delete" => "DELETE",
        "/update" => "PUT",
        "/" => "GET"
    ]
];
$jina = new JinaClient($config);

现在您可以使用这些函数

// this creates a Document that you can add data to the structure
$d = $jina->document();

// This creates a DocumentArray that Documents can be added to
$da = $jina->documentArray();

// This adds Documents to a DocumentArray
$jina->addDocument($da, $d);

// This sends the DocumentArray to your JinaClient application and returns the result.
$jina->submit("/index",$da);

结构

Document

DocumentArray

过滤器

过滤器是 DocArray 中每个数据存储所独有的。结构和它们的传递方式取决于您如何设置 Executors。对于我提供的每个示例,我假设您的 Executors 接受请求参数部分的 filter 键。如果您的 Executors 以不同的方式接受过滤器,您需要相应地修改请求。

在此客户端中,您可以通过链式调用过滤器函数来构建一个过滤器。首先,您必须使用 useFilterFormatter() 函数创建一个 Filter 类的实例。

use DcoAi\PhpJina\JinaClient;
// set the config and create a new instance of the JinaClient
$config = [
    "url" => "localhost",
    "port" => "1234",
    "endpoints" => [
        "/status" => "GET",
        "/post" => "POST",
        "/index" => "POST",
        "/search" => "POST",
        "/delete" => "DELETE",
        "/update" => "PUT",
        "/" => "GET",
    ]
];
$jina = new JinaClient($config);

// create a new instance of the filter class
$filterBuilder = $jina->useFilterFormatter();

现在您有了过滤器,这是如何链式调用一个基本的过滤器

$filterBuilder->
    and()->
        equal("env","dev")->
        equal("userId","2")->
    endAnd()->
    or()->
        notEqual("env","2")->
        greaterThan("id","5")->
    endOr()->
    equal("env","dev")->
    notEqual("env","prod");

某些数据存储将具有分组运算符(如 andor),允许您将过滤器组合在一起。如果数据存储有这些运算符,将有一个与打开函数相对应的关闭函数。

一旦您构建了过滤器,您需要从 Filter 类中检索它并将其添加到请求中。这不是自动完成的。

// Lets make an empty DocumentArray
$da = $jina->documentArray();
// And add the filter to the parameters 
$da->parameters->filter = $filterBuilder->createFilter();
// print ths document and see what we got
print_r(json_encode($da, JSON_PRETTY_PRINT));

此过滤器将生成如下字符串

{
    "data": [],
    "parameters": {
        "filter": [
            {
                "$and": [
                    {
                        "env": {
                            "$eq": "dev"
                        }
                    },
                    {
                        "userId": {
                            "$eq": "2"
                        }
                    }
                ]
            },
            {
                "$or": [
                    {
                        "env": {
                            "$ne": "2"
                        }
                    },
                    {
                        "id": {
                            "$gt": 5
                        }
                    }
                ]
            },
            {
                "env": {
                    "$eq": "dev"
                }
            },
            {
                "env": {
                    "$ne": "prod"
                }
            }
        ]
    }
}

此示例有些复杂且可能没有用,但它显示了可以做什么。

默认过滤器

DocArray 有一个默认过滤器结构,可以在此客户端中直接使用,无需任何配置更改。文档可以在此处找到: 文档

以下是默认过滤器支持的运算符列表。 $column 是您要过滤的字段,而 $value 是您要过滤的值。

默认过滤器的组合函数列表在此

AnnLite 过滤器

此过滤器使用 AnnLite 数据存储,与默认过滤器非常相似,但有细微差别。文档可以在此处找到: 文档

要使用此过滤器,您必须在配置中的 dataStore 数组中添加 "type" => "annlite" 键。

use DcoAi\PhpJina\JinaClient;
// set the config and create a new instance of the JinaClient
$config = [
    "url" => "localhost",
    "port" => "1234",
    "endpoints" => [
        "/status" => "GET",
        "/post" => "POST",
        "/index" => "POST",
        "/search" => "POST",
        "/delete" => "DELETE",
        "/update" => "PUT",
        "/" => "GET",
    ],
    "dataStore" => [
        "type" => "annlite",
    ]
];
$jina = new JinaClient($config);

与其他所有过滤器一样,您可以使用链式方法构建它。以下是使用此数据存储的具体字段

默认过滤器的组合函数列表在此

Weaviate 过滤器

此过滤器使用 Weaviate 数据存储并使用 GraphQL 作为查询语言。由于此语言依赖于数据库中的模式,我们需要连接到您的 Weaviate 实例并检索模式以构建查询。这会自动完成,但您需要在创建 JinaClient 实例时添加 urlport 参数。文档可在此处找到:文档

要使用此过滤器,您必须在配置中的 dataStore 数组中添加 "type" => "weaviate" 键。

use DcoAi\PhpJina\JinaClient;
// set the config and create a new instance of the JinaClient
$config = [
    "url" => "localhost",
    "port" => "1234",
    "endpoints" => [
        "/status" => "GET",
        "/post" => "POST",
        "/index" => "POST",
        "/search" => "POST",
        "/delete" => "DELETE",
        "/update" => "PUT",
        "/" => "GET",
    ],
    "dataStore" => [
        "type" => "weaviate",
        "url" => "localhost",
        "port" => "8080",
    ]
];
$jina = new JinaClient($config);

以下是使用此数据存储的具体字段

默认过滤器的组合函数列表在此

所有其他数据存储过滤器

目前,这些不受支持,但计划在未来版本中提供。如果您想为此项目做出贡献,请随时提交一个拉取请求,并就任何问题与我联系。

响应

为了在调用您的 Jina 应用程序时节省数据传输和内存,此客户端将通过删除任何未设置值的键来自动清理请求和响应。在进行响应评估时请记住这一点,首先检查键是否存在。

如果您想返回所有值,可以在使用 submit() 函数时设置一个标志。

// setting the third parameter to false will not remove any empty values from the response
$jina->submit("/index", $da, false);