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

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

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

ianrothmann/langserve-php-client

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.

ianrothmann/langserve-php-client 适用场景与选型建议

ianrothmann/langserve-php-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.18k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ianrothmann/langserve-php-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ianrothmann/langserve-php-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 ianrothmann/langserve-php-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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