distantmagic / resonance
PHP Framework with AI capabilities.
Installs: 248
Dependents: 1
Suggesters: 0
Security: 0
Stars: 202
Watchers: 7
Forks: 9
Open Issues: 0
Requires
- php: >=8.2
- beberlei/assert: ^3.3
- defuse/php-encryption: ^2.4
- distantmagic/graphql-swoole-promise-adapter: ^0.1.1
- distantmagic/swoole-futures: ^0.1.2
- doctrine/dbal: ^4.0
- doctrine/migrations: ^3.6
- doctrine/orm: ^3.0
- doctrine/sql-formatter: ^1.2
- dragonmantank/cron-expression: ^3.3
- dunglas/doctrine-json-odm: ^1.4
- ezyang/htmlpurifier: ^4.16
- grpc/grpc: ^1.57
- hyperf/coroutine: ^3.1
- hyperf/grpc-client: ^3.1
- league/commonmark: ^2.4
- league/oauth2-client: ^2.7
- league/oauth2-server: ^8.5
- nette/php-generator: ^4.1
- nicmart/tree: ^0.8.0
- nyholm/psr7: ^1.8
- nyholm/psr7-server: ^1.1
- psr/container: ^2.0
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/log: ^3.0
- ramsey/uuid: ^4.7
- rubix/ml: ^2.5
- symfony/console: ^7.0
- symfony/doctrine-bridge: ^7.0
- symfony/event-dispatcher: ^7.0
- symfony/expression-language: ^7.0
- symfony/filesystem: ^7.0
- symfony/finder: ^7.0
- symfony/http-client: ^7.0
- symfony/http-foundation: ^7.0
- symfony/mailer: ^7.0
- symfony/messenger: ^7.0
- symfony/routing: ^7.0
- symfony/uid: ^7.0
- symfony/yaml: ^7.0
- twig/cache-extra: ^3.7
- twig/twig: ^3.7
- webonyx/graphql-php: ^15.6
Requires (Dev)
- mockery/mockery: ^1.6
- phpunit/phpunit: ^11.0
- swoole/ide-helper: ^5.1
- symfony/var-dumper: ^7.0
Suggests
- ext-ds: For better memory management
- ext-igbinary: For better object serialization
- ext-intl: Date formatting
- ext-mailparse: To integrate with Postfix
- ext-openssl: To generate application keys
- ext-readline: Unlocks some console features
- ext-redis: HTTP Sessions driver
- ext-uuid: Faster UUID generation
- dev-master
- v0.31.0
- v0.30.0
- v0.29.0
- v0.28.2
- v0.28.1
- v0.28.0
- v0.27.4
- v0.27.3
- v0.27.2
- v0.27.0
- v0.26.0
- v0.25.2
- v0.25.1
- v0.25.0
- v0.24.0
- v0.23.0
- v0.22.1
- v0.22.0
- v0.21.0
- v0.20.1
- v0.20.0
- v0.19.1
- v0.19.0
- v0.18.1
- v0.18.0
- v0.17.0
- v0.16.0
- v0.15.0
- v0.14.1
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.2
- v0.11.1
- v0.11.0
- v0.10.0
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-experiment-backends
- dev-fix-events
This package is not auto-updated.
Last update: 2024-12-23 23:46:11 UTC
README
About Resonance
Resonance is designed from the ground up to facilitate interoperability and messaging between services in your infrastructure and beyond. It provides AI capabilities, has a built in web server and integrates with llama.cpp.
Takes full advantage of asynchronous PHP. Built on top of Swoole.
Why Resonance?
Predictable Performance
Resonance is designed with a few priorities: no memory leaks, blocking operations, and garbage collector surprises.
Most of the internals are read-only and stateless. After the application startup, nothing disturbs JIT and opcode (Reflection is only used during the application startup), so there are no surprise slowdowns during runtime.
Opinionated
All the libraries under the hood have been thoroughly tested to ensure they work together correctly, complement each other, and work perfectly under async environments.
For example, Resonance implements custom Doctrine drivers, so it uses Swoole's connection pools.
Resolves Input/Output Issues
Resonance is designed to handle IO-intensive tasks, such as serving Machine Learning models, handling WebSocket connections, and processing long-running HTTP requests.
It views modern applications as a mix of services that communicate with each other asynchronously, including AI completions and ML inferences, so it provides a set of tools to make this communication as easy as possible.
Complete Package
Resonance includes everything you need to build a modern web application, from the HTTP server to the AI capabilities.
It provides security features, HTML templating, integration with open-source LLMs, and provides capability to serve ML models.
Documentation
https://resonance.distantmagic.com/
Installation
It's best to install Resonance by using Composer's create-project command:
composer create-project distantmagic/resonance-project my-project
Resonance requires minimum 8.2 version of PHP, as well as Data Structures and Swoole extensions. Read more about required and recommended extensions, as well as other installation methods in our installation guide.
First-time use
You'll need to create a config.ini
file after installing the project (config.ini.example
is provided) and then use bin/resonance.php
as an entry point.
Running the server
php bin/resonance.php
serve starts the built-in HTTP server. If you need to, you can generate the SSL Certificate for Local Development.
Features
Chat with Open-Source LLMs
Create prompt controllers to directly answer user's prompts.
LLM takes care of determining user's intention, you can focus on taking an appropriate action.
#[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(); } }
Asynchronous Where it Matters
Respond asynchronously to incoming RPC or WebSocket messages (or both combined) with little overhead.
You can set up all the asynchronous features using attributes. No elaborate configuration is needed.
#[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, )); } }
Simple Things Remain Simple
Writing HTTP controllers is similar to how it's done in the synchronous code.
Controllers have new exciting features that take advantage of the asynchronous environment.
#[RespondsToHttp( method: RequestMethod::GET, pattern: '/', )] function Homepage(ServerRequestInterface $request, ResponseInterface $response): TwigTemplate { return new TwigTemplate('website/homepage.twig'); }
Consistency is Key
You can keep the same approach to writing software no matter the size of your project.
There are no growing central configuration files or service dependencies registries. Every relation between code modules is local to those modules.
#[ListensTo(HttpServerStarted::class)] #[Singleton(collection: SingletonCollection::EventListener)] final readonly class InitializeErrorReporting extends EventListener { public function handle(object $event): void { // ... } }
Promises in PHP
Resonance provides a partial implementation of Promise/A+ spec to handle various asynchronous tasks.
$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 Out of the Box
You can build elaborate GraphQL schemas by using just the PHP attributes.
Resonance takes care of reusing SQL queries and optimizing the resources' usage.
All fields can be resolved asynchronously.
#[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(...), ]; } }
Tutorials
- 'Hello, World' with Resonance
- Session-Based Authentication
- Building a Basic GraphQL Schema
- How to Serve LLM Completions (With llama.cpp)?
- How to Create LLM WebSocket Chat with llama.cpp?
- Semi-Scripted Conversational Applications
Community
Feel free to use GitHub discussions and other pages related to the repository.
License
The Resonance framework is open-sourced software licensed under the MIT license.