Download the PHP package elliotjreed/ai without Composer
On this page you can find all versions of the php package elliotjreed/ai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elliotjreed/ai
More information about elliotjreed/ai
Files in elliotjreed/ai
Package ai
Short Description A library for interacting with ChatGPT and Claude AI
License MIT
Homepage https://github.com/elliotjreed/ai
Informations about the package ai
A library for interacting with Anthropic / Claude AI and OpenAI / ChatGPT.
PHP 8.3 and above is supported.
- Usage
- Anthropic Claude AI
- Text prompts
- System (developer or role) prompt
- Structured prompt
- History
- Images
- OpenAI ChatGPT
- Text prompts
- System (developer or role) prompt
- Structured prompt
- History
- Images
- Error handling
- Anthropic Claude AI
- Testing
- Simple mock
- Advanced
- Development
- Getting Started
- Installing Composer
- Installing
- Running the Tests
- Unit tests
- Debugging
- Unit tests
- Code formatting
- Running everything
- Outdated dependencies
- Validating Composer configuration
- Running via GNU Make
- Running the tests on a Continuous Integration platform (eg. Github Actions)
- Github Actions
- Getting Started
Usage
Install via Composer:
There are two classes, one for Claude AI, and one for ChatGPT. Each extent the abstract Prompt class and are designed to be interoperable.
Each take the first argument in the constructor as your API key, and the second argument as the model you want to use.
You can optionally provide a Guzzle HTTP client:
This could be useful where you are using a framework such as Symfony, you could autowire the service and reference a configured Guzzle client.
Here's an example of a Symfony integration in the services.yaml file:
Here's an example of a Symfony integration in the services.yaml file using the interface:
By using the PromptInterface you can use it in your application like:
The following two sections show examples for both Anthropic / Claude and OpenAI / ChatGPT - they take the same request and are functionally the same. The same examples are shown for both for simplicity.
Anthropic Claude AI
Text prompts
For text-based prompts you can either set a plain text prompt, or use the included StructuredPrompt to use a light prompting framework and format in an LLM-friendly way.
For a really simple request and response:
System (developer or role) prompt
You can also include a system prompt as either a string or a StructuredPrompt. This takes priority in terms of instructions over the user prompts. For example, you could let the LLM know what role it is taking on.
Structured prompt
You can provide a StructuredPrompt too. A StructuredPrompt wraps context, instructions, user input, data, and examples in XML tags before sending the request to the AI API. This can help the LLMs understand a bit better, and can be particularly useful when dealing with potentially untrusted user input (eg. from a web form or chat bot implementation)/
Setting the temperature (between 0 and 1, basically how "creative" you want the AI to be), and the maximum tokens to use (recommended if the user input is from a indirect source, for example an online chatbot):
History
If you want to keep a conversation going (like you would on ChatGPT or Claude's website or app), you can pass through the history from the previous response to a new request:
Images
You can send image data, by either providing URL or a base64 encoded image file.
As with the text prompts you can also retain the history between requests.
OpenAI ChatGPT
Text prompts
For text-based prompts you can either set a plain text prompt, or use the included StructuredPrompt to use a light prompting framework and format in an LLM-friendly way.
For a really simple request and response:
System (developer or role) prompt
You can also include a system prompt as either a string or a StructuredPrompt. This takes priority in terms of instructions over the user prompts. For example, you could let the LLM know what role it is taking on.
Structured prompt
You can provide a StructuredPrompt too. A StructuredPrompt wraps context, instructions, user input, data, and examples in XML tags before sending the request to the AI API. This can help the LLMs understand a bit better, and can be particularly useful when dealing with potentially untrusted user input (eg. from a web form or chat bot implementation)/
Setting the temperature (between 0 and 1, basically how "creative" you want the AI to be), and the maximum tokens to use (recommended if the user input is from a indirect source, for example an online chatbot):
History
If you want to keep a conversation going (like you would on ChatGPT or Claude's website or app), you can pass through the history from the previous response to a new request:
Images
You can send image data, by either providing URL or a base64 encoded image file.
As with the text prompts you can also retain the history between requests.
Error handling
All exceptions extend the base AIException.
Testing
Simple mock
For convenience, there are two mock classes which can be used for simple unit tests, once for Claude (ElliotJReed\AI\Double\ClaudePromptMock) and one for ChatGPT(ElliotJReed\AI\Double\ChatGPTPromptMock).
One for each is provided as the underlying API requests differ, and the History between the two providers differ, even if the Request and usage is the same.
Advanced
For more complex requirements, you could use the Claude or ChatGPT Prompt classes directly and mock the raw response using Guzzle.
Refer to the Anthropic and OpenAI API documentation for raw response examples. Or view the source code of this library.
Development
Getting Started
PHP 8.3 or above and Composer is expected to be installed.
Installing Composer
For instructions on how to install Composer visit getcomposer.org.
Installing
After cloning this repository, change into the newly created directory and run:
or if you have installed Composer locally in your current directory:
This will install all dependencies needed for the project.
Henceforth, the rest of this README will assume composer is installed globally (ie. if you are using composer.phar you will need to use composer.phar instead of composer in your terminal / command-line).
Running the Tests
Unit tests
Unit testing in this project is via PHPUnit.
All unit tests can be run by executing:
Debugging
To have PHPUnit stop and report on the first failing test encountered, run:
Code formatting
A standard for code style can be important when working in teams, as it means that less time is spent by developers processing what they are reading (as everything will be consistent).
Code formatting is automated via PHP-CS-Fixer. PHP-CS-Fixer will not format line lengths which do form part of the PSR-2 coding standards so these will product warnings when checked by PHP Code Sniffer.
These can be run by executing:
Running everything
All the tests can be run by executing:
Outdated dependencies
Checking for outdated Composer dependencies can be performed by executing:
Validating Composer configuration
Checking that the composer.json is valid can be performed by executing:
Running via GNU Make
If GNU Make is installed, you can replace the above composer command prefixes with make.
All the tests can be run by executing:
Running the tests on a Continuous Integration platform (eg. Github Actions)
Specific output formats better suited to CI platforms are included as Composer scripts.
To output unit test coverage in text and Clover XML format (which can be used for services such as Coveralls):
To output PHP-CS-Fixer (dry run) and PHPCS results in checkstyle format (which GitHub Actions will use to output a readable format):
Github Actions
Look at the example in .github/workflows/php.yml.
Built With
License
This project is licensed under the MIT License - see the LICENCE.md file for details.
All versions of ai with dependencies
ext-fileinfo Version *
ext-libxml Version *
ext-simplexml Version *
guzzlehttp/guzzle Version ^7.0