Skip to main content

Bring your own model

On myhomet.com an assistant called Homie answers customers, offers times and books visits for the pro. That assistant is not in the open edition. Out of the box, every conversation stays with the pro.

You can plug in a model of your own. The app talks to its assistant only through interfaces in app/Contracts. The open edition binds them to simple classes in packages/hometown/intelligence-stub, and you replace the ones you want.

The interfaces​

InterfaceWhat it does
App\Contracts\Ai\ConversationAssistantContractAnswers a customer's message in a conversation (texts and the portal chat)
App\Contracts\Ai\AiTaskContractOne-off tasks by name: a greeting, a project plan, tags for a service, reading a receipt or a punch-list photo, and more (AiTaskContract::*)
App\Contracts\Services\LlmClientContractA plain chat call to a model, used by older parts of the app

Each method's comment in app/Contracts says what goes in and what must come back.

A conversation assistant​

handle() gets the conversation and the message being answered. Save the reply on the conversation as an assistant message: the app then texts it to the customer (for a text conversation) and tells the pro. Return the same text as next_question, which the portal chat shows.

<?php

declare(strict_types=1);

namespace App\Ai;

use App\Contracts\Ai\ConversationAssistantContract;
use App\Models\ChatThread;
use Illuminate\Support\Facades\Http;

final class MyAssistant implements ConversationAssistantContract
{
public function handle(ChatThread $thread, array $input, string $language = 'en'): array
{
$history = $thread->messages()->orderBy('id')->get(['role', 'content'])
->map(fn ($m): array => ['role' => $m->role === 'assistant' ? 'assistant' : 'user', 'content' => (string) $m->content])
->all();

$text = (string) Http::withToken((string) config('services.my_model.key'))
->post('https://your-model.example/v1/chat/completions', [
'model' => 'your-model',
'messages' => [
['role' => 'system', 'content' => 'You answer customers of a home-service pro. Never promise a time the pro has not offered.'],
...$history,
],
])
->json('choices.0.message.content', '');

if ($text !== '') {
$thread->messages()->create(['role' => 'assistant', 'content' => $text]);
}

return ['action' => 'ask', 'next_question' => $text];
}

public function guestThreadReadyToBook(ChatThread $thread): void {}

public function rememberLater(ChatThread $thread): void {}
}

Wiring it in​

Bind it in a service provider listed in bootstrap/providers.php. Those load after the stub's, so your binding wins:

public function register(): void
{
$this->app->bind(\App\Contracts\Ai\ConversationAssistantContract::class, \App\Ai\MyAssistant::class);
}

Keep the model's key in .env, never in the code.

Before you turn it on​

  • A model that books, cancels or quotes for a pro speaks for that pro. Start with answers only and let the pro confirm times and prices.
  • The pro can take a conversation over by hand at any time; leave the assistant out of a conversation the pro is running.
  • Texts to customers follow the carrier rules for business texting. Keep the replies short and never send links the customer did not ask for.