nlpcloud / nlpcloud-client
NLP Cloud 提供高性能的预训练或自定义模型,包括命名实体识别、情感分析、分类、摘要、释义、语法和拼写纠正、关键词和短语提取、聊天机器人、产品描述和广告生成、意图分类、文本生成、图像生成、代码生成、问答、自动语音识别、机器翻译、语言检测、语义搜索、语义相似度、分词、词性标注、嵌入和依存句法分析。它适用于生产环境,通过 REST API 提供。
Requires
- php: >=7.2
- nategood/httpful: *
README
这是 NLP Cloud API 的 PHP 客户端。有关更多详细信息,请参阅 文档。
NLP Cloud 提供高性能的预训练或自定义模型,用于命名实体识别、情感分析、分类、摘要、对话摘要、释义、意图分类、产品描述和广告生成、聊天机器人、语法和拼写纠正、关键词和短语提取、文本生成、图像生成、代码生成、问答、自动语音识别、机器翻译、语言检测、语义搜索、语义相似度、分词、词性标注、嵌入和依存句法分析。它适用于生产环境,通过 REST API 提供。
您可以使用 NLP Cloud 的预训练模型,微调自己的模型,或部署自己的模型。
如果您遇到问题,请不要犹豫,将其作为一个 GitHub 问题提出。谢谢!
安装
通过 composer 安装。
创建一个包含以下内容的 composer.json
文件
{ "require": { "nlpcloud/nlpcloud-client": "*" } }
然后运行以下命令
composer install
示例
这是一个完整的示例,使用 Facebook 的 Bart 大型 CNN 模型对文本进行摘要,并使用了一个假令牌
require 'vendor/autoload.php'; use NLPCloud\NLPCloud; $client = new NLPCloud('bart-large-cnn', '<your token>'); echo json_encode($client->summarization('One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally gathering real steam. Close to a million doses -- over 951,000, to be more exact -- made their way into the arms of Americans in the past 24 hours, the U.S. Centers for Disease Control and Prevention reported Wednesday. That s the largest number of shots given in one day since the rollout began and a big jump from the previous day, when just under 340,000 doses were given, CBS News reported. That number is likely to jump quickly after the federal government on Tuesday gave states the OK to vaccinate anyone over 65 and said it would release all the doses of vaccine it has available for distribution. Meanwhile, a number of states have now opened mass vaccination sites in an effort to get larger numbers of people inoculated, CBS News reported.'));
这是一个完整的示例,在 GPU 上执行相同的操作
require 'vendor/autoload.php'; use NLPCloud\NLPCloud; $client = new NLPCloud('bart-large-cnn', '<your token>', True); echo json_encode($client->summarization('One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally gathering real steam. Close to a million doses -- over 951,000, to be more exact -- made their way into the arms of Americans in the past 24 hours, the U.S. Centers for Disease Control and Prevention reported Wednesday. That s the largest number of shots given in one day since the rollout began and a big jump from the previous day, when just under 340,000 doses were given, CBS News reported. That number is likely to jump quickly after the federal government on Tuesday gave states the OK to vaccinate anyone over 65 and said it would release all the doses of vaccine it has available for distribution. Meanwhile, a number of states have now opened mass vaccination sites in an effort to get larger numbers of people inoculated, CBS News reported.'));
这是一个完整的示例,在法语文本上执行相同的操作
require 'vendor/autoload.php'; use NLPCloud\NLPCloud; $client = new NLPCloud('bart-large-cnn', '<your token>', True, 'fra_Latn'); echo json_encode($client->summarization('Sur des images aériennes, prises la veille par un vol de surveillance de la Nouvelle-Zélande, la côte d’une île est bordée d’arbres passés du vert au gris sous l’effet des retombées volcaniques. On y voit aussi des immeubles endommagés côtoyer des bâtiments intacts. « D’après le peu d’informations dont nous disposons, l’échelle de la dévastation pourrait être immense, spécialement pour les îles les plus isolées », avait déclaré plus tôt Katie Greenwood, de la Fédération internationale des sociétés de la Croix-Rouge. Selon l’Organisation mondiale de la santé (OMS), une centaine de maisons ont été endommagées, dont cinquante ont été détruites sur l’île principale de Tonga, Tongatapu. La police locale, citée par les autorités néo-zélandaises, a également fait état de deux morts, dont une Britannique âgée de 50 ans, Angela Glover, emportée par le tsunami après avoir essayé de sauver les chiens de son refuge, selon sa famille.'));
返回一个 JSON 对象
{ "summary_text": "Over 951,000 doses were given in the past 24 hours. That's the largest number of shots given in one day since the rollout began. That number is likely to jump quickly after the federal government gave states the OK to vaccinate anyone over 65. A number of states have now opened mass vaccination sites." }
用法
客户端初始化
在初始化时将您要使用的模型和 NLP Cloud 令牌传递给客户端。
模型可以是预训练模型,如 en_core_web_lg
、bart-large-mnli
等,也可以是您的自定义模型,使用 custom_model/<model id>
(例如 custom_model/2568
)。
您的令牌可以从您的 NLP Cloud 控制台 获取。
use NLPCloud\NLPCloud; $client = new NLPCloud('<model>','<your token>');
如果您想使用 GPU,请将 true
作为第三个参数传递。
use NLPCloud\NLPCloud; $client = new NLPCloud('<model>','<your token>', true);
如果您想使用多语言插件来处理非英语文本,请将 '<your language code>'
作为第四个参数设置。例如,如果您想处理法语文本,应设置 'fra_Latn'
。
use NLPCloud\NLPCloud; $client = new NLPCloud('<model>','<your token>', false, '<your language code>');
如果您想进行异步请求,请将 true
作为第四个参数传递。
use NLPCloud\NLPCloud; $client = new NLPCloud('<model>', '<your token>', false, '<your language code>', true);
如果您正在执行异步请求,您将始终收到一个包含 URL 的快速响应。然后您应该定期(例如每 10 秒)使用 asyncResult()
对该 URL 进行轮询,以检查结果是否可用。以下是一个示例
$client->asyncResult('https://api.nlpcloud.io/v1/get-async-result/21718218-42e8-4be9-a67f-b7e18e03b436');
如果响应可用,上述命令将返回一个对象。否则,它将返回空(NULL
)。
自动语音识别(语音转文本)端点
调用 asr()
方法并传递以下参数
- (可选:以下任一或编码文件应设置)
url
:您的音频或视频文件托管的位置的 URL - (可选:以下任一或 URL 应设置)
encodedFile
:文件的 base64 编码版本 - (可选)
inputLanguage
:文件的 ISO 语言代码
echo json_encode($client->asr('<Your url>'));
上述命令返回一个对象。
聊天机器人端点
调用 chatbot()
方法并传入您的输入。作为选项,您还可以传入一个上下文和一个对话历史,该历史是一个命名数组的数组。每个命名数组由一个 input
和一个来自聊天机器人的 response
组成。
echo json_encode($client->chatbot('<Your input>', '<Your context>', array(array('input'=>'input 1','response'=>'response 1'), array('input'=>'input 2','response'=>'response 2'), ...)));
上述命令返回一个对象。
分类接口
调用 classification()
方法并传入 3 个参数
- 您想要分类的文本,作为字符串
- 您文本的候选标签,作为字符串数组
- 分类是否为多类,作为布尔值
echo json_encode($client->classification('<Your block of text>', array('label 1', 'label 2', ...), True|False));
上述命令返回一个对象。
代码生成接口
调用 codeGeneration()
方法并传入您程序的描述
echo json_encode($client->codeGeneration('<Your instruction>'));
上述命令返回一个对象。
依赖接口
调用 dependencies()
方法并传入您想要进行词性标注(POS)+ 弧标注的文本
echo json_encode($client->dependencies('<Your block of text>'));
上述命令返回一个对象。
嵌入接口
调用 embeddings()
方法并传入您想要从中提取嵌入的文本块数组
echo json_encode($client->embeddings(array('<Text 1>', '<Text 2>', '<Text 3>', ...)));
上述命令返回一个对象。
实体接口
调用 entities()
方法并传入您想要进行命名实体识别(NER)的文本
echo json_encode($client->entities('<Your block of text>'));
上述命令返回一个对象。
生成接口
调用 generation()
方法并传入以下参数
- 启动生成文本的文本块。GPT-J 在 CPU 上的最大长度为 256 个标记,GPT-J 和 GPT-NeoX 20B 在 GPU 上的最大长度为 1024 个标记,Fast GPT-J 和微调的 GPT-NeoX 20B 在 GPU 上的最大长度为 2048 个标记。
- (可选)
max_length
:可选。生成文本应包含的最大标记数。GPT-J 在 CPU 上的最大长度为 256 个标记,GPT-J 和 GPT-NeoX 20B 在 GPU 上的最大长度为 1024 个标记,Fast GPT-J 和微调的 GPT-NeoX 20B 在 GPU 上的最大长度为 2048 个标记。如果length_no_input
为 false,则生成文本的大小是max_length
与您输入文本的长度之差。如果length_no_input
为 true,则生成文本的大小仅为max_length
。默认为 50。 - (可选)
length_no_input
:是否min_length
和max_length
应包含输入文本的长度,作为布尔值。如果为 false,则min_length
和max_length
包含输入文本的长度。如果为 true,则 min_length 和max_length
不包含输入文本的长度。默认为 false。 - (可选)
end_sequence
:应该是生成序列结束的特定标记,作为字符串。例如,可以是.
或\n
或###
或任何其他 10 个字符以下的字符串。 - (可选)
remove_input
:您是否想从结果中删除输入文本,作为布尔值。默认为 false。 - (可选)
num_beams
:光束搜索的光束数。1 表示没有光束搜索。这是一个整数。默认为 1。 - (可选)
num_return_sequences
:对于批处理中的每个元素,返回的独立计算序列的数量,作为一个整数。默认为 1。 - (可选)
top_k
:用于 top-k-filtering 的高概率词汇标记的最高数量,作为一个整数。最大 1000 个标记。默认为 0。 - (可选)
top_p
:如果设置为小于 1 的浮点数,则仅保留累积概率达到 top_p 或更高的最可能标记进行生成。这是一个浮点数。应在 0 和 1 之间。默认为 0.7。 - (可选)
temperature
:用于模块化下一个标记概率的值,作为一个浮点数。应在 0 和 1 之间。默认为 1。 - (可选)
repetition_penalty
:重复惩罚的参数,作为一个浮点数。1.0 表示没有惩罚。默认为 1.0。 - (可选)
bad_words
:不允许生成的标记列表,作为字符串列表。默认为 null。 - (可选)
remove_end_sequence
:可选。您是否想从结果中删除end_sequence
字符串。默认为 false。
echo json_encode($client->generation('<Your input text>'));
语法和拼写校正接口
调用 gsCorrection()
方法并传入您想要校正的文本
echo json_encode($client->gsCorrection('<Your block of text>'));
上述命令返回一个对象。
图像生成接口
调用 imageGeneration()
方法,并传递您想要用来生成图像的文本
echo json_encode($client->imageGeneration('<Your block of text>'));
上述命令返回一个对象。
意图分类端点
调用 intentClassification()
方法,并传递您想要提取意图的文本
echo json_encode($client->intentClassification('<Your block of text>'));
上述命令返回一个对象。
关键词和关键短语提取端点
调用 kwKpExtraction()
方法,并传递您想要提取关键词和关键短语的文本
echo json_encode($client->kwKpExtraction('<Your block of text>'));
上述命令返回一个对象。
语言检测端点
调用 langdetection()
方法,并传递您想要分析的文本。
echo json_encode($client->langdetection('<Text to analyze>'));
上述命令返回一个对象。
释义端点
调用 paraphrasing()
方法,并传递您想要释义的文本。
echo json_encode($client->paraphrasing('<Your text to paraphrase>'));
上述命令返回一个对象。
问答端点
调用 question()
方法,并传递以下内容
- 您的问题
- 模型将用于尝试回答您问题的上下文
echo json_encode($client->question('<Your question>','<Your context>'));
上述命令返回一个对象。
语义搜索端点
调用 semanticSearch()
方法,并传递您的搜索查询
echo json_encode($client->semanticSearch('<Your search query>'));
上述命令返回一个对象。
语义相似度端点
调用 semanticSimilarity()
方法,并传递一个由两段您想要比较的文本组成的数组。
echo json_encode($client->semanticSimilarity(array('<Block of text 1>', '<Block of text 2>')));
上述命令返回一个对象。
句子依存关系端点
调用 sentenceDependencies()
方法,并传递一个由多个句子组成的文本块,您想要对其进行 POS + 弧分析。
echo json_encode($client->sentenceDependencies('<Your block of text>'));
上述命令返回一个对象。
情感分析端点
调用 sentiment()
方法,并传递您想要分析情感的文本
echo json_encode($client->sentiment('<Your block of text>'));
上述命令返回一个对象。
语音合成端点
调用 speechSynthesis()
方法,并传递您想要转换为音频的文本
echo json_encode($client->speechSynthesis('<Your block of text>'));
上述命令返回一个 JSON 对象。
摘要端点
调用 summarization()
方法,并传递您想要摘要的文本。
echo json_encode($client->summarization('<Your text to summarize>'));
上述命令返回一个对象。
分词端点
调用 tokens()
方法,并传递您想要分词的文本。
echo json_encode($client->tokens('<Your block of text>'));
上述命令返回一个对象。
翻译端点
调用 translation()
方法,并传递您想要翻译的文本。
echo json_encode($client->translation('<Your text to translate>'));
上述命令返回一个对象。