tourze / open-ai-bundle
DeepSeek API integration for Symfony
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/open-ai-bundle
Requires
- php: ^8.1
- ext-mbstring: *
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/form: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-client: ^6.4
- symfony/http-client-contracts: ^2.5 | ^3.0
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/serializer: ^6.4
- symfony/uid: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/bundle-dependency: 0.0.*
- tourze/doctrine-entity-markdown-bundle: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-random-bundle: 0.1.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- yiisoft/json: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:19:42 UTC
README
A comprehensive Symfony bundle for integrating with DeepSeek API and other AI models, providing conversation management, function calling, and easy-to-use chat interfaces.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Advanced Configuration
- Usage
- Advanced Usage
- Security
- Contributing
- License
Features
- π€ Multiple AI Model Support: DeepSeek (coder, chat, math, chinese), and customizable models
- π¬ Conversation Management: Full conversation history with role-based characters
- π§ Function Calling: Built-in AI functions for code analysis, file operations, and database queries
- π Streaming Responses: Real-time streaming output with chain-of-thought support
- π¨ Admin Interface: EasyAdmin integration for managing API keys, characters, and conversations
- π» CLI Tools: Interactive chat command with multiple modes
- π Secure: API key management with character-specific permissions
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 3.0 or higher
Installation
composer require tourze/open-ai-bundle
Quick Start
1. Configure Database
Run migrations to create the required tables:
php bin/console doctrine:migrations:migrate
2. Set Up API Keys
Create an API key in the database:
INSERT INTO open_ai_api_key (id, title, base_url, api_key, model, status) VALUES (1, 'DeepSeek API', 'https://api.deepseek.com', 'your-api-key', 'deepseek-chat', 1);
3. Create a Character
INSERT INTO open_ai_character (id, name, system_prompt, status, preferred_api_key_id) VALUES (1, 'Assistant', 'You are a helpful assistant.', 1, 1);
4. Start Chatting
# Interactive mode php bin/console open-ai:chat -c 1 # Single prompt mode php bin/console open-ai:chat -c 1 -p "Write a poem about coding"
Usage
Command Line Interface
The open-ai:chat command provides flexible interaction modes:
# Basic interactive chat php bin/console open-ai:chat --character 1 # Non-streaming mode php bin/console open-ai:chat -c 1 --no-stream # Quiet mode (only AI responses) php bin/console open-ai:chat -c 1 --prompt "Hello" --quiet # Debug mode php bin/console open-ai:chat -c 1 --debug
Interactive Commands:
- Type
q,quit, orexitto exit - Type
corclearto clear conversation history
Programmatic Usage
use OpenAIBundle\Service\OpenAiService; use OpenAIBundle\Service\ConversationService; use OpenAIBundle\VO\StreamRequestOptions; // Inject services $openAiService = $container->get(OpenAiService::class); $conversationService = $container->get(ConversationService::class); // Initialize conversation $conversation = $conversationService->initConversation($character, $apiKey); // Add user message $conversationService->createUserMessage($conversation, $apiKey, "Hello!"); // Get AI response (streaming) $options = new StreamRequestOptions( model: 'deepseek-chat', temperature: 0.7, maxTokens: 2000 ); foreach ($openAiService->streamReasoner($apiKey, $messages, $options) as $chunk) { // Process streaming chunks foreach ($chunk->getChoices() as $choice) { echo $choice->getContent(); } }
Function Calling
The bundle includes several built-in AI functions:
- Code Analysis: Analyze code structure, find references
- File Operations: List files, read text files
- Database Operations: Query tables, fetch results
- System Information: Get timezone, random numbers
Enable function calling on an API key:
UPDATE open_ai_api_key SET function_calling = 1 WHERE id = 1;
Admin Interface
Access the admin panel to manage:
- API Keys:
/admin?crudAction=index&crudControllerFqcn=OpenAIBundle\Controller\Admin\ApiKeyCrudController - Characters:
/admin?crudAction=index&crudControllerFqcn=OpenAIBundle\Controller\Admin\CharacterCrudController - Conversations:
/admin?crudAction=index&crudControllerFqcn=OpenAIBundle\Controller\Admin\ConversationCrudController
Advanced Configuration
Character Customization
$character = new Character(); $character->setName('Code Expert'); $character->setSystemPrompt('You are an expert programmer...'); $character->setTemperature(0.5); $character->setMaxTokens(4000); $character->setTopP(0.9); $character->setFrequencyPenalty(0.0); $character->setPresencePenalty(0.0);
Custom AI Functions
Create custom functions by implementing the function interface:
namespace App\AiFunction; use OpenAIBundle\Service\AiFunctionInterface; use OpenAIBundle\VO\FunctionDefinition; class MyCustomFunction implements AiFunctionInterface { public function getDefinition(): FunctionDefinition { // Define function parameters and description } public function execute(array $args): string { // Implement function logic } }
Error Handling
The bundle provides specific exception types:
try { $result = $openAiService->chat($apiKey, $messages, $options); } catch (ConfigurationException $e) { // Handle configuration errors } catch (ModelException $e) { // Handle model-related errors } catch (OpenAiException $e) { // Handle general errors }
Advanced Usage
Custom AI Functions
Create custom AI functions by implementing the AiFunctionInterface:
use OpenAIBundle\AiFunction\AiFunctionInterface; class CustomFunction implements AiFunctionInterface { public function getDefinition(): FunctionDefinition { return new FunctionDefinition( 'custom_function', 'Description of your function', [ new FunctionParam('param_name', FunctionParamType::STRING, 'Parameter description', true) ] ); } public function execute(array $arguments): string { // Your custom logic here return json_encode(['result' => 'success']); } }
Conversation Callbacks
Implement conversation callbacks for custom processing:
$conversationService->onMessageCreated(function (Message $message) { // Custom processing for each message });
Stream Processing
Handle streaming responses with custom processors:
foreach ($openAiService->streamReasoner($apiKey, $messages, $options) as $chunk) { // Custom stream processing $this->processChunk($chunk); }
Security
API Key Management
- Store API keys securely using Symfony's parameter encryption
- Rotate API keys regularly
- Use environment-specific keys for different environments
Character Permissions
- Configure character-specific API key access
- Implement role-based access control for characters
- Audit character usage and permissions regularly
Function Calling Security
- Validate all function parameters
- Implement function-level permissions
- Log and monitor function calls for security auditing
Data Protection
- Encrypt sensitive conversation data
- Implement data retention policies
- Ensure GDPR compliance for user data
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
The MIT License (MIT). See LICENSE for details.