PHP AI 插件大集合!文心 + 千问 + 火山 + DeepSeek 等众多大模型
大家好,我是小皮
PHP AI 插件,整合文心、千问、火山、DeepSeek、混元大模型、智谱清言、Kimi、ChatGPT、Gemini、Minmax 等AI模型接口,支持 ThinkPHP 和 Laravel 框架。
功能特性
-
✅ 支持多个AI模型:文心、千问、火山、DeepSeek、混元大模型、智谱清言、Kimi、ChatGPT、Gemini、Minmax -
✅ 统一API接口,简化AI调用 -
✅ 支持流式响应 -
✅ 支持ThinkPHP 6.0+ -
✅ 支持Laravel 8.0+ -
✅ 灵活的配置系统
安装
使用composer安装:
composer require phpais/ai-plugin
配置
Laravel 配置
-
发布配置文件:
php artisan vendor:publish --provider="Phpais\AiPlugin\Laravel\AiPluginServiceProvider"
-
在 .env文件中配置AI模型:
# 默认AI模型AI_DEFAULT=wenxin# 文心AI配置AI_WENXIN_API_KEY=your_api_keyAI_WENXIN_MODEL=ernie-bot# 千问AI配置AI_QIANWEN_API_KEY=your_api_keyAI_QIANWEN_MODEL=ep-20240101123456-abcde# 火山AI配置AI_VOLCANO_API_KEY=your_api_keyAI_VOLCANO_MODEL=ep-20240101123456-abcde# DeepSeek AI配置AI_DEEPSEEK_API_KEY=your_api_keyAI_DEEPSEEK_MODEL=deepseek-chat# 混元大模型配置AI_HUNYUAN_API_KEY=your_api_keyAI_HUNYUAN_MODEL=hunyuan-pro# 智谱清言配置AI_ZHIPU_API_KEY=your_api_keyAI_ZHIPU_MODEL=glm-4# ChatGPT配置AI_CHATGPT_API_KEY=your_api_keyAI_CHATGPT_MODEL=gpt-3.5-turbo# Gemini配置AI_GEMINI_API_KEY=your_api_keyAI_GEMINI_MODEL=gemini-pro# Minmax配置AI_MINMAX_API_KEY=your_api_keyAI_MINMAX_MODEL=abab5.5-chat# Kimi配置AI_KIMI_API_KEY=your_api_keyAI_KIMI_MODEL=kimi
ThinkPHP 配置
-
在
config目录下创建ai.php配置文件,内容参考src/Config/ai.php -
在
.env文件中配置AI模型,配置项与Laravel相同
使用示例
Laravel 示例
基本使用
usePhpais\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();});
使用特定模型
usePhpais\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 示例
基本使用
// 发送文本请求$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();});
使用特定模型
usePhpais\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'];
API 文档
核心方法
chat(string $prompt, array $options = []): array
发送文本请求到AI模型
-
$prompt: 提示词 -
$options: 可选参数,包括 temperature, max_tokens, system 等 -
返回值: 包含响应文本和使用信息的数组
streamChat(string $prompt, callable $callback, array $options = []): void
发送流式文本请求到AI模型
-
$prompt: 提示词 -
$callback: 回调函数,用于处理流式响应 -
$options: 可选参数
generateImage(string $prompt, array $options = []): array
生成图像(部分模型支持)
-
$prompt: 提示词 -
$options: 可选参数 -
返回值: 包含图像URL和使用信息的数组
getModelInfo(): array
获取模型信息
-
返回值: 模型信息数组
支持的AI模型
|
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
注意事项
-
请确保在使用前配置好对应的API密钥 -
不同模型的API调用方式和参数可能略有差异 -
流式响应需要在支持的环境中使用
感谢大家阅读,个人观点仅供参考,欢迎在评论区发表不同观点。
欢迎关注、分享、点赞、收藏、在看,我是微信公众号「PHP驿站」作者小皮。
夜雨聆风
