Download the PHP package ami-praha/ai-item-finder without Composer
On this page you can find all versions of the php package ami-praha/ai-item-finder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ami-praha/ai-item-finder
More information about ami-praha/ai-item-finder
Files in ami-praha/ai-item-finder
Package ai-item-finder
Short Description Laravel package for finding the most similar item from a list using LLM
License MIT
Informations about the package ai-item-finder
AI Item Finder
A Laravel package that uses Large Language Models (LLM) to find the closest matching item from a list. This package leverages OpenAI's GPT models to perform intelligent matching based on semantic similarity rather than simple string matching.
Features
- Find the most similar item from a list using AI
- Configurable OpenAI models (GPT-4.1, GPT-5, etc.)
- Easy-to-use fluent interface
- Laravel facade support
- Customizable system instructions
- Confidence scoring with AI-powered match evaluation
- Optional "no result" mode to return
nullwhen confidence is below threshold - List description support for more accurate matching context
Requirements
- PHP 8.1 or higher
- Laravel 10.x or 11.x
- OpenAI API key
Installation
Install the package via Composer:
Publish the configuration file:
Add your OpenAI API key to your .env file:
Optionally, you can also set the model:
Usage
Basic Usage
Using the Service Class Directly
Advanced Usage with Custom Instructions
Using Different Models
Custom System Message
If you need complete control over the AI's behavior, you can set a custom system message:
Using List Description for Better Accuracy
You can provide a description of what the list items represent to help the AI make more accurate matches:
Handling Low-Confidence Matches
By default, the package evaluates match confidence and returns null if the match confidence is below 80%. You can customize this behavior:
Configuration
The configuration file config/ai-item-finder.php contains the following options:
Performance Considerations
Two-Step Confidence Evaluation
By default, when confidence scoring is enabled (setAllowNoResult(true)), the package uses a two-step approach:
- First API call: Finds the best matching item from your list
- Second API call: Evaluates the confidence score for that specific match
This separation provides more accurate and unbiased confidence scores because:
- The matching task focuses solely on finding the best relative match
- The confidence evaluation provides an independent, absolute quality assessment
- Better calibrated scores (avoiding confirmation bias where the LLM justifies its own choice)
However, this comes with performance tradeoffs:
Cost Implications
- With confidence scoring enabled (
setAllowNoResult(true)): 2 API calls per search - With confidence scoring disabled (
setAllowNoResult(false)): 1 API call per search
Example costs with gpt-4o-mini (approximate):
- Single search with confidence: ~$0.0002 - $0.002
- Single search without confidence: ~$0.0001 - $0.001
- 1,000 searches/day with confidence: ~$0.20 - $2.00/day
- 1,000 searches/day without confidence: ~$0.10 - $1.00/day
Latency Implications
- With confidence scoring: ~2-4 seconds (two sequential API calls)
- Without confidence scoring: ~1-2 seconds (single API call)
Optimizing for High-Volume Scenarios
If you're processing many searches or need faster response times, disable confidence scoring:
When to Use Confidence Scoring
Enable confidence scoring when:
- Quality and accuracy are more important than speed/cost
- You need to filter out poor matches (e.g., data validation)
- You're making high-value decisions based on matches
- Search volume is low to moderate
- You need access to confidence scores for logging/analytics
Disable confidence scoring when:
- You need fast response times (real-time user interactions)
- You're processing high volumes (thousands of searches)
- You trust the AI to always pick the best available match
- Cost optimization is a priority
- You're operating in a latency-sensitive environment
Accessing Confidence Data
When confidence scoring is enabled, you can access the score, reasoning, and the matched item that was evaluated after finding a match:
Available Methods
setList(array $list)
Set the list of items to search through.
setSearchedItem(string $key, mixed $value)
Set the item you're searching for. The key is used for context, and the value is what will be matched.
setDescriptionOfList(string $description)
Set a description of what the list items represent. This provides additional context to the AI for more accurate matching.
setAdditionalInstructions(string $instructions)
Add additional instructions to guide the AI's matching behavior.
setCustomSystemMessage(string $message)
Override the default system message entirely.
setAllowNoResult(bool $allowNoResult = true)
Set whether to allow null to be returned if the confidence score is below the threshold. When set to true (default), the find() method will return null if the best match has a confidence score below the threshold set by setNoResultConfidenceThreshold(). When set to false, the closest match will always be returned regardless of confidence.
setNoResultConfidenceThreshold(int $threshold)
Set the minimum confidence threshold (0-100) required for returning a match. Default is 80%. This threshold only applies when setAllowNoResult(true) is set. If the best match has a confidence score below this threshold, null will be returned instead.
setModel(string $model)
Set the OpenAI model to use (e.g., 'gpt-4.1', 'gpt-4.1-mini', 'gpt-5').
find()
Execute the search and return the closest matching item. Returns array|null - the matched item from the list, or null if no sufficiently confident match is found (when allowNoResult is true and confidence is below the threshold).
getConfidenceScore()
Get the confidence score (0-100) of the last match. Returns int|null - the confidence score, or null if no match has been performed yet or if confidence scoring was disabled (setAllowNoResult(false)). This method should be called after find().
getConfidenceReasoning()
Get the reasoning behind the confidence score of the last match. Returns string|null - a text explanation of why the confidence score was assigned, or null if no match has been performed yet or if confidence scoring was disabled. This method should be called after find().
getConfidenceEvaluationMatchedItem()
Get the matched item that was used for the confidence evaluation. Returns array|null - the matched item that was evaluated for confidence scoring, or null if no match has been performed yet or if confidence scoring was disabled (setAllowNoResult(false)). This method should be called after find(). Note that this returns the same item as find() when a match is found, but provides access to it even when examining confidence data.
getModel()
Get the OpenAI model being used. Returns string - the OpenAI model name (e.g., 'gpt-4.1-mini', 'gpt-4o', 'gpt-5').
getApiUrl()
Get the OpenAI API URL. Returns string - the API endpoint URL.
getList()
Get the list of items to search through. Returns array - the array of items that was set via setList().
getDescriptionOfList()
Get the description of the list. Returns string|null - the description of what the list items represent, or null if not set.
getSearchedItemKey()
Get the searched item key. Returns string|null - the key name for the searched item, or null if not set.
getSearchedItemValue()
Get the searched item value. Returns mixed - the value being searched for, or null if not set.
getAdditionalInstructions()
Get the additional instructions. Returns string|null - the additional instructions for the AI, or null if not set.
getSystemMessage()
Get the complete system message that will be sent to the AI. Returns string|null - the constructed system message including custom message, list description, and additional instructions.
getAllowNoResult()
Get whether no result is allowed. Returns bool - whether null results are allowed when confidence is low.
getNoResultConfidenceThreshold()
Get the no result confidence threshold. Returns int - the confidence threshold (0-100) below which null is returned.
Error Handling
The package provides specific exception classes for different error scenarios:
Exception Types
InvalidConfigurationException - Thrown when configuration is invalid:
- Missing OpenAI API key
- Invalid confidence threshold (not between 0-100)
InvalidInputException - Thrown when input data is invalid:
- Empty list
- Missing searched item
InvalidApiResponseException - Thrown when the API returns unexpected data:
- API request errors (authentication, rate limits, etc.)
- Empty or malformed responses
- AI returns item not in the provided list
All exceptions extend the base AiItemFinderException class, which extends PHP's standard Exception.
Usage Examples
You can also catch all package exceptions using the base exception class:
Use Cases
- Matching user input to predefined options
- Finding similar products in a catalog
- Mapping inconsistent data to standardized values
- Intelligent autocomplete and suggestions
- Data normalization and deduplication
- Fuzzy matching with confidence thresholds for quality control
- Semantic search across categorized data with contextual descriptions
License
MIT License
Author
AMI Praha a.s.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
All versions of ai-item-finder with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/http Version ^10.0|^11.0|^12.0
guzzlehttp/guzzle Version ^7.0