Download the PHP package noah-medra/prompt-builder without Composer

On this page you can find all versions of the php package noah-medra/prompt-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?
noah-medra/prompt-builder
Rate from 1 - 5
Rated 1.00 based on 1 reviews

Informations about the package prompt-builder

PromptBuilder

Tests PHP

PromptBuilder composes structured AI prompts with a fluent, query-builder-style API and executes them against an LLM (Ollama today). Its defining idea is a clean split between three concerns, so you can build a prompt once and render it however your target model prefers, in whatever language you want, and send it through whichever driver you like:

Contents

Requirements

Installation

This is a library: composer.lock and vendor/ are intentionally not shipped with the source. Your application resolves the dependency against its own lock.

Quick start

toPrompt() above prints:

A runnable, network-free demo lives in examples/basic-usage.php.

Architecture

Each layer has one job and never reaches into the next. That's what lets you unit test composition in isolation, render the same prompt differently per model, and swap execution backends.

Layer Classes Responsibility
Compose PromptBuilder โ†’ PromptSpec, Instructions\Instruction, Examples\Example Build the prompt as plain data. Pure, no I/O, no framework.
Render Rendering\RendererInterface โ†’ TextRenderer, ChatMessagesRenderer, XmlRenderer Turn a PromptSpec into a string or chat-message array. Pure and localizable.
Execute Drivers\PromptDriverInterface โ†’ Drivers\OllamaDriver, Drivers\Laravel\OllamaDriver Render the spec and send it to a model. The only layer that does I/O.

PromptSpec is the hand-off: the builder fills it, a renderer reads it, a driver sends it. You can grab it directly with getSpec() for full control.

Composition

Every method below only mutates the internal PromptSpec โ€” no I/O, no driver.

Method Purpose
persona(string) Who the model should act as
context(string) Background information
instruction(string, ?Closure) A neutral instruction (optionally with nested sub-instructions)
must(string, ?Closure) A positive constraint, rendered with a [Required] marker
mustNot(string, ?Closure) A negative constraint, rendered with a [Forbidden] marker
example(string $input, string $output) A few-shot input/output pair
expectResponseFormat(string $json) Ask for a specific JSON output shape (throws if the sample isn't valid JSON)
withParams(array) / setParams(array) Values for {placeholder} interpolation
language(string) / locale(string) Language of the rendered labels (default English)
ask(string) The actual question
when(bool, Closure $ifTrue, ?Closure $ifFalse) Conditional composition
getSpec() Escape hatch to the raw PromptSpec

Nested instructions

instruction(), must() and mustNot() accept a closure to add nested sub-instructions. Each ->add() appends a sibling at the same depth; pass a closure to add() to go one level deeper.

Parameter interpolation

Any text you pass supports {key} and {nested.key} placeholders, resolved from withParams(). Unknown placeholders are left untouched (not silently emptied) so typos are easy to spot.

Conditionals

Structured JSON output

Adds an explicit "answer only with valid JSON matching this shape" instruction to the rendered prompt, and validates that the sample you pass is itself valid JSON (throwing otherwise).

Rendering

toPrompt() renders the composed prompt so you can iterate on quality for free. It defaults to TextRenderer; pass any renderer to get a different shape.

Renderer Output Use for
TextRenderer Single string with # Role, # Context, โ€ฆ sections Completion-style APIs, previews
ChatMessagesRenderer {role, content} message array (system + history + question) Chat APIs (Ollama /api/chat, OpenAI, Anthropic)
XmlRenderer Well-formed, escaped XML with explicit tags Models that follow XML-delimited structure better

Write your own by implementing Rendering\RendererInterface โ€” it receives a PromptSpec and returns a string or a {role, content} array.

Language (i18n)

The section labels the renderer emits (# Role, [Required], Example n:, the JSON-output instruction, โ€ฆ) are localized. English is the default; pick another language per builder:

Bundled locales: en (default), es, fr, de, zh, ar. An unknown locale, or a key missing in a locale, falls back to English. Only the labels are translated โ€” your persona/context/instruction text is emitted verbatim, and XmlRenderer tag names stay English on purpose (they're structural).

Translation goes through a framework-free Translation\TranslatorInterface. The default Translation\ArrayTranslator reads bundled PHP language files with no framework. Point it at your own directory, or implement the interface for full control:

Using Laravel's translator

Inside Laravel, the service provider registers the bundled strings under the promptbuilder translation namespace. Publish them to customize:

Then render through Laravel's translator (which follows the app locale and any overrides you published) with Translation\Laravel\LaravelTranslator:

Execution

If you call process() without setting a driver, the standalone Drivers\OllamaDriver is used by default.

BuilderOutput decodes JSON responses and lets you pluck values with dotted paths, or grab the raw body:

Choosing an Ollama driver

Two interchangeable implementations ship with the package:

Driver Built on Use when
Drivers\OllamaDriver Guzzle directly You want a framework-agnostic driver that works in any PHP script, no booted Laravel app. This is the default. Accepts an injectable Guzzle client for testing.
Drivers\Laravel\OllamaDriver Laravel Http facade You're already in a Laravel app and want Http::fake() in your tests. Requires a booted application.

Both accept model, endpoint, a renderer, and a timeoutSeconds:

Writing a custom driver

Implement Drivers\PromptDriverInterface. A driver receives a PromptSpec directly (never a pre-rendered string) and picks its own renderer:

Conversation history

Call useHistory() to enable multi-turn memory. Prior turns are loaded into the prompt, and process() appends this turn's question and the model's reply back into the store โ€” so the next builder using the same store sees the full exchange.

Store Persistence Notes
History\InMemoryHistoryStore Process lifetime Default, framework-free
History\Laravel\CacheHistoryStore Across requests, via the Laravel cache Pass a conversation id: new CacheHistoryStore('conv-42')

Implement History\HistoryStoreInterface (all(), push(), clear()) for a custom backend. You can also seed turns manually with setHistory([['role' => 'user', 'content' => 'โ€ฆ']]).

Laravel integration

Everything above works without Laravel. When you are in a Laravel app, these opt-in conveniences light up automatically via package auto-discovery (PromptBuilderServiceProvider):

Testing

The Unit suite covers the framework-free core (composition, every renderer, translation, history) with plain PHPUnit. The Feature suite uses Orchestra Testbench to boot a real Laravel app for the facade, the Laravel driver (via Http::fake()), the cache history store and the translator bridge. The framework-agnostic Ollama driver is tested with a Guzzle MockHandler, asserting the composed prompt is really what gets sent.

License

Released under the MIT License โ€” see LICENCE.txt.


All versions of prompt-builder with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0 || ^11.0 || ^12.0
guzzlehttp/guzzle Version ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package noah-medra/prompt-builder contains the following files

Loading the files please wait ...