llm-agents/cli-chat

一个简单的命令行聊天应用

1.4.0 2024-09-07 09:40 UTC

This package is auto-updated.

Last update: 2024-09-07 09:40:39 UTC


README

这个酷炫的包为你提供了一个命令行聊天界面,可以与LLM代理进行聊天。它基于Symfony Console构建,所以你肯定知道它会非常流畅。

这究竟是怎么回事?

  • 与不同的代理开始聊天会话
  • 发送消息并获取回复
  • 处理工具调用及其结果
  • 显示聊天历史

非常适合测试您的代理或构建基于命令行的聊天机器人。

安装

您可以通过Composer安装此包

composer require llm-agents/cli-chat

配置

此包不需要太多配置。您需要设置的主要事项包括

  • LLM\Agents\Chat\ChatServiceInterface的实现
  • LLM\Agents\Chat\ChatHistoryRepositoryInterface的实现
  • LLM\Agents\Chat\SessionInterface的实现
  • 您的代理注册表
  • 您的工具注册表

使用方法

要使用此包,您需要在您的应用程序中创建两个控制台命令

  • 一个用于启动聊天会话的命令
  • 一个用于显示聊天历史的命令

以下是如何使用Spiral框架设置这些命令的方法

注意:您可以使用任何支持Symfony Console命令的框架或库。

聊天会话命令

此命令启动新的聊天会话

<?php

declare(strict_types=1);

namespace App\Endpoint\Console;

use LLM\Agents\Agent\AgentRegistryInterface;
use LLM\Agents\Chat\ChatHistoryRepositoryInterface;
use LLM\Agents\Chat\ChatServiceInterface;
use LLM\Agents\Chat\Console\ChatSession;
use LLM\Agents\Tool\ToolRegistryInterface;
use Ramsey\Uuid\Uuid;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Command;
use Spiral\Console\Console;
use Symfony\Component\Console\Cursor;

#[AsCommand(
    name: 'chat',
    description: 'Chat session'
)]
final class ChatCommand extends Command
{
    public function __invoke(
        AgentRegistryInterface $agents,
        ChatServiceInterface $chat,
        Console $console,
        ChatHistoryRepositoryInterface $chatHistory,
        ToolRegistryInterface $tools,
    ): int {
        $cursor = new Cursor($this->output);
        $cursor->clearScreen();
        $console->run(command: 'agent:list', output: $this->output);

        $chat = new ChatSession(
            input: $this->input,
            output: $this->output,
            agents: $agents,
            chat: $chat,
            chatHistory: $chatHistory,
            tools: $tools,
        );

        $chat->run(accountUuid: Uuid::fromString('00000000-0000-0000-0000-000000000000'));

        return self::SUCCESS;
    }
}

聊天历史命令

此命令显示特定会话的聊天历史

<?php

declare(strict_types=1);

namespace App\Endpoint\Console;

use LLM\Agents\Chat\ChatHistoryRepositoryInterface;
use LLM\Agents\Chat\ChatServiceInterface;
use LLM\Agents\Chat\Console\ChatHistory;
use Ramsey\Uuid\Uuid;
use Spiral\Console\Attribute\Argument;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Command;

#[AsCommand(
    name: 'chat:session',
    description: 'Chat session'
)]
final class ChatWindowCommand extends Command
{
    #[Argument(name: 'session_uuid')]
    public string $sessionUuid;

    public function __invoke(
        ChatHistoryRepositoryInterface $chatHistory,
        ChatServiceInterface $chatService,
    ): int {
        $chatWindow = new ChatHistory(
            input: $this->input,
            output: $this->output,
            chatHistory: $chatHistory,
            chat: $chatService,
        );

        $chatWindow->run(Uuid::fromString($this->sessionUuid));

        return self::SUCCESS;
    }
}

要使用这些命令,您需要根据您的框架将它们注册到应用程序的控制台内核或命令加载器。

类图

让我们快速看一下所有这些是如何协同工作的

贡献

我们非常欢迎您的帮助,使这个包变得更好!以下是如何贡献的方法

  1. 克隆存储库
  2. 编写一些优秀的代码
  3. 创建一个新的Pull Request

请确保您的代码遵循PSR-12编码标准,并为任何新功能编写测试。

许可

此包是开源软件,根据MIT许可证授权。您可以自由使用、修改和分享它!

就这些了,朋友们!如果您有任何问题或遇到任何问题,请随时在GitHub上创建一个问题。