PHPackages                             swoole-inc/open-ai - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. swoole-inc/open-ai

ActiveLibrary[API Development](/categories/api)

swoole-inc/open-ai
==================

OpenAI API Client in PHP

v1.5.0(2y ago)91.0k1MITPHPPHP &gt;=8.0

Since Jun 25Pushed 1y agoCompare

[ Source](https://github.com/swoole-inc/open-ai)[ Packagist](https://packagist.org/packages/swoole-inc/open-ai)[ Docs](https://github.com/swoole-inc/open-ai)[ GitHub Sponsors](https://github.com/orhanerday)[ RSS](/packages/swoole-inc-open-ai/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

OpenAI API Client in PHP
========================

[](#openai-api-client-in-php)

为何 `fork` 此库而不是向原项目发起 `PR` ，主要是是作者的响应时间太长，一个非常简单的 `PR` 经常 `1-2` 周的时间没有任何回复。 我们需要快速验证迭代，后续会抽时间提交给 `orhanerday/open-ai`

改进
--

[](#改进)

对 `orhanerday/open-ai` 库做了一些改进，包括如下内容：

1. 支持了获取 `curl` 底层错误码，在出现网络底层的问题后可以根据错误信息排查问题
2. 支持设置 `HTTP` 版本，使用方法 `$openai->setHttpVersion(2)` ，通过设置 `HTTP2` 协议可以绕过 Nginx 的 `proxy cache`
3. 改进了 `setBaseUrl()` 方法，原库是硬编码写死了 `api.openai.com`，发送请求前通过字符串替换设置 `BaseUrl`，不是很优雅
4. 改进了 `chat stream` 的实现，原库直接使用了 `WRITE_FUNCTION`，应用层需要分割 `Chunks`并手工解析 `Event-Stream` 消息

使用
--

[](#使用)

```
composer require swoole-inc/open-ai
```

实例
--

[](#实例)

```
define('BASE_PATH', dirname(__DIR__));
require BASE_PATH . '/vendor/autoload.php';

use SwooleAi\OpenAi\OpenAi;

$open_ai = new OpenAi('test');
$open_ai->setBaseURL('https://chat.swoole.com/');
$messages[] = ["role" => "system", "content" => "You are a helpful assistant."];
$messages[] = ["role" => "user", "content" => "Who won the world series in 2020?"];
$messages[] = ["role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020."];
$messages[] = ["role" => "user", "content" => "Where was it played?"];
$complete = $open_ai->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $messages,
    'stream' => true,
    'raw' => 1,
], function ($curl_info, $data) use (&$txt) {
    if ($data !== '[DONE]') {
        $json = json_decode($data, true);
        $txt .= $json['choices'][0]['delta']['content'];
    }
});

if ($complete) {
    var_dump($txt);
} else {
    var_dump($open_ai->getError(), $open_ai->getErrno() === CURLE_COULDNT_CONNECT);
}
```

代理
--

[](#代理)

在中国无法直接访问 `OpenAI` 服务器，可以设置代理。

```
$open_ai = new OpenAi('test');
$open_ai->setProxy('socks5h://127.0.0.1:1080');
```

- `socks5h://`：`socks5` 代理并且在对端进行 `DNS` 解析（推荐使用）
- `socks5://`：`socks5` 代理并且在本地进行 `DNS` 解析
- `http://`：`HTTP` 代理

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 69.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1050d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1dea9261ba377a13a16e7e0f3883e73bda4094551ddea280e852f23a5d248c0?d=identicon)[Tianfeng.Han](/maintainers/Tianfeng.Han)

---

Top Contributors

[![orhanerday](https://avatars.githubusercontent.com/u/22305274?v=4)](https://github.com/orhanerday "orhanerday (135 commits)")[![matyhtf](https://avatars.githubusercontent.com/u/2017766?v=4)](https://github.com/matyhtf "matyhtf (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![orhan-cmd](https://avatars.githubusercontent.com/u/98099231?v=4)](https://github.com/orhan-cmd "orhan-cmd (8 commits)")[![adetch](https://avatars.githubusercontent.com/u/1463387?v=4)](https://github.com/adetch "adetch (5 commits)")[![bashar94](https://avatars.githubusercontent.com/u/13801112?v=4)](https://github.com/bashar94 "bashar94 (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")[![cotrufoa](https://avatars.githubusercontent.com/u/121743171?v=4)](https://github.com/cotrufoa "cotrufoa (4 commits)")[![kuafuRace](https://avatars.githubusercontent.com/u/13517412?v=4)](https://github.com/kuafuRace "kuafuRace (3 commits)")[![johanvanhelden](https://avatars.githubusercontent.com/u/19389981?v=4)](https://github.com/johanvanhelden "johanvanhelden (1 commits)")[![mahadsprouttech](https://avatars.githubusercontent.com/u/56071270?v=4)](https://github.com/mahadsprouttech "mahadsprouttech (1 commits)")[![marcosegato](https://avatars.githubusercontent.com/u/6240628?v=4)](https://github.com/marcosegato "marcosegato (1 commits)")[![dsampaolo](https://avatars.githubusercontent.com/u/1384803?v=4)](https://github.com/dsampaolo "dsampaolo (1 commits)")[![Muchwat](https://avatars.githubusercontent.com/u/13133862?v=4)](https://github.com/Muchwat "Muchwat (1 commits)")[![mydnic](https://avatars.githubusercontent.com/u/2733767?v=4)](https://github.com/mydnic "mydnic (1 commits)")[![assert6](https://avatars.githubusercontent.com/u/28561563?v=4)](https://github.com/assert6 "assert6 (1 commits)")[![ali-wells](https://avatars.githubusercontent.com/u/19995855?v=4)](https://github.com/ali-wells "ali-wells (1 commits)")[![slaFFik](https://avatars.githubusercontent.com/u/282234?v=4)](https://github.com/slaFFik "slaFFik (1 commits)")

---

Tags

swooleOpen AI

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/swoole-inc-open-ai/health.svg)

```
[![Health](https://phpackages.com/badges/swoole-inc-open-ai/health.svg)](https://phpackages.com/packages/swoole-inc-open-ai)
```

###  Alternatives

[orhanerday/open-ai

OpenAI GPT-3 Api Client in PHP

2.4k4.3M37](/packages/orhanerday-open-ai)[daodao97/apidog

A swagger library for Hyperf.

15040.1k1](/packages/daodao97-apidog)[hyperf/swagger

A swagger library for Hyperf.

19338.7k6](/packages/hyperf-swagger)[hyperf/graphql

A GraphQL component for hyperf.

151.0k](/packages/hyperf-graphql)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
