mayursaptal/semantic-kernel-php
Composer 安装命令:
composer require mayursaptal/semantic-kernel-php
包简介
PHP implementation of Microsoft's Semantic Kernel framework for orchestrating LLMs, memory, and AI agents
关键字:
README 文档
README
Build AI-powered applications in PHP - A complete framework for orchestrating Large Language Models, memory systems, and intelligent planning. Compatible with Microsoft's Semantic Kernel patterns.
✨ What can you build?
🤖 AI-Powered Applications
// Customer support bot with memory $kernel = Kernel::createBuilder() ->withOpenAI($_ENV['OPENAI_API_KEY']) ->withVolatileMemory() ->build(); $response = $kernel->getChatService()->generateText( "Help customer with order {{order_id}}: {{question}}", new ContextVariables(['order_id' => '12345', 'question' => 'Where is my package?']) );
📝 Document Processing & Summarization
// Smart document summarizer $summarizer = new SemanticFunction( 'summarize', 'Summarize this document in 3 bullet points: {{input}}', 'Extracts key insights from documents' ); $plugin = KernelPlugin::create('DocumentTools'); $plugin->addFunction($summarizer); $kernel->importPlugin($plugin); $result = $kernel->run('DocumentTools.summarize', new ContextVariables([ 'input' => $longDocument ]));
🧠 Intelligent Planning & Task Execution
// AI plans and executes complex tasks $planner = new Planner($kernel); $plan = $planner->createPlan('Create and send weekly sales report'); // AI automatically breaks down into steps: // 1. Gather sales data → 2. Analyze trends → 3. Create report → 4. Send email $result = $planner->executePlan($plan, $context);
🚀 Quick Start
Installation
composer require mayursaptal/semantic-kernel-php
Basic Usage
<?php require_once 'vendor/autoload.php'; use SemanticKernel\Kernel; use SemanticKernel\ContextVariables; // Create kernel with AI service $kernel = Kernel::createBuilder() ->withOpenAI($_ENV['OPENAI_API_KEY']) ->withVolatileMemory() ->build(); // Generate AI response $response = $kernel->getChatService()->generateText('Explain AI in simple terms'); echo $response;
🤖 Supported AI Services
| Service | Models | Use Case |
|---|---|---|
| OpenAI | GPT-3.5, GPT-4 | General-purpose AI tasks |
| Azure OpenAI | GPT-3.5, GPT-4 | Enterprise applications |
| Google Gemini | Gemini 1.5 Flash/Pro | Multimodal AI (text + images) |
| Ollama | Llama2, Mistral, etc. | Local/private deployments |
Switch AI Services Easily
// OpenAI $kernel = Kernel::createBuilder() ->withOpenAI($_ENV['OPENAI_API_KEY'], 'gpt-4') ->build(); // Google Gemini $kernel = Kernel::createBuilder() ->withGemini($_ENV['GOOGLE_API_KEY'], 'gemini-1.5-pro') ->build(); // Azure OpenAI $kernel = Kernel::createBuilder() ->withAzureOpenAI($_ENV['AZURE_API_KEY'], $_ENV['AZURE_ENDPOINT'], $_ENV['DEPLOYMENT']) ->build(); // Local Ollama $kernel = Kernel::createBuilder() ->withOllama('llama2', 'http://localhost:11434') ->build();
🧩 Core Features
📦 Plugin System
Organize AI functions into reusable plugins:
$plugin = KernelPlugin::create('TextUtils'); // Add semantic functions (AI-powered) $plugin->addFunction(new SemanticFunction( 'translate', 'Translate "{{text}}" from {{from}} to {{to}}', 'Translates between languages' )); // Add native functions (PHP code) $plugin->addFunction(new NativeFunction( 'word_count', fn($context) => str_word_count($context->get('text')), 'Counts words in text' )); $kernel->importPlugin($plugin); // Use functions $result = $kernel->run('TextUtils.translate', new ContextVariables([ 'text' => 'Hello world', 'from' => 'English', 'to' => 'Spanish' ]));
💾 Memory & Context
AI remembers conversations and context:
// Store information $kernel->getMemoryStore()->store('user_preferences', 'user_123', 'Prefers technical explanations'); // Retrieve context $preferences = $kernel->getMemoryStore()->retrieve('user_preferences', 'user_123'); // Use in conversation $response = $kernel->getChatService()->generateText( "Based on user preference: {{preference}}, explain APIs", new ContextVariables(['preference' => $preferences]) );
🎯 Advanced Function Control
Control how AI uses functions:
use SemanticKernel\AI\PromptExecutionSettings; // AI automatically decides when to call functions $autoSettings = PromptExecutionSettings::withAutoFunctionChoice(); // AI must call at least one function $requiredSettings = PromptExecutionSettings::withRequiredFunctionChoice(); // Disable function calling $noneSettings = PromptExecutionSettings::withNoFunctionCalling();
📡 Event System
Monitor and observe AI operations:
$kernel->getEventDispatcher()->addListener('function.invoked', function($event) { echo "Function '{$event->getFunctionName()}' executed in {$event->getDuration()}ms\n"; });
📚 Examples
Explore comprehensive examples in the /examples directory:
01_basic_usage.php- Getting started with text generation02_ai_services.php- All supported AI services (OpenAI, Gemini, etc.)03_semantic_functions.php- Creating and using AI-powered functions04_memory_and_planning.php- Memory storage and intelligent planning05_advanced_features.php- Events, caching, monitoring
# Run examples
php examples/01_basic_usage.php
php examples/02_ai_services.php
🔧 Environment Setup
Create a .env file with your API keys:
# OpenAI OPENAI_API_KEY=sk-your-openai-key # Google Gemini GOOGLE_API_KEY=your-google-api-key # Azure OpenAI AZURE_OPENAI_API_KEY=your-azure-key AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com AZURE_OPENAI_DEPLOYMENT=your-deployment-name
🏗️ Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Application │ │ Semantic Kernel │ │ AI Services │
│ │───▶│ │───▶│ │
│ - Chat Bots │ │ - Orchestration │ │ - OpenAI │
│ - Summarizers │ │ - Planning │ │ - Gemini │
│ - Analyzers │ │ - Memory │ │ - Azure OpenAI │
│ - Workflows │ │ - Events │ │ - Ollama │
└─────────────────┘ └──────────────────┘ └─────────────────┘
🌟 Key Benefits
- 🚀 Easy Integration - Add AI to existing PHP applications
- 🔄 Service Agnostic - Switch between OpenAI, Gemini, Azure seamlessly
- 🧠 Memory & Context - AI remembers conversation history
- 📦 Modular Plugins - Reusable AI function libraries
- 🎯 Planning System - AI breaks down complex tasks automatically
- 📊 Production Ready - Caching, rate limiting, monitoring, events
- 🔧 Microsoft Compatible - Aligned with Microsoft's Semantic Kernel patterns
📖 Documentation
- Getting Started - Your first AI application
- AI Services - Configure OpenAI, Gemini, Azure
- Semantic Functions - Create AI-powered functions
- Memory Systems - Store and retrieve context
- Planning - Intelligent task decomposition
- Modular Plugins - Advanced plugin architecture
- Cookbook - Copy-paste solutions for common use cases
- Framework Overview - Complete feature guide
🤝 Contributing
We welcome contributions! See CONTRIBUTING.md for guidelines.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Inspired by Microsoft's Semantic Kernel
- Built for the PHP community with ❤️
⭐ Star this repo if you find it useful! | 📢 Share with the PHP community
mayursaptal/semantic-kernel-php 适用场景与选型建议
mayursaptal/semantic-kernel-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「microsoft」 「ai」 「machine-learning」 「artificial-intelligence」 「Gemini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mayursaptal/semantic-kernel-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mayursaptal/semantic-kernel-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mayursaptal/semantic-kernel-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple PHP package for sending messages to Microsoft Teams
Laravel 5 Queue Driver for Microsoft Azure Storage Queue
Simple PHP client for Qdrant vector database. Easy-to-use library for storing, searching, and managing vector embeddings in AI and machine learning applications.
Laravel package for the simplification and integration of many of your users most wanted login providers. Installation is a breeze. Never worry about OAuth again.
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
LaraMicrosoft-Auth: autenticación social con Microsoft Entra ID (Office 365). Integrable con frontends Vue, Nuxt o React.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-27