A Laravel package for interacting with Wit.ai's API, providing easy-to-use services for message parsing and entity/intent management.
- Install the package via Composer:
composer require nigel/wit_parser
- Publish the configuration file:
php artisan vendor:publish --provider="Nigel\WitParser\WitParserServiceProvider" --tag="config"
- Add your Wit.ai credentials to your
.env
file:
WIT_AI_TOKEN=your_wit_ai_token_here
WIT_AI_BASE_URL=https://api.wit.ai/
use Nigel\WitParser\WitParserService;
class YourController extends Controller
{
public function parseMessage()
{
$parser = new WitParserService();
$result = $parser->parse("What's the weather in New York?");
// Access parsed data
$intent = $result->intent; // e.g., "get_weather"
$confidence = $result->confidence; // e.g., 0.95
$entities = $result->entities; // Array of entities
$raw = $result->raw; // Raw API response
}
}
use Nigel\WitParser\WitManagerService;
class YourController extends Controller
{
public function manageWit()
{
$manager = new WitManagerService();
// Create a new entity
$entity = $manager->createEntity('location', [
['value' => 'New York'],
['value' => 'London']
]);
// Get all entities
$entities = $manager->getEntities();
// Create a new intent
$intent = $manager->createIntent('get_weather', [
['text' => 'What\'s the weather in New York?'],
['text' => 'How\'s the weather in London?']
]);
// Get all intents
$intents = $manager->getIntents();
// Get app info
$appInfo = $manager->getAppInfo();
}
}
parse(string $message): WitResult
- Parse a message and get structured data
createEntity(string $name, array $values = []): array
- Create a new entitygetEntities(): array
- Get all entitiescreateIntent(string $name, array $examples = []): array
- Create a new intentgetIntents(): array
- Get all intentsgetAppInfo(): array
- Get app information
The parse()
method returns a WitResult
object with the following properties:
intent
: The detected intentconfidence
: Confidence score (0-1)entities
: Array of detected entitiesraw
: Raw API response
The package throws RuntimeException
with descriptive messages for:
- API request failures
- Invalid responses
- Network errors
- Authentication issues
This package is open-sourced software licensed under the MIT license.