Skip to content

Commit

Permalink
Init project
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Aug 31, 2024
0 parents commit 33f4393
Show file tree
Hide file tree
Showing 22 changed files with 1,049 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
/vendor
/.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) llm-agents-php <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
142 changes: 142 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# JSON Schema Mapper for LLM Agents

[![PHP](https://img.shields.io/packagist/php-v/llm-agents/json-schema-mapper.svg?style=flat-square)](https://packagist.org/packages/llm-agents/json-schema-mapper)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/llm-agents/json-schema-mapper.svg?style=flat-square)](https://packagist.org/packages/llm-agents/json-schema-mapper)
[![Total Downloads](https://img.shields.io/packagist/dt/llm-agents/json-schema-mapper.svg?style=flat-square)](https://packagist.org/packages/llm-agents/json-schema-mapper)

This package is a super handy JSON Schema Mapper for the LLM Agents project.

## What's it all about?

This package gives you a nifty SchemaMapper that can:

- Convert PHP classes to JSON schemas
- Turn JSON data into PHP objects

## Class Diagram

Here's a quick look at how the main components fit together:

```mermaid
classDiagram
class SchemaMapperInterface {
<<interface>>
+toJsonSchema(string $class): array
+toObject(string $json, ?string $class): object
}
class SchemaMapper {
-generator: JsonSchemaGenerator
-mapper: TreeMapper
+toJsonSchema(string $class): array
+toObject(string $json, ?string $class): object
}
SchemaMapperInterface <|.. SchemaMapper
SchemaMapper --> JsonSchemaGenerator
SchemaMapper --> TreeMapper
```

## Getting Started

### Installation

First things first, let's get this package installed:

```bash
composer require llm-agents/json-schema-mapper
```

### Setting it up in Spiral

If you're using the Spiral framework (and why wouldn't you be? It's awesome!), you'll need to register the bootloader.

**Here's how:**

1. Open up your `app/src/Application/Kernel.php` file.
2. Add the `LLM\Agents\JsonSchema\Mapper\Integration\Spiral\SchemaMapperBootloader` to the `defineBootloaders()` method:

```php
class Kernel extends \Spiral\Framework\Kernel
{
// ...

public function defineBootloaders(): array
{
return [
// ... other bootloaders ...
\LLM\Agents\JsonSchema\Mapper\Integration\Spiral\SchemaMapperBootloader::class,
];
}
}
```

And that's it! The bootloader will take care of registering all the necessary components for you.

### Setting it up in Laravel

If you're using the Laravel framework, you'll need to register the Service provider.

**Here's how:**

Just register the `LLM\Agents\JsonSchema\Mapper\Integration\Laravel\SchemaMapperServiceProvider`

And that's it! The service provider will take care of registering all the necessary components for you.

## How to Use It

### Converting a PHP Class to JSON Schema

Let's say you have a `User` class and you want to get its JSON schema:

```php
use LLM\Agents\JsonSchema\Mapper\SchemaMapperInterface;

class UserController
{
public function __construct(
private SchemaMapperInterface $schemaMapper
) {}

public function getUserSchema(): array
{
return $this->schemaMapper->toJsonSchema(User::class);
}
}
```

### Converting JSON to a PHP Object

Got some JSON data that you want to turn into a PHP object? No problem:

```php
use LLM\Agents\JsonSchema\Mapper\SchemaMapperInterface;

class UserService
{
public function __construct(
private SchemaMapperInterface $schemaMapper
) {}

public function createUserFromJson(string $json): User
{
return $this->schemaMapper->toObject($json, User::class);
}
}
```

## Contributing

We'd love your help to make this package even better! Here's how you can contribute:

1. Fork the repository
2. Write some awesome code
3. Create a new Pull Request

Please make sure your code follows PSR-12 coding standards and include tests for any new features.

## License

This package is open-sourced software licensed under the MIT license. Feel free to use it, modify it, and share it!

---

That's all, folks! If you have any questions or run into any issues, don't hesitate to open an issue on GitHub.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "llm-agents/cli-chat",
"description": "A simple CLI chat application",
"license": "MIT",
"require": {
"php": "^8.2",
"symfony/console": "^7.0",
"ramsey/uuid": "^4.0",
"llm-agents/agents": "^1.0",
"llm-agents/openai-client": "^1.0",
"llm-agents/json-schema-mapper": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^11.3"
},
"autoload": {
"psr-4": {
"LLM\\Agents\\Chat\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"LLM\\Tests\\": "tests/src"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
127 changes: 127 additions & 0 deletions src/AgentExecutorBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

namespace LLM\Agents\Chat;

use LLM\Agents\Agent\AgentExecutor;
use LLM\Agents\Agent\Exception\InvalidBuilderStateException;
use LLM\Agents\Agent\Execution;
use LLM\Agents\LLM\OptionsFactoryInterface;
use LLM\Agents\LLM\OptionsInterface;
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
use LLM\Agents\LLM\Prompt\Chat\Prompt;
use LLM\Agents\OpenAI\Client\Option;
use LLM\Agents\OpenAI\Client\StreamChunkCallbackInterface;

final class AgentExecutorBuilder
{
private ?Prompt $prompt = null;
private ?string $agentKey = null;
private array $sessionContext = [];
private OptionsInterface $options;

public function __construct(
private readonly AgentExecutor $executor,
OptionsFactoryInterface $optionsFactory,
) {
$this->options = $optionsFactory->create();
}

public function withStreamChunkCallback(StreamChunkCallbackInterface $callback): self
{
$self = clone $this;
$self->options = $this->options->with(Option::StreamChunkCallback, $callback);

return $self;
}

public function withPrompt(Prompt $prompt): self
{
$self = clone $this;
$self->prompt = $prompt;

return $self;
}

public function withAgentKey(string $agentKey): self
{
$self = clone $this;
$self->agentKey = $agentKey;

return $self;
}

public function withSessionContext(array $sessionContext): self
{
$self = clone $this;
$self->sessionContext = $sessionContext;

return $self;
}

public function withMessage(MessagePrompt $message): self
{
if ($this->prompt === null) {
throw new InvalidBuilderStateException('Cannot add message without a prompt');
}

$this->prompt = $this->prompt->withAddedMessage($message);

return $this;
}

public function ask(string|\Stringable $prompt): Execution
{
if ($this->agentKey === null) {
throw new InvalidBuilderStateException('Agent key is required');
}

if ($this->prompt !== null) {
$prompt = $this->prompt->withAddedMessage(
MessagePrompt::user(
prompt: $prompt,
),
);
}

$execution = $this->executor->execute(
agent: $this->agentKey,
prompt: $prompt,
options: $this->options,
sessionContext: $this->sessionContext,
);

$this->prompt = $execution->prompt;

return $execution;
}

public function continue(): Execution
{
if ($this->agentKey === null) {
throw new InvalidBuilderStateException('Agent key is required');
}

$execution = $this->executor->execute(
agent: $this->agentKey,
prompt: $this->prompt,
options: $this->options,
sessionContext: $this->sessionContext,
);

$this->prompt = $execution->prompt;

return $execution;
}

public function __clone()
{
$this->prompt = null;
}

public function getPrompt(): ?Prompt
{
return $this->prompt;
}
}
Loading

0 comments on commit 33f4393

Please sign in to comment.