partitech/php-tei-client

用于从Hugging Face进行文本嵌入推理的简单PHP客户端

0.0.1 2024-07-19 19:54 UTC

This package is auto-updated.

Last update: 2024-09-20 21:52:45 UTC


README

这是一个非常简单的PHP客户端,用于与Hugging Face的文本嵌入推理(TEI)服务器交互,这是一个用于文本嵌入模型的快速推理解决方案。此客户端允许您轻松地与TEI服务器的嵌入、重新排序和预测API端点进行交互。

安装

您可以通过Composer安装此包

composer require partitech/php-tei-client

用法

以下是使用TEI客户端的基本示例

嵌入

<?php

use Partitech\PhpTeiClient\TeiClient;

$client = new TeiClient(url: 'https://:8080', apiKey: 'yourApiKey');

// Embed a single string
$result = $client->embed(content: 'What is Deep Learning?');

// Embed an array of strings
$contents = ['What is Deep Learning?', 'What is Machine Learning?'];
$result = $client->embed(content: $contents);

结果

Array
(
    [0] => Array
        (
            [0] => 0.007737029
            [1] => -0.06754478
            [2] => -0.0380035
-----
            [1023] => 0.026126498
        )

)

使用重新排序器

<?php

use Partitech\PhpTeiClient\TeiClient;

$client = new TeiClient(url: 'https://:8080', apiKey: 'yourApiKey');


// Rerank an array of texts based on a query
$content = ['Deep learning is...', 'cheese is made of', 'Deep Learning is not...'];
$result = $client->rerank('What is the difference between Deep Learning and Machine Learning?', $content);

结果

Array
(
    [0] => Array
        (
            [index] => 0
            [score] => 0.94238955
        )

    [1] => Array
        (
            [index] => 2
            [score] => 0.120219156
        )

    [2] => Array
        (
            [index] => 1
            [score] => 3.7323738E-5
        )

)

您还可以获取相关文本并在结果中限制为前n个结果

<?php

use Partitech\PhpTeiClient\TeiClient;

$client = new TeiClient(url: 'https://:8080', apiKey: 'yourApiKey');


// Rerank an array of texts based on a query
$content = ['Deep learning is...', 'cheese is made of', 'Deep Learning is not...'];
$result = $client->getRerankedContent('What is the difference between Deep Learning and Machine Learning?', $content);

结果

Array
(
    [0] => Array
        (
            [index] => 0
            [score] => 0.94238955
            [content] => Deep learning is...
        )

    [1] => Array
        (
            [index] => 2
            [score] => 0.120219156
            [content] => Deep Learning is not...
        )

)

使用序列分类

<?php

use Partitech\PhpTeiClient\TeiClient;

$client = new TeiClient(url: 'https://:8080', apiKey: 'yourApiKey');

// Predict the sentiment of a string
$result = $client->predict('I love this product!');

结果

Array
(
    [0] => Array
        (
            [score] => 0.986059
            [label] => love
        )

    [1] => Array
        (
            [score] => 0.006502793
            [label] => admiration
        )

    [2] => Array
        (
            [score] => 0.0020027023
            [label] => approval
        )

    [3] => Array
        (
            [score] => 0.0008381181
            [label] => neutral
        )

    [4] => Array
        (
            [score] => 0.0005737838
            [label] => joy
        )
---------

    [27] => Array
        (
            [score] => 2.2074879E-5
            [label] => grief
        )

)

使用SPLADE池化

<?php

use Partitech\PhpTeiClient\TeiClient;

$client = new TeiClient(url: 'https://:8080', apiKey: 'yourApiKey');
$result = $client->embedSparse(content: 'What is Deep Learning?');

结果

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [index] => 1012
                    [value] => 1.0751953
                )

            [1] => Array
                (
                    [index] => 2003
                    [value] => 1.5722656
                )

            [2] => Array
                (
                    [index] => 2784
                    [value] => 2.9082031
                )

            [3] => Array
                (
                    [index] => 4083
                    [value] => 2.7929688
                )

        )

)

贡献

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

请确保适当更新测试。

许可

MIT