-
-
Notifications
You must be signed in to change notification settings - Fork 144
Add AI Package with OpenAI and Anthropic integrations #1948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Tresor-Kasenda
wants to merge
3
commits into
tempestphp:3.x
Choose a base branch
from
Tresor-Kasenda:feature/ai-package
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Exclude build/test files from the release | ||
| .github/ export-ignore | ||
| tests/ export-ignore | ||
| .gitattributes export-ignore | ||
| .gitignore export-ignore | ||
| phpunit.xml export-ignore | ||
| README.md export-ignore | ||
|
|
||
| # Configure diff output | ||
| *.view.php diff=html | ||
| *.php diff=php | ||
| *.css diff=css | ||
| *.html diff=html | ||
| *.md diff=markdown |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2024 Brent Roose brendt@stitcher.io | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "tempest/ai", | ||
| "description": "A component for integrating AI capabilities into Tempest applications.", | ||
| "license": "MIT", | ||
| "minimum-stability": "dev", | ||
| "require": { | ||
| "php": "^8.4", | ||
| "tempest/container": "dev-main", | ||
| "tempest/http-client": "dev-main", | ||
| "tempest/support": "dev-main" | ||
| }, | ||
| "require-dev": { | ||
| "phpunit/phpunit": "^12.2.3" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "Tempest\\AI\\": "src" | ||
| }, | ||
| "files": [ | ||
| "src/functions.php" | ||
| ] | ||
| }, | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "Tempest\\AI\\Tests\\": "tests" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| interface AIChat | ||
| { | ||
| /** | ||
| * Send a simple prompt and get a response. | ||
| */ | ||
| public function prompt(string $prompt): AIResponse; | ||
|
|
||
| /** | ||
| * Send multiple messages as a conversation. | ||
| * | ||
| * @param AIMessage[] $messages | ||
| */ | ||
| public function chat(array $messages): AIResponse; | ||
|
|
||
| /** | ||
| * Set the model to use for this request. | ||
| */ | ||
| public function withModel(string $model): self; | ||
|
|
||
| /** | ||
| * Set the temperature for this request. | ||
| */ | ||
| public function withTemperature(float $temperature): self; | ||
|
|
||
| /** | ||
| * Set the max tokens for this request. | ||
| */ | ||
| public function withMaxTokens(int $maxTokens): self; | ||
|
|
||
| /** | ||
| * Set a system prompt for this request. | ||
| */ | ||
| public function withSystemPrompt(string $systemPrompt): self; | ||
|
|
||
| /** | ||
| * Use a specific AI provider. | ||
| */ | ||
| public function using(AIProvider $provider): self; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| use Tempest\AI\Driver\AnthropicDriver; | ||
| use Tempest\AI\Driver\OpenAIDriver; | ||
| use Tempest\Container\Container; | ||
| use Tempest\Container\Initializer; | ||
| use Tempest\Container\Singleton; | ||
| use Tempest\HttpClient\HttpClient; | ||
|
|
||
| final class AIChatInitializer implements Initializer | ||
| { | ||
| #[Singleton] | ||
| public function initialize(Container $container): AIChat|GenericAIChat | ||
| { | ||
| $config = $container->get(AIConfig::class); | ||
| $httpClient = $container->get(HttpClient::class); | ||
|
|
||
| $chat = new GenericAIChat($config); | ||
|
|
||
| // Register available drivers | ||
| $chat->addDriver(new OpenAIDriver($httpClient, $config)); | ||
| $chat->addDriver(new AnthropicDriver($httpClient, $config)); | ||
|
|
||
| return $chat; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| use Tempest\AI\Config\AnthropicConfig; | ||
| use Tempest\AI\Config\OpenAIConfig; | ||
|
|
||
| final class AIConfig | ||
| { | ||
| public function __construct( | ||
| public AIProvider $defaultProvider = AIProvider::OPENAI, | ||
| public ?OpenAIConfig $openai = null, | ||
| public ?AnthropicConfig $anthropic = null, | ||
| public ?string $defaultModel = null, | ||
| public float $defaultTemperature = 0.7, | ||
| public int $defaultMaxTokens = 1024, | ||
| ) { | ||
| $this->openai ??= new OpenAIConfig(); | ||
| $this->anthropic ??= new AnthropicConfig(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| use Tempest\Container\Container; | ||
| use Tempest\Container\Initializer; | ||
| use Tempest\Container\Singleton; | ||
|
|
||
| final class AIConfigInitializer implements Initializer | ||
| { | ||
| #[Singleton] | ||
| public function initialize(Container $container): AIConfig | ||
| { | ||
| return new AIConfig(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| interface AIDriver | ||
| { | ||
| /** | ||
| * Send messages to the AI provider and get a response. | ||
| * | ||
| * @param AIMessage[] $messages | ||
| */ | ||
| public function chat( | ||
| array $messages, | ||
| ?string $model = null, | ||
| ?float $temperature = null, | ||
| ?int $maxTokens = null, | ||
| ): AIResponse; | ||
|
|
||
| /** | ||
| * Get the provider this driver handles. | ||
| */ | ||
| public function getProvider(): AIProvider; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| final readonly class AIMessage | ||
| { | ||
| public function __construct( | ||
| public MessageRole $role, | ||
| public string $content, | ||
| ) {} | ||
|
|
||
| public static function system(string $content): self | ||
| { | ||
| return new self(MessageRole::SYSTEM, $content); | ||
| } | ||
|
|
||
| public static function user(string $content): self | ||
| { | ||
| return new self(MessageRole::USER, $content); | ||
| } | ||
|
|
||
| public static function assistant(string $content): self | ||
| { | ||
| return new self(MessageRole::ASSISTANT, $content); | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'role' => $this->role->value, | ||
| 'content' => $this->content, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| enum AIProvider: string | ||
| { | ||
| case OPENAI = 'openai'; | ||
| case ANTHROPIC = 'anthropic'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI; | ||
|
|
||
| final readonly class AIResponse | ||
| { | ||
| public function __construct( | ||
| public string $content, | ||
| public ?string $model = null, | ||
| public ?int $promptTokens = null, | ||
| public ?int $completionTokens = null, | ||
| public ?int $totalTokens = null, | ||
| public ?string $finishReason = null, | ||
| public array $raw = [], | ||
| ) {} | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return $this->content; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI\Attribute; | ||
|
|
||
| use Attribute; | ||
|
|
||
| /** | ||
| * Mark a method as an AI-powered handler. | ||
| * | ||
| * The method's return type and docblock will be used to instruct the AI | ||
| * on how to structure the response. | ||
| * | ||
| * Usage: | ||
| * ```php | ||
| * #[AIHandler] | ||
| * public function summarize(string $text): string | ||
| * { | ||
| * return $this->ai->prompt("Summarize: {$text}")->content; | ||
| * } | ||
| * ``` | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD)] | ||
| final readonly class AIHandler | ||
| { | ||
| public function __construct( | ||
| public ?string $model = null, | ||
| public ?float $temperature = null, | ||
| public ?int $maxTokens = null, | ||
| ) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI\Attribute; | ||
|
|
||
| use Attribute; | ||
|
|
||
| /** | ||
| * Cache AI responses to avoid redundant API calls. | ||
| * | ||
| * Usage: | ||
| * ```php | ||
| * #[CacheAIResponse(ttl: 3600)] | ||
| * public function getFactAbout(string $topic): string | ||
| * { | ||
| * return $this->ai->prompt("Give me a fact about {$topic}")->content; | ||
| * } | ||
| * ``` | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD)] | ||
| final readonly class CacheAIResponse | ||
| { | ||
| public function __construct( | ||
| /** Time to live in seconds */ | ||
| public int $ttl = 3600, | ||
| /** Custom cache key prefix */ | ||
| public ?string $key = null, | ||
| ) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\AI\Attribute; | ||
|
|
||
| use Attribute; | ||
|
|
||
| /** | ||
| * Request structured JSON output from the AI. | ||
| * | ||
| * Usage: | ||
| * ```php | ||
| * #[JsonOutput] | ||
| * public function getColors(): array | ||
| * { | ||
| * $response = $this->ai->prompt('List 3 primary colors with hex codes'); | ||
| * return json_decode($response->content, true); | ||
| * } | ||
| * ``` | ||
| * | ||
| * With schema: | ||
| * ```php | ||
| * #[JsonOutput(schema: ['name' => 'string', 'hex' => 'string'])] | ||
| * public function getColor(): array | ||
| * ``` | ||
| */ | ||
| #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PARAMETER)] | ||
| final readonly class JsonOutput | ||
| { | ||
| public function __construct( | ||
| public ?array $schema = null, | ||
| ) {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package requires PHP
^8.4, but the root composer.json and other Tempest packages require^8.5. In a monorepo this will create inconsistent platform requirements; align this to^8.5unless there is a deliberate reason to support 8.4 across the repo.