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.
Download noah-medra/prompt-builder
More information about noah-medra/prompt-builder
Files in noah-medra/prompt-builder
Package prompt-builder
Short Description Laravel package to build dynamic AI prompts
License MIT
Rated 1.00 based on 1 reviews
Informations about the package prompt-builder
PromptBuilder
PromptBuilder is a Laravel package that allows you to create and execute AI prompts in a flexible and structured way. This package enables you to generate dynamic queries, customize parameters, and manage conversation history to improve the relevance of responses.
Installation
Composer
To install PromptBuilder via Composer, run the following command:
Usage
Creating a PromptBuilder object
Start by instantiating a PromptBuilder object:
Defining the Driver (Processing Engine)
By default, PromptBuilder uses the OllamaDriver. If you want to use another driver, you can specify it using the driver() method.
Adding instructions (with Sub-Instructions and Conditions)
Instructions are rules or constraints you want to apply to the AI's generated response. These instructions can have sub-instructions or be condition-dependent. You can chain instructions infinitely and even create nested conditions.
Here is an example where instructions can contain sub-instructions, and conditional logic can be applied using the when() method.
Example with Instructions and Sub-Instructions:
In this example:
- Main instructions: The first two instructions introduce the financial history topic and explain the review process.
- Conditional instructions: The
when()method checks a condition (in this case,true), and if true, additional sub-instructions are added (e.g., a summary of the financial history).
Adding dynamic parameters
You can pass custom parameters to your prompt with the withParams() method:
Defining a context
You can add additional context to guide the AI in generating the response.
Asking a question
You can ask a question that will be included in the final prompt.
Managing history
If you want the AI to use previous conversation history to provide more contextual responses, enable the useHistory() option.
Generating and retrieving the response
Once all instructions and parameters are defined, you can generate the prompt and obtain the AI's response by calling the getOutput() method:
Handling JSON responses
If you want the AI's response to be formatted as JSON, use the expectResponseFormat() method.
Driver: How It Works (Input/Output)
The Driver is responsible for handling the input (your prompt) and generating the output (the AI's response). Each driver implements the DriverInterface and provides a process() method that takes a BuilderInput object, processes the prompt, and returns a BuilderOutput.
For example, the OllamaDriver sends the prompt to a local Ollama service and receives the AI's response.
Here is an example of the OllamaDriver:
In this example:
- The
process()method receives aBuilderInputobject which contains the prompt text. - The
executePrompt()method sends the prompt to a remote API (Ollama) and receives the AI response. - The response is returned as a
BuilderOutputobject.
Complete Example
Here is a complete example of how to use PromptBuilder:
Features
- Driver flexibility: Easily add or replace text-processing engines.
- Dynamic instructions with sub-instructions and conditions: Add custom instructions that can have nested sub-instructions, and create complex prompts with conditional instructions using the
when()method. - History management: Store previous conversations to provide context.
- JSON response support: Format the AI's responses in JSON for easy integration.
- Dynamic parameters: Pass custom parameters to your prompts.
Customization
You can easily extend PromptBuilder by creating your own drivers or by customizing the PromptDriverInterface. To do this, simply create a class that implements the PromptDriverInterface and pass it to the driver() method.
Contributions
Contributions are welcome! If you have ideas to improve the package, feel free to submit an issue or a pull request.