定制 ianrothmann/langserve-php-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ianrothmann/langserve-php-client

最新稳定版本:1.2.0

Composer 安装命令:

composer require ianrothmann/langserve-php-client

包简介

A PHP Client for your LangServe project that is able to call API endpoints such as invoke, batch and stream.

README 文档

README

Purpose

This package provides a PHP client for the LangServe API, allowing you to easily call API endpoints such as invoke, batch, and stream. It simplifies the process of sending requests and handling responses from your LangServe project.

It was designed to work with vanilla PHP so it can be included with any framework (such as Laravel).

https://www.langchain.com/langserve

Installation

To install the package, use Composer:

composer require ianrothmann/langserve-php-client

Dependencies

This package requires:

  • PHP 8.0 or higher
  • Symfony HttpClient component, to support communication and streaming capabilities.

Ensure these are included in your project by adding them to your composer.json if not already present:

composer require symfony/http-client

Usage

First, instantiate the RemoteRunnable with the base URL of your LangServe API:

use IanRothmann\LangServePhpClient\RemoteRunnable;

$runnable = new RemoteRunnable('http://localhost:8100/summarize/');

You can optionally add a Bearer token as the second parameter (remember to implement it in LangServe):

$runnable = new RemoteRunnable('http://localhost:8100/summarize/');

You have some different options for adding headers and authentication:

$runnable->authenticateWithBearerToken('your-token'); //Add a Bearer token header with the token
$runnable->authenticateWithXToken('your-token'); //Add the X-Token header, if you implemented it according to the LangServe Examples with X-Token
$runnable->addHeader('Key', 'Value'); //Add any other header

Invoke

Invoke a single API call:

$input = ['text' => 'Hello, this is something for you to summarize'];
$response = $runnable->invoke($inputs);
dd($response->getRunId(), $response->getContent(), $response->getTokenUsage(), $response->getData(), $response->toJson());

Batch

Invoke multiple API calls in a batch:

$input = [['text' => 'Hello, this is something for you to summarize'],['text' => 'Hello, this is more text for you to summarize']];
$batchResponse = $runnable->batch($inputs);

foreach ($batchResponse->getResponses() as $response) {
    //$response->getRunId(),
    //$response->getContent(),
    //$response->getTokenUsage());
}

Stream

Handle streaming responses:

$result = $runnable->stream($input, function($response) {
    //Do something with the content here
    $response->getContent();
});
// Finally, this gives you the full content after everything has been streamed:
$result->getContent();

Response Classes

Responses from invoke and batch are handled through RemoteRunnableResponse and RemoteRunnableBatchResponse, respectively. stream initially provides RemoteRunnableStreamEvent for each received event, and finally, a RemoteRunnableStreamResponse aggregates all events to compile the total content.

Exceptions

This client can throw several exceptions based on API response:

  • InternalServerErrorException for server errors.
  • MalformedInputException for input schema mismatches.
  • NotFoundException when the requested resource is not found.
  • RemoteInvocationException for errors during the remote invocation.

These exceptions help in accurately diagnosing issues with API interactions.

Contributions

Any contributions to this package is welcome. I'm looking to add more functionality to it and will work on it as required.

统计信息

  • 总下载量: 2.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-02