PHPackages                             ogi/prompt - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ogi/prompt

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ogi/prompt
==========

A PHP package to convert class properties into XML-formatted prompts for LLM APIs.

v1.0.0(1y ago)018MITPHPPHP &gt;=7.4

Since Oct 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ogidimitrov/prompt)[ Packagist](https://packagist.org/packages/ogi/prompt)[ RSS](/packages/ogi-prompt/feed)WikiDiscussions main Synced 1mo ago

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

[![CI](https://github.com/ogidimitrov/prompt/actions/workflows/tests.yml/badge.svg)](https://github.com/ogidimitrov/prompt/actions)[![Packagist](https://camo.githubusercontent.com/312b445d962bd680ac6dd4241ae5190cc151c03c33579ac7369ee1198767c605/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f67692f70726f6d70742e737667)](https://camo.githubusercontent.com/312b445d962bd680ac6dd4241ae5190cc151c03c33579ac7369ee1198767c605/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f67692f70726f6d70742e737667)[![License](https://camo.githubusercontent.com/89f3261bf70c6adad9bdc52aa06bf5e380a30b8e7e8060abf5a7d25ac17c0669/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f67692f70726f6d70742e737667)](https://camo.githubusercontent.com/89f3261bf70c6adad9bdc52aa06bf5e380a30b8e7e8060abf5a7d25ac17c0669/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f67692f70726f6d70742e737667)[![GitHub issues](https://camo.githubusercontent.com/00972efe896c07decce039ae8949e69cf8d3fa89d5059fa31649a431eea90088/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6f676964696d6974726f762f70726f6d70742e737667)](https://camo.githubusercontent.com/00972efe896c07decce039ae8949e69cf8d3fa89d5059fa31649a431eea90088/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6f676964696d6974726f762f70726f6d70742e737667)[![GitHub stars](https://camo.githubusercontent.com/968afd4b6e69efa2387ed58c7bbe160735553ab030cffd29f50983cc61e999e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6f676964696d6974726f762f70726f6d70742e7376673f7374796c653d736f6369616c)](https://camo.githubusercontent.com/968afd4b6e69efa2387ed58c7bbe160735553ab030cffd29f50983cc61e999e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6f676964696d6974726f762f70726f6d70742e7376673f7374796c653d736f6369616c)[![GitHub last commit](https://camo.githubusercontent.com/cdc4a21154bd3a63c9e7a4f413340183c18b00076eb375d534f7e79f16fc9a89/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f6f676964696d6974726f762f70726f6d70742e737667)](https://camo.githubusercontent.com/cdc4a21154bd3a63c9e7a4f413340183c18b00076eb375d534f7e79f16fc9a89/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f6f676964696d6974726f762f70726f6d70742e737667)

Prompt
======

[](#prompt)

A PHP package that transforms class properties into XML-formatted prompts, ready for Large Language Model (LLM) API calls. It recursively handles arrays, collections, nested arrays, and nested `Prompt` instances, converting them into XML with appropriate tags.

---

Why XML Structured Prompts?
---------------------------

[](#why-xml-structured-prompts)

Structured prompts are essential when interacting with Large Language Models (LLMs) to ensure consistency, clarity, and optimal performance. XML provides a standardized way to structure data, making it an excellent choice for formatting prompts:

- **Consistency Across Models**: XML's hierarchical structure allows for consistent formatting of prompts, which can be beneficial when working with different models or APIs.
- **Improved Parsing**: XML is a widely accepted format that can be easily parsed and validated, reducing the likelihood of errors in prompt interpretation.
- **Flexibility**: XML's ability to represent complex nested data makes it suitable for constructing detailed and intricate prompts.
- **Readability**: Structured XML prompts are more readable and maintainable, aiding in debugging and prompt optimization.

By using XML-formatted prompts, developers can provide clear and unambiguous instructions to LLMs, potentially enhancing the models' understanding and response accuracy.

---

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Features](#features)
- [Usage](#usage)
    - [Basic Example](#basic-example)
    - [Handling Arrays](#handling-arrays)
    - [Using Collections](#using-collections)
    - [Nested Prompt Instances](#nested-prompt-instances)
    - [Using the GeneralPromptTemplate](#using-the-generalprompttemplate)
    - [Custom Objects with `toArray()` or `__toString__`](#custom-objects-with-toarray-or-__tostring__)
    - [Handling Special Cases](#handling-special-cases)
        - [Private and Protected Properties](#private-and-protected-properties)
        - [Null and Empty Values](#null-and-empty-values)
        - [Boolean Values](#boolean-values)
        - [Numeric Values](#numeric-values)
        - [Special Characters](#special-characters)
        - [Objects Without `toArray()` or `__toString__`](#objects-without-toarray-or-__tostring__)
        - [Circular References](#circular-references)
- [Testing](#testing)
- [Contributing](#contributing)
- [License](#license)
- [Additional Notes](#additional-notes)

---

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

[](#installation)

Install the package via Composer:

```
composer require ogi/prompt
```

---

Features
--------

[](#features)

- **Automatic XML Conversion**: Transforms public class properties into XML elements.
- **Array and Collection Handling**: Supports arrays, collections, and nested arrays, using `` and `` tags where appropriate.
- **Nested Prompt Instances**: Allows properties to be instances of `Prompt`, rendering them recursively within `` tags.
- **Custom Object Support**: Handles objects implementing `toArray()` or `__toString()`.
- **Edge Case Handling**: Manages special cases like private/protected properties, null values, booleans, numerics, special characters, and circular references.
- **Recursive Processing**: Recursively processes nested arrays and collections to any depth.
- **Easy Integration**: Extend the `Prompt` class and define your data; the `render()` method handles the rest.
- **Templates**: Extend the `GeneralPromptTemplate` class and leverage its setters to populate a versatile, general-use template prompt for your data.

---

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

Create a PHP class that extends the `Prompt` class provided by the package. Define your data using public properties.

```
