PHPackages                             elliotjreed/ai - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. elliotjreed/ai

ActiveLibrary

elliotjreed/ai
==============

A library for interacting with ChatGPT and Claude AI

2.2.0(10mo ago)0864↓43.3%MITPHPPHP ^8.3CI passing

Since Mar 1Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/elliotjreed/php-ai)[ Packagist](https://packagist.org/packages/elliotjreed/ai)[ Docs](https://github.com/elliotjreed/ai)[ RSS](/packages/elliotjreed-ai/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (7)Versions (7)Used By (0)

[![Contributor Covenant](https://camo.githubusercontent.com/2757a9db291c5ceda172e31d4fa5f3c4048a6e6257ee0b7113f80de277074b91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6e7472696275746f72253230436f76656e616e742d76322e3025323061646f707465642d6666363962342e737667)](code-of-conduct.md)

A library for interacting with Anthropic / Claude AI and OpenAI / ChatGPT.

**PHP 8.3** and above is supported.

- [Usage](#usage)
    - [Anthropic Claude AI](#anthropic-claude-ai)
        - [Text prompts](#text-prompts)
        - [System (developer or role) prompt](#system-developer-or-role-prompt)
        - [Structured prompt](#structured-prompt)
        - [History](#history)
        - [Images](#images)
    - [OpenAI ChatGPT](#openai-chatgpt)
        - [Text prompts](#text-prompts-1)
        - [System (developer or role) prompt](#system-developer-or-role-prompt-1)
        - [Structured prompt](#structured-prompt-1)
        - [History](#history-1)
        - [Images](#images-1)
    - [Error handling](#error-handling)
- [Testing](#testing)
    - [Simple mock](#simple-mock)
    - [Advanced](#advanced)
- [Development](#development)
    - [Getting Started](#getting-started)
        - [Installing Composer](#installing-composer)
        - [Installing](#installing)
    - [Running the Tests](#running-the-tests)
        - [Unit tests](#unit-tests)
            - [Debugging](#debugging)
    - [Code formatting](#code-formatting)
        - [Running everything](#running-everything)
        - [Outdated dependencies](#outdated-dependencies)
        - [Validating Composer configuration](#validating-composer-configuration)
        - [Running via GNU Make](#running-via-gnu-make)
        - [Running the tests on a Continuous Integration platform (eg. Github Actions)](#running-the-tests-on-a-continuous-integration-platform-eg-github-actions)
            - [Github Actions](#github-actions)

Usage
=====

[](#usage)

Install via [Composer](https://getcomposer.org/):

```
composer require elliotjreed/ai
```

There are two classes, one for Claude AI, and one for ChatGPT. Each extent the abstract `Prompt` class and are designed to be interoperable.

```
$claude = new ElliotJReed\AI\Claude\Prompt('API KEY', 'claude-3-5-haiku-latest');
$chatGPT = new ElliotJReed\AI\ChatGPT\Prompt('API KEY', 'gpt-4o-mini');
```

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:

```
$claude = new ElliotJReed\AI\Claude\Prompt('API KEY', 'claude-3-5-haiku-latest', new \GuzzleHttp\Client());
$chatGPT = new ElliotJReed\AI\ChatGPT\Prompt('API KEY', 'gpt-4o-mini', new \GuzzleHttp\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:

```
  guzzle.client.ai:
    class: GuzzleHttp\Client
    arguments:
      - {
        timeout: 10,
        headers: {
          'User-Agent': 'My Symfony Project'
        }
      }

  ElliotJReed\AI\Claude\Prompt:
    class: ElliotJReed\AI\Claude\Prompt
    arguments:
      $apiKey: '%env(string:CLAUDE_API_KEY)%'
      $model: 'claude-3-5-haiku-latest'
      $client: '@guzzle.client.ai'

  ElliotJReed\AI\ChatGPT\Prompt:
    class: ElliotJReed\AI\ChatGPT\Prompt
    arguments:
      $apiKey: '%env(string:CHATGPT_API_KEY)%'
      $model: 'gpt-4o-mini'
      $client: '@guzzle.client.ai'
```

Here's an example of a Symfony integration in the `services.yaml` file using the interface:

```
  guzzle.client.ai:
    class: GuzzleHttp\Client
    arguments:
      - {
        timeout: 10,
        headers: {
          'User-Agent': 'My Symfony Project'
        }
      }

  ElliotJReed\AI\PromptInterface:
    class: ElliotJReed\AI\ChatGPT\Prompt
    arguments:
      $apiKey: '%env(string:CHATGPT_API_KEY)%'
      $model: 'gpt-4o-mini'
      $client: '@guzzle.client.ai'
```

By using the `PromptInterface` you can use it in your application like:

```
