承接 tourze/open-ai-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/open-ai-bundle

Composer 安装命令:

composer require tourze/open-ai-bundle

包简介

DeepSeek API integration for Symfony

README 文档

README

English | 中文

Latest Version Total Downloads License PHP Version Build Status Code Coverage

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

  • 🤖 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, or exit to exit
  • Type c or clear to 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:

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

The MIT License (MIT). See LICENSE for details.

tourze/open-ai-bundle 适用场景与选型建议

tourze/open-ai-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/open-ai-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/open-ai-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/open-ai-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-03