PHPackages                             phpais/ai-plugin - 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. phpais/ai-plugin

ActiveLibrary[API Development](/categories/api)

phpais/ai-plugin
================

PHP AI 插件，整合文心、千问、火山、deepseek、混元、智谱清言、月之暗面等AI模型接口，支持ThinkPHP和Laravel框架

1.2.01(3mo ago)8361MITPHPPHP &gt;=7.4

Since Jan 28Pushed 3mo agoCompare

[ Source](https://github.com/phpais/ai-plugin)[ Packagist](https://packagist.org/packages/phpais/ai-plugin)[ Docs](https://github.com/phpais/ai-plugin)[ RSS](/packages/phpais-ai-plugin/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (4)Used By (0)

PHP AI 插件
=========

[](#php-ai-插件)

PHP AI 插件，整合文心、千问、火山、DeepSeek、混元大模型、智谱清言、Kimi、ChatGPT、Gemini、Minmax等AI模型接口，支持ThinkPHP和Laravel框架。

功能特性
----

[](#功能特性)

- ✅ 支持多个AI模型：文心、千问、火山、DeepSeek、混元大模型、智谱清言、Kimi、ChatGPT、Gemini、Minmax
- ✅ 统一API接口，简化AI调用
- ✅ 支持流式响应
- ✅ 支持ThinkPHP 6.0+
- ✅ 支持Laravel 8.0+
- ✅ 支持Symfony 5.0+
- ✅ 支持Yii 2.0+
- ✅ 支持Slim 4.0+
- ✅ 支持Hyperf 2.0+
- ✅ 灵活的配置系统

安装
--

[](#安装)

使用composer安装：

```
composer require phpais/ai-plugin
```

配置
--

[](#配置)

### Laravel 配置

[](#laravel-配置)

1. 发布配置文件：

```
php artisan vendor:publish --provider="Phpais\AiPlugin\Laravel\AiPluginServiceProvider"
```

2. 在 `.env` 文件中配置AI模型：

```
# 默认AI模型
AI_DEFAULT=wenxin

# 文心AI配置
AI_WENXIN_API_KEY=your_api_key
AI_WENXIN_MODEL=ernie-bot

# 千问AI配置
AI_QIANWEN_API_KEY=your_api_key
AI_QIANWEN_MODEL=ep-20240101123456-abcde

# 火山AI配置
AI_VOLCANO_API_KEY=your_api_key
AI_VOLCANO_MODEL=ep-20240101123456-abcde

# DeepSeek AI配置
AI_DEEPSEEK_API_KEY=your_api_key
AI_DEEPSEEK_MODEL=deepseek-chat

# 混元大模型配置
AI_HUNYUAN_API_KEY=your_api_key
AI_HUNYUAN_MODEL=hunyuan-pro

# 智谱清言配置
AI_ZHIPU_API_KEY=your_api_key
AI_ZHIPU_MODEL=glm-4

# ChatGPT配置
AI_CHATGPT_API_KEY=your_api_key
AI_CHATGPT_MODEL=gpt-3.5-turbo

# Gemini配置
AI_GEMINI_API_KEY=your_api_key
AI_GEMINI_MODEL=gemini-pro

# Minmax配置
AI_MINMAX_API_KEY=your_api_key
AI_MINMAX_MODEL=abab5.5-chat

# Kimi配置
AI_KIMI_API_KEY=your_api_key
AI_KIMI_MODEL=kimi
```

### ThinkPHP 配置

[](#thinkphp-配置)

1. 在 `config` 目录下创建 `ai.php` 配置文件，内容参考 `src/Config/ai.php`
2. 在 `.env` 文件中配置AI模型，配置项与Laravel相同

### Symfony 配置

[](#symfony-配置)

1. 注册Bundle到 `config/bundles.php`：

```
return [
    // 其他Bundle
    Phpais\AiPlugin\Symfony\AiPluginBundle::class => ['all' => true],
];
```

2. 在 `.env` 文件中配置AI模型，配置项与Laravel相同

### Yii 配置

[](#yii-配置)

1. 注册Module到应用配置：

```
// config/web.php 或 config/console.php
return [
    'modules' => [
        'aiPlugin' => [
            'class' => Phpais\AiPlugin\Yii\AiPluginModule::class,
        ],
    ],
];
```

2. 在 `.env` 文件中配置AI模型，配置项与Laravel相同

### Slim 配置

[](#slim-配置)

1. 注册服务到Slim应用：

```
use Phpais\AiPlugin\Slim\AiPluginProvider;

$app = AppFactory::create();

// 注册AI插件服务
$app->add(AiPluginProvider::class);
```

2. 在 `.env` 文件中配置AI模型，配置项与Laravel相同

### Hyperf 配置

[](#hyperf-配置)

1. 注册Provider到 `config/autoload/provider.php`：

```
return [
    // 其他Provider
    Phpais\AiPlugin\Hyperf\Provider\AiPluginServiceProvider::class,
];
```

2. 在 `.env` 文件中配置AI模型，配置项与Laravel相同

使用示例
----

[](#使用示例)

### Laravel 示例

[](#laravel-示例)

#### 基本使用

[](#基本使用)

```
use Phpais\AiPlugin\Laravel\Facades\AI;

// 发送文本请求
$result = AI::chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 带参数的请求
$result = AI::chat('写一首关于春天的诗', [
    'temperature' => 0.8,
    'max_tokens' => 512,
    'system' => '你是一位诗人，擅长写抒情诗'
]);
echo $result['text'];

// 流式响应
AI::streamChat('写一篇关于AI的文章', function ($chunk) {
    echo $chunk;
    flush();
});
```

#### 使用特定模型

[](#使用特定模型)

```
use Phpais\AiPlugin\Factories\AiClientFactory;

// 使用千问模型
$qianwenClient = AiClientFactory::create('qianwen', config('ai.providers.qianwen'));
$result = $qianwenClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用DeepSeek模型
$deepseekClient = AiClientFactory::create('deepseek', config('ai.providers.deepseek'));
$result = $deepseekClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用混元大模型
$hunyuanClient = AiClientFactory::create('hunyuan', config('ai.providers.hunyuan'));
$result = $hunyuanClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用智谱清言
$zhipuClient = AiClientFactory::create('zhipu', config('ai.providers.zhipu'));
$result = $zhipuClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用ChatGPT
$chatgptClient = AiClientFactory::create('chatgpt', config('ai.providers.chatgpt'));
$result = $chatgptClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用Gemini
$geminiClient = AiClientFactory::create('gemini', config('ai.providers.gemini'));
$result = $geminiClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用Minmax
$minmaxClient = AiClientFactory::create('minmax', config('ai.providers.minmax'));
$result = $minmaxClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用Kimi
$kimiClient = AiClientFactory::create('kimi', config('ai.providers.kimi'));
$result = $kimiClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];
```

### ThinkPHP 示例

[](#thinkphp-示例)

#### 基本使用

[](#基本使用-1)

```
// 发送文本请求
$result = app('ai')->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 带参数的请求
$result = app('ai')->chat('写一首关于春天的诗', [
    'temperature' => 0.8,
    'max_tokens' => 512,
    'system' => '你是一位诗人，擅长写抒情诗'
]);
echo $result['text'];

// 流式响应
app('ai')->streamChat('写一篇关于AI的文章', function ($chunk) {
    echo $chunk;
    flush();
});
```

#### 使用特定模型

[](#使用特定模型-1)

```
use Phpais\AiPlugin\Factories\AiClientFactory;

// 使用千问模型
$qianwenClient = AiClientFactory::create('qianwen', config('ai.providers.qianwen'));
$result = $qianwenClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用DeepSeek模型
$deepseekClient = AiClientFactory::create('deepseek', config('ai.providers.deepseek'));
$result = $deepseekClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用混元大模型
$hunyuanClient = AiClientFactory::create('hunyuan', config('ai.providers.hunyuan'));
$result = $hunyuanClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 使用智谱清言
$zhipuClient = AiClientFactory::create('zhipu', config('ai.providers.zhipu'));
$result = $zhipuClient->chat('你好，能介绍一下你自己吗？');
echo $result['text'];
```

### Symfony 示例

[](#symfony-示例)

#### 基本使用

[](#基本使用-2)

```
// 注入AI服务
public function index(Phpais\AiPlugin\Contracts\AiClientInterface $aiClient)
{
    // 发送文本请求
    $result = $aiClient->chat('你好，能介绍一下你自己吗？');
    echo $result['text'];

    // 带参数的请求
    $result = $aiClient->chat('写一首关于春天的诗', [
        'temperature' => 0.8,
        'max_tokens' => 512,
        'system' => '你是一位诗人，擅长写抒情诗'
    ]);
    echo $result['text'];

    // 流式响应
    $aiClient->streamChat('写一篇关于AI的文章', function ($chunk) {
        echo $chunk;
        flush();
    });
}
```

### Yii 示例

[](#yii-示例)

#### 基本使用

[](#基本使用-3)

```
// 发送文本请求
$result = Yii::$app->get('ai')->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 带参数的请求
$result = Yii::$app->get('ai')->chat('写一首关于春天的诗', [
    'temperature' => 0.8,
    'max_tokens' => 512,
    'system' => '你是一位诗人，擅长写抒情诗'
]);
echo $result['text'];

// 流式响应
Yii::$app->get('ai')->streamChat('写一篇关于AI的文章', function ($chunk) {
    echo $chunk;
    flush();
});
```

### Slim 示例

[](#slim-示例)

#### 基本使用

[](#基本使用-4)

```
// 发送文本请求
$result = $app->get('ai')->chat('你好，能介绍一下你自己吗？');
echo $result['text'];

// 带参数的请求
$result = $app->get('ai')->chat('写一首关于春天的诗', [
    'temperature' => 0.8,
    'max_tokens' => 512,
    'system' => '你是一位诗人，擅长写抒情诗'
]);
echo $result['text'];

// 流式响应
$app->get('ai')->streamChat('写一篇关于AI的文章', function ($chunk) {
    echo $chunk;
    flush();
});
```

### Hyperf 示例

[](#hyperf-示例)

#### 基本使用

[](#基本使用-5)

```
// 注入AI服务
public function index(Phpais\AiPlugin\Contracts\AiClientInterface $aiClient)
{
    // 发送文本请求
    $result = $aiClient->chat('你好，能介绍一下你自己吗？');
    echo $result['text'];

    // 带参数的请求
    $result = $aiClient->chat('写一首关于春天的诗', [
        'temperature' => 0.8,
        'max_tokens' => 512,
        'system' => '你是一位诗人，擅长写抒情诗'
    ]);
    echo $result['text'];

    // 流式响应
    $aiClient->streamChat('写一篇关于AI的文章', function ($chunk) {
        echo $chunk;
        flush();
    });
}
```

API 文档
------

[](#api-文档)

### 核心方法

[](#核心方法)

#### `chat(string $prompt, array $options = []): array`

[](#chatstring-prompt-array-options---array)

发送文本请求到AI模型

- `$prompt`: 提示词
- `$options`: 可选参数，包括 temperature, max\_tokens, system 等
- 返回值: 包含响应文本和使用信息的数组

#### `streamChat(string $prompt, callable $callback, array $options = []): void`

[](#streamchatstring-prompt-callable-callback-array-options---void)

发送流式文本请求到AI模型

- `$prompt`: 提示词
- `$callback`: 回调函数，用于处理流式响应
- `$options`: 可选参数

#### `generateImage(string $prompt, array $options = []): array`

[](#generateimagestring-prompt-array-options---array)

生成图像（部分模型支持）

- `$prompt`: 提示词
- `$options`: 可选参数
- 返回值: 包含图像URL和使用信息的数组

#### `getModelInfo(): array`

[](#getmodelinfo-array)

获取模型信息

- 返回值: 模型信息数组

#### `setPrepareRequestDataCallback(callable $callback): $this`

[](#setpreparerequestdatacallbackcallable-callback-this)

设置自定义请求数据准备回调函数

- `$callback`: 接收 `$prompt`、`$options`、`$clientConfig` 参数，返回自定义请求数据数组
- 返回值: 当前客户端实例，支持链式调用

**使用示例**:

```
use Phpais\AiPlugin\Laravel\Facades\AI;

// 获取客户端实例
$aiClient = app('ai');

// 设置自定义请求数据回调
$aiClient->setPrepareRequestDataCallback(function ($prompt, $options, $clientConfig) {
    return [
        'model' => $clientConfig['model'] ?? 'deepseek-chat',
        'messages' => [
            [
                'role' => 'system',
                'content' => '你是一位专业的助手，只提供准确的信息。'
            ],
            [
                'role' => 'user',
                'content' => $prompt
            ]
        ],
        'temperature' => $options['temperature'] ?? 0.7,
        'max_tokens' => $options['max_tokens'] ?? 1024,
        'top_p' => 0.9,
    ];
});

// 正常调用
$result = $aiClient->chat('什么是PHP？');
echo $result['text'];
```

#### `setParseResponseCallback(callable $callback): $this`

[](#setparseresponsecallbackcallable-callback-this)

设置自定义响应解析回调函数

- `$callback`: 接收 `$response`、`$clientConfig` 参数，返回自定义响应数据数组
- 返回值: 当前客户端实例，支持链式调用

**使用示例**:

```
use Phpais\AiPlugin\Laravel\Facades\AI;

// 获取客户端实例
$aiClient = app('ai');

// 设置自定义响应解析回调
$aiClient->setParseResponseCallback(function ($response, $clientConfig) {
    return [
        'answer' => $response['choices'][0]['message']['content'] ?? '无响应',
        'model' => $response['model'] ?? $clientConfig['model'],
        'usage' => $response['usage'] ?? [],
        'custom_fields' => [
            'timestamp' => time(),
            'response_length' => strlen($response['choices'][0]['message']['content'] ?? ''),
            'is_success' => isset($response['choices']) && count($response['choices']) > 0,
        ]
    ];
});

// 正常调用
$result = $aiClient->chat('什么是PHP？');
echo $result['answer'];
echo '处理时间: ' . date('Y-m-d H:i:s', $result['custom_fields']['timestamp']);
```

支持的AI模型
-------

[](#支持的ai模型)

模型提供者配置键说明文心一言百度wenxin百度的AI模型千问阿里qianwen阿里的AI模型火山字节跳动volcano字节跳动的AI模型DeepSeekDeepSeekdeepseekDeepSeek的AI模型混元大模型腾讯hunyuan腾讯的AI模型智谱清言智谱AIzhipu智谱AI的AI模型Kimi月之暗面kimi月之暗面的AI模型ChatGPTOpenAIchatgptOpenAI的AI模型GeminiGooglegeminiGoogle的AI模型MinmaxMinmaxminmaxMinmax的AI模型注意事项
----

[](#注意事项)

1. 请确保在使用前配置好对应的API密钥
2. 不同模型的API调用方式和参数可能略有差异
3. 流式响应需要在支持的环境中使用

许可证
---

[](#许可证)

MIT License

贡献
--

[](#贡献)

欢迎提交Issue和Pull Request！

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance81

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Every ~4 days

Total

3

Last Release

102d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/20204d24ceaa71efdd8a163154ff3640dddea8a69fde446295e20d947d7146b2?d=identicon)[sugar1569](/maintainers/sugar1569)

---

Top Contributors

[![sugar1569](https://avatars.githubusercontent.com/u/14051877?v=4)](https://github.com/sugar1569 "sugar1569 (10 commits)")

---

Tags

phplaravelaideepseekthinkphpVolcanowenxinqianwen

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phpais-ai-plugin/health.svg)

```
[![Health](https://phpackages.com/badges/phpais-ai-plugin/health.svg)](https://phpackages.com/packages/phpais-ai-plugin)
```

###  Alternatives

[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

47073.9k5](/packages/deepseek-php-deepseek-php-client)[mozex/anthropic-laravel

Anthropic PHP for Laravel is a supercharged PHP API client that allows you to interact with the Anthropic API

71226.4k1](/packages/mozex-anthropic-laravel)

PHPackages © 2026

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