PHPackages                             manuelkiessling/ai-tool-bridge - 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. [API Development](/categories/api)
4. /
5. manuelkiessling/ai-tool-bridge

ActiveLibrary[API Development](/categories/api)

manuelkiessling/ai-tool-bridge
==============================

Seamlessly empower your AI integration to interact with your own code and services

v0.1.1(2y ago)75441[2 PRs](https://github.com/manuelkiessling/php-ai-tool-bridge/pulls)GPL-3.0-or-laterPHPPHP ^8.2

Since May 24Pushed 2y ago4 watchersCompare

[ Source](https://github.com/manuelkiessling/php-ai-tool-bridge)[ Packagist](https://packagist.org/packages/manuelkiessling/ai-tool-bridge)[ Patreon](https://www.patreon.com/manuelkiessling)[ RSS](/packages/manuelkiessling-ai-tool-bridge/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

AI Tool Bridge for PHP
======================

[](#ai-tool-bridge-for-php)

Seamlessly empower your AI integration to interact with your own code and services
----------------------------------------------------------------------------------

[](#seamlessly-empower-your-ai-integration-to-interact-with-your-own-code-and-services)

Note: This product is independently developed and not affiliated, endorsed, or sponsored by OpenAI.

[![](https://camo.githubusercontent.com/7e128906ab6aff03055a882f91710f4e4d3efe7f3bbabb50c947e36717a80c56/68747470733a2f2f6d616e75656c2e6b696573736c696e672e6e65742f7068702d61692d746f6f6c2d6272696467652f5048505f41495f546f6f6c5f4272696467655f2d5f44656d6f5f566964656f2e676966)](https://camo.githubusercontent.com/7e128906ab6aff03055a882f91710f4e4d3efe7f3bbabb50c947e36717a80c56/68747470733a2f2f6d616e75656c2e6b696573736c696e672e6e65742f7068702d61692d746f6f6c2d6272696467652f5048505f41495f546f6f6c5f4272696467655f2d5f44656d6f5f566964656f2e676966)

Installation
------------

[](#installation)

Install this package as a dependency using [Composer](https://getcomposer.org).

```
composer require manuelkiessling/ai-tool-bridge
```

Overview
--------

[](#overview)

The major challenge when integrating AI into any project is managing interactions between the AI and the rest of your application. This becomes especially complex when the AI needs to make API calls to retrieve information or trigger actions.

AI Tool Bridge for PHP elegantly solves this problem by offering a straightforward interface to define "tool functions" that the AI can utilize when it needs to interact with external systems.

An important optimization is the library's capability to generate the required JSON structure for a tool function. It does so by requesting only the required values from the AI and then generating the JSON based on a provided JSON schema. This approach guarantees the validity of the final JSON that reaches your application code.

Key features of this library include:

- Facilitating the definition of tools that the AI can use for external interactions.
- Providing a robust prompt structure to guide the AI on when and how to use these tools.
- Ensuring that the tools are triggered with complete and correctly formatted JSON.

Example
-------

[](#example)

Let's assume you have an ecommerce business and you want to provide an AI chat interface which allows to browse your product catalog. To do so, you've decided to integrate with OpenAI's GPT-4 model through the ChatGPT API.

You will probably prompt the AI assistant along the lines of "You are a friendly and helpful shopping assistant that informs the user about our product catalog..." and so on.

However, you cannot add your whole product catalog to the prompt. Thus, when your user asks the AI to "tell me about some kitchen helpers on offer", you need to identify that at this point in the conversation, the AI needs information from your ecommerce backend systems (e.g. by making a request to your Product Search API with query "kitchen helpers"), you need to retrieve this information for the AI, and you need to provide the resulting information back to the AI assistant, which can then summarize the product information for the user.

The AI knows best when it is time to retrieve these information from the external world. Because making your own code listen to the conversation and having it guess when it is time to make the Product Search API call is complex and error prone, and makes the idea of using a powerful AI a bit pointless.

The best approach is to make the AI recognize that the time has come to talk to the outside world, and have it do so in a structured and unmistakable way.

The solution is to teach the AI, within the initial system prompt, that it has one or more tools at its disposal which it can use at will.

This is done by first writing a so-called tool function definition, like this:

```
