mohammadsalamat/chat_ai
Composer 安装命令:
composer require mohammadsalamat/chat_ai
包简介
The Chat AI Laravel Package is a smart chatbot integration for Laravel applications. It dynamically retrieves responses from multiple sources, prioritizing efficiency and accuracy
README 文档
README
Overview
The Chat AI Laravel Package is an intelligent chatbot integration for Laravel applications. It retrieves responses dynamically from multiple sources, ensuring accuracy and efficiency:
- Database – Checks for an existing response.
- ChatGPT – If no response is found in the database, it queries OpenAI’s ChatGPT.
- Gemini AI – If ChatGPT reaches its limit, it switches to Google Gemini.
- Google Search – If no AI-generated response is available, it fetches relevant search results.
- Self-Learning – If an answer is not found, it is stored in the database for future queries.
How It Works
The chatbot follows a structured search approach:
-
Database Lookup
- The chatbot first searches the database for a response.
- Admins can manually add a list of questions and answers.
- If the user's question is at least 70% similar (adjustable via the config file) to a stored question, it retrieves the answer.
-
ChatGPT Integration
- If no answer is found in the database, it queries ChatGPT.
- The response is then stored in the database to reduce API usage and allow admin modifications.
-
Fallback to Gemini AI
- If ChatGPT is inactive or has reached its limit, the chatbot switches to Gemini AI and follows the same process.
-
Google Search
- If neither ChatGPT nor Gemini AI provides an answer, it fetches relevant data using Google Search.
Provides
- Simple Blade View – A minimal and customizable chat interface.
- Lightweight Controller – A clean structure to handle requests and responses.
- Database Migration – Stores previous queries and responses for improved efficiency.
Features
- Multi-source response system (Database → ChatGPT → Gemini → Google Search).
- Self-learning AI – Stores new questions and answers.
- Efficient fallback mechanism for uninterrupted responses.
- Simple API interface for easy integration.
- Optimized for performance with caching and similarity checks.
Installation
1. Install via Composer
Run the following command to install the package:
composer require mohammadsalamat/chat_ai
2. Publish Configuration (Optional)
php artisan vendor:publish
This command publishes the migration and view files.
3. Run Migrations
php artisan migrate
This creates a new database table called questions to store chatbot interactions.
4. Add API Keys to .env
Obtain API keys for OpenAI, Gemini, and Google Search, then update your .env file:
CHATGPT_API_KEY=your_chatgpt_api_key GEMINI_API_KEY=your_gemini_api_key GOOGLE_SEARCH_API_KEY=your_google_search_api_key GOOGLE_SEARCH_CX=your_google_search_cx
Usage
1. Use in a Controller or Route
Dependency Injection Approach
use Salamat\chat_ai\Http\Controllers\ChatAiController; public function sendMessage(Request $request, ChatAiController $controller) { return $controller->generateText($request); }
Instantiating the Controller Manually
use Salamat\chat_ai\Http\Controllers\ChatAiController; public function sendMessage(Request $request) { $controller = new ChatAiController(); return $controller->generateText($request); }
2. Create a POST Route to Send a Message
use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Route; Route::post('/sendMessage', [Controller::class, 'sendMessage']);
3. Frontend Integration
Send an AJAX request to your Laravel endpoint. A custom view is available at resources/views/chat_ai, including the following AJAX request:
fetch('/sendMessage', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': '{{ csrf_token() }}' // CSRF token for security }, body: JSON.stringify({ message: messageText }) }) .then(response => response.json()) .then(data => console.log(data));
Configuration
The package includes a configuration file located at config/chat_ai.php, where you can customize settings such as:
- API Limits
- Similarity Threshold
- Caching Options
Contributing
Feel free to submit pull requests or open issues for bug fixes and feature requests.
License
This package is open-source and available under the MIT License.
mohammadsalamat/chat_ai 适用场景与选型建议
mohammadsalamat/chat_ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mohammadsalamat/chat_ai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohammadsalamat/chat_ai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-18