distantmagic/resonance

具有AI功能的PHP框架。

v0.31.0 2024-07-08 21:32 UTC

README

关于Resonance

Resonance是从底层设计的,旨在促进您基础设施内以及基础设施之间的服务互操作性和消息传递。它提供了AI功能,内置了网络服务器,并与llama.cpp集成。

充分利用异步PHP。建立在Swoole之上。

为什么选择Resonance?

可预测的性能

Resonance的设计考虑了几个优先级:没有内存泄漏、阻塞操作和垃圾收集器惊喜。

大多数内部结构都是只读和状态无关的。在应用程序启动后,没有任何东西会干扰JIT和操作码(反射仅在应用程序启动期间使用),因此在运行时不会有意外减慢。

有见地

底下的所有库都经过彻底测试,以确保它们可以正确协同工作、相互补充,并在异步环境中完美运行。

例如,Resonance实现了定制的Doctrine驱动程序,因此它使用Swoole的连接池。

解决输入/输出问题

Resonance被设计来处理IO密集型任务,例如提供机器学习模型、处理WebSocket连接和处理长时间运行的HTTP请求。

它将现代应用程序视为一种服务组合,这些服务通过异步方式相互通信,包括AI补全和ML推理,因此它提供了一套工具,使这种通信尽可能简单。

完整套件

Resonance包含构建现代Web应用程序所需的一切,从HTTP服务器到AI功能。

它提供了安全功能、HTML模板、与开源LLMs的集成以及提供ML模型的服务能力。

文档

https://resonance.distantmagic.com/

安装

最好通过使用Composer的create-project命令来安装Resonance。

composer create-project distantmagic/resonance-project my-project

Resonance需要PHP的最低版本为8.2,以及数据结构和Swoole扩展。有关所需和推荐扩展以及其他安装方法的更多信息,请参阅我们的安装指南

首次使用

安装项目后(提供了config.ini.example),您需要创建一个config.ini文件,然后使用bin/resonance.php作为入口点。

运行服务器

php bin/resonance.php serve启动内置的HTTP服务器。如果需要,您可以为本地开发生成SSL证书

特性

与开源LLMs聊天

创建提示控制器以直接回答用户的提示。

LLM负责确定用户意图,您可以专注于采取适当的行动。

#[RespondsToPromptSubject(
    action: 'adopt',
    subject: 'cat',
)]
#[Singleton(collection: SingletonCollection::PromptSubjectResponder)]
readonly class CatAdopt implements PromptSubjectResponderInterface
{
    public function respondToPromptSubject(PromptSubjectRequest $request, PromptSubjectResponse $response): void
    {
        // Pipes message through WebSocket... 

        $response->write("Here you go:\n\n");
        $response->write("   |\_._/|\n");
        $response->write("   | o o |\n");
        $response->write("   (  T  )\n");
        $response->write("  .^`-^-`^.\n");
        $response->write("  `.  ;  .`\n");
        $response->write("  | | | | |\n");
        $response->write(" ((_((|))_))\n");
        $response->end();
    }
}

异步处理

以最小的开销异步响应传入的RPC或WebSocket消息(或两者结合)。

您可以使用属性设置所有异步功能。不需要复杂的配置。

#[RespondsToWebSocketJsonRPC(JsonRPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketJsonRPCResponder)]
final readonly class EchoResponder extends WebSocketJsonJsonRPCResponder
{
    public function getConstraint(): Constraint
    {
        return new StringConstraint();
    }

    public function onRequest(
        WebSocketAuthResolution $webSocketAuthResolution,
        WebSocketConnection $webSocketConnection,
        RPCRequest $rpcRequest,
    ): void {
        $webSocketConnection->push(new JsonRPCResponse(
            $rpcRequest,
            $rpcRequest->payload,
        ));
    }
}

简单的事情仍然简单

编写HTTP控制器的方式与同步代码类似。

控制器具有利用异步环境的新兴功能。

#[RespondsToHttp(
    method: RequestMethod::GET,
    pattern: '/',
)]
function Homepage(ServerRequestInterface $request, ResponseInterface $response): TwigTemplate
{
    return new TwigTemplate('website/homepage.twig');
}

一致性是关键

无论项目规模大小,您都可以保持相同的软件编写方法。

没有不断增长的中央配置文件或服务依赖注册表。代码模块之间的关系都是局部的。

#[ListensTo(HttpServerStarted::class)]
#[Singleton(collection: SingletonCollection::EventListener)]
final readonly class InitializeErrorReporting extends EventListener
{
    public function handle(object $event): void
    {
        // ...
    }
}

PHP中的承诺

Resonance提供对Promise/A+规范的局部实现,以处理各种异步任务。

$future1 = new SwooleFuture(function (int $value) {
    assert($value === 1);

    return $value + 2;
});

$future2 = $future1->then(new SwooleFuture(function (int $value) {
    assert($value === 3);

    return $value + 4;
}));

assert($future2->resolve(1)->result === 7);

开箱即用的GraphQL

您可以使用PHP属性构建复杂的GraphQL模式。

Resonance负责重用SQL查询和优化资源使用。

所有字段都可以异步解析。

#[GraphQLRootField(
    name: 'blogPosts',
    type: GraphQLRootFieldType::Query,
)]
#[Singleton(collection: SingletonCollection::GraphQLRootField)]
final readonly class Blog implements GraphQLFieldableInterface
{
    public function __construct(
        private DatabaseConnectionPoolRepository $connectionPool,
        private BlogPostType $blogPostType,
    ) {}

    public function resolve(): GraphQLReusableDatabaseQueryInterface
    {
        return new SelectBlogPosts($this->connectionPool);
    }

    public function toGraphQLField(): array
    {
        return [
            'type' => new ListOfType($this->blogPostType),
            'resolve' => $this->resolve(...),
        ];
    }
}

教程

社区

您可以在以下位置找到官方渠道

许可证

Resonance框架是开源软件,受MIT许可证许可。