blinq/synth

这是我的包 synth

v1.0.7 2023-07-13 09:08 UTC

README

(非官方Laravel包)

Synth for Laravel

Synth 是一个 Laravel 工具,可以帮助你在 Laravel 应用程序中生成代码和执行各种任务。它利用 OpenAI 的 GPT 语言模型的力量,提供交互式和智能的开发体验。

演示

架构 -> 创建待办事项应用 😎🏗️📝

B.mp4

附加文件并创建一个 readme.md 📎📄✍️

B2.mp4

安装

  1. 使用 Composer 安装 Synth 包

    composer require blinq/synth
  2. 发布 Synth 配置文件

    php artisan vendor:publish --tag=synth-config

    在这里您可以更改所使用的模型(gpt4 对比 gpt3)

  3. .env 文件中设置您的 OpenAI API 密钥

    OPENAI_KEY=YOUR_API_KEY

用法

要使用 Synth,只需运行 synth 命令

php artisan synth

这将在 Synth CLI 中打开,您可以在其中与 GPT 模型交互并执行各种任务。

特性 🌟

  • 根据需要自动从小型模型切换到大型模型(gpt-3.5-turbo 对比 gpt-3.5-turbo-16k) 🔄
  • 包括整个数据库模式作为附件
  • 使用 OpenAI 的函数 API 👨‍💻
  • 使用 Ctrl+C 取消生成 🚫⌨
  • 附件:将文件附加到与 GPT 的对话中。 🗂️
  • 架构:头脑风暴并生成新的应用程序架构。 💡🏛
  • 聊天:与 GPT 聊天以获取响应并执行操作。 💬
  • 强制 GPT 为提出的问题生成文件。 📂
  • 迁移:为您的应用程序生成迁移。 📦
  • 模型:为您的应用程序生成模型。 📈
  • 文件:将文件写入文件系统。 🖊️

您可以从主菜单选择一个模块,然后按照提示执行所需操作。

注意:某些模块需要在完成之前的步骤,例如在生成迁移或模型之前创建架构。

编写您自己的模块

Synth 允许您通过编写自己的模块来扩展其功能。模块是一个实现注册和处理特定操作的必要方法的类。

要创建新的模块,请按照以下步骤操作

  1. 创建一个新的 PHP 类,该类扩展了 Module 类。
  2. 实现 name 方法来定义您模块的名称。
  3. 实现 register 方法来定义您模块提供的操作。
  4. 实现 onSelect 方法来处理选定的操作。

以下是一个自定义模块实现的示例

use Blinq\Synth\Modules\Module;

/**
 * Class MyModule
 * 
 * @propery \Blinq\Synth\Commands\SynthCommand $cmd
 */
class MyModule extends Module
{
    public function name(): string
    {
        return 'MyModule';
    }

    public function register(): array
    {
        return [
            'action1' => 'Perform Action 1',
            'action2' => 'Perform Action 2',
        ];
    }

    public function onSelect(?string $key = null)
    {
        $this->cmd->info("You selected: {$key}");

        $synth = $this->cmd->synth;

        if ($key === 'action1') {
            // Handle Action 1
            while (true) {
                $input = $this->cmd->ask("You");

                // Send the input to GPT
                $synth->chat($input, [
                    // ... The OpenAI Chat options

                    // If you want a function to be called by GPT
                    'function_call' => ['name' => 'some_function'], // Forces the function call
                    'functions' => [
                        [
                            'name' => 'some_function',
                            'description' => 'Description of the function',
                            'parameters' => [
                                // ..schema
                            ]
                        ]
                    ]
                ]);

                Functions::register('some_function', function (SynthCommand $cmd, $args, $asSpecified, $inSchema) { // etc..
                    // Do something with the call
                });

                // This will parse the json result and call the function if needed
                $synth->handleFunctionsForLastMessage();

                // Just retrieve the last message
                $lastMessage = $synth->getLastMessage();

                // Echo it's contents
                echo $lastMessage->content;

                // Or it's raw function_call
                dump($lastMessage->function_call);

                if (!$input || $input == 'exit') {
                    break;
                }
            }
        }
        if ($key === 'action2') {
            // Handle Action 2
        }
    }
}

然后,您可以在 Synth 包内的 Modules 类中注册您的自定义模块,并在 CLI 界面中使用它

use Blinq\Synth\Modules;

Modules::register(MyModule::class);