Skip to content

Commit

Permalink
add the idea of embedding drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 29, 2024
1 parent 39b4486 commit a8ff8c2
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/Domains/Messages/SearchOrSummarizeChatRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function search(Chat $chat, string $input): string

/** @var EmbeddingsResponseDto $embedding */
$embedding = LlmDriverFacade::driver(
$chat->chatable->getDriver()
$chat->chatable->getEmddingDriver()
)->embedData($input);

$results = DocumentChunk::query()
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function store()
'name' => 'required',
'description' => 'required',
'driver' => 'required',
'embedding_driver' => 'required',
]);

$validated['team_id'] = auth()->user()->current_team_id;
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/VectorlizeDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle(): void
$content = $this->documentChunk->content;

/** @var EmbeddingsResponseDto $results */
$results = LlmDriverFacade::driver($this->documentChunk->getDriver())
$results = LlmDriverFacade::driver($this->documentChunk->getEmbeddingDriver())
->embedData($content);

$this->documentChunk->update([
Expand Down
39 changes: 30 additions & 9 deletions app/LlmDriver/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public function embedData(string $data): EmbeddingsResponseDto

Log::info('LlmDriver::ClaudeClient::embedData');



return EmbeddingsResponseDto::from([
'embedding' => data_get($data, 'data.0.embedding'),
'token_count' => 1000,
Expand All @@ -36,17 +34,40 @@ public function embedData(string $data): EmbeddingsResponseDto
*/
public function chat(array $messages): CompletionResponse
{
if (! app()->environment('testing')) {
sleep(2);
$model = $this->getConfig('claude')['models']['completion_model'];
$maxTokens = $this->getConfig('claude')['max_tokens'];

Log::info('LlmDriver::Claude::completion');

$messages = collect($messages)->map(function($item) {
if($item->role === 'system') {
$item->role = 'assistant';
}

return $item->toArray();
})->reverse()->values()->all();

$results = $this->getClient()->post('/messages', [
'model' => $model,
"max_tokens" => $maxTokens,
'messages' => $messages,
]);

if(!$results->ok()) {
$error = $results->json()['error']['type'];
Log::error('Claude API Error ' . $error);
throw new \Exception('Claude API Error ' . $error);
}

Log::info('LlmDriver::MockClient::completion');
$data = null;

$data = <<<'EOD'
Voluptate irure cillum dolor anim officia reprehenderit dolor. Eiusmod veniam nostrud consectetur incididunt proident id. Anim adipisicing pariatur amet duis Lorem sunt veniam veniam est. Deserunt ea aliquip cillum pariatur consectetur. Dolor in reprehenderit adipisicing consectetur cupidatat ad cupidatat reprehenderit. Nostrud mollit voluptate aliqua anim pariatur excepteur eiusmod velit quis exercitation tempor quis excepteur.
EOD;
foreach($results->json()['content'] as $content) {
$data = $content['text'];
}

return new CompletionResponse($data);
return CompletionResponse::from([
'content' => $data,
]);
}

public function completion(string $prompt): CompletionResponse
Expand Down
13 changes: 13 additions & 0 deletions app/LlmDriver/DriversEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\LlmDriver;

enum DriversEnum : string {

case Mock = 'mock';
case OpenAi = 'openai';
case OpenAiAzure = 'openai_azure';
case Ollama = 'ollama';
case Gemini = 'gemini';
case Claude = 'claude';
}
10 changes: 10 additions & 0 deletions app/LlmDriver/HasDrivers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\LlmDriver;


interface HasDrivers
{
public function getDriver(): string;
public function getEmbeddingDriver(): string;
}
18 changes: 15 additions & 3 deletions app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,48 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use App\LlmDriver\DriversEnum;
use App\LlmDriver\HasDrivers;

/**
* Class Project
*
* @property int $id
* @property string $name
* @property string|null $description
* @property DriversEnum $driver
* @property DriversEnum $embedding_driver
* @property bool $active
* @property int $team_id
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
*/
class Collection extends Model
class Collection extends Model implements HasDrivers
{
use HasFactory;

protected $guarded = [];

protected $cast = [
protected $casts = [
'active' => 'boolean',
'driver' => DriversEnum::class,
'embedding_driver' => DriversEnum::class
];


public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}

public function getDriver(): string
{
return $this->driver;
return $this->driver->value;
}

public function getEmbeddingDriver(): string
{
return $this->embedding_driver->value;
}

public function documents(): HasMany
Expand Down
11 changes: 9 additions & 2 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use App\LlmDriver\HasDrivers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -17,7 +18,7 @@
* @property string|null $summary
* @property string|null $file_path
*/
class Document extends Model
class Document extends Model implements HasDrivers
{
use HasFactory;

Expand All @@ -29,6 +30,7 @@ class Document extends Model
'summary_status' => StatusEnum::class,
];


public function collection(): BelongsTo
{
return $this->belongsTo(Collection::class);
Expand Down Expand Up @@ -58,6 +60,11 @@ public function mkdirPathToFile(): ?string

public function getDriver(): string
{
return $this->collection->driver;
return $this->collection->driver->value;
}


public function getEmbeddingDriver(): string {
return $this->collection->embedding_driver->value;
}
}
10 changes: 8 additions & 2 deletions app/Models/DocumentChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace App\Models;

use App\Domains\Documents\StatusEnum;
use App\LlmDriver\HasDrivers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Pgvector\Laravel\Vector;

/**
* @property Document $document
*/
class DocumentChunk extends Model
class DocumentChunk extends Model implements HasDrivers
{
use HasFactory;

Expand Down Expand Up @@ -41,8 +42,13 @@ protected static function booted()
});
}

public function getEmbeddingDriver(): string
{
return $this->document->collection->embedding_driver->value;
}

public function getDriver(): string
{
return $this->document->collection->driver;
return $this->document->collection->driver->value;
}
}
3 changes: 2 additions & 1 deletion config/llmdriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
],
'claude' => [
'api_key' => env('CLAUDE_API_KEY'),
'max_tokens' => env('CLAUDE_MAX_TOKENS', 1024),
'max_tokens' => env('CLAUDE_MAX_TOKENS', 4000),
'models' => [
//@see https://www.anthropic.com/news/claude-3-family
'completion_model' => env('CLAUDE_COMPLETION_MODEL', 'claude-3-opus-20240229'),
]
],
Expand Down
3 changes: 3 additions & 0 deletions database/factories/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\LlmDriver\DriversEnum;
use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -22,6 +23,8 @@ public function definition(): array
'description' => $this->faker->paragraph,
'active' => $this->faker->boolean,
'team_id' => Team::factory(),
'driver' => DriversEnum::Mock,
'embedding_driver' => DriversEnum::Mock,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use App\LlmDriver\DriversEnum;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('collections', function (Blueprint $table) {
$table->string('embedding_driver')->default(DriversEnum::Mock);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('collections', function (Blueprint $table) {
//
});
}
};
66 changes: 66 additions & 0 deletions resources/js/Pages/Collection/Components/EmbeddingType.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<template>
<Listbox as="div" v-model="selected">
<ListboxLabel class="sr-only">Choose Embedding LLM Drier</ListboxLabel>
<div class="relative">
<div class="inline-flex divide-x divide-indigo-700 rounded-md shadow-sm">
<div class="inline-flex items-center gap-x-1.5 rounded-l-md bg-indigo-600 px-3 py-2 text-white shadow-sm">
<CheckIcon class="-ml-0.5 h-5 w-5" aria-hidden="true" />
<p class="text-sm font-semibold">{{ selected.title }}</p>
</div>
<ListboxButton class="inline-flex items-center rounded-l-none rounded-r-md bg-indigo-600 p-2 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2 focus:ring-offset-gray-50">
<ChevronDownIcon class="h-5 w-5 text-white" aria-hidden="true" />
</ListboxButton>
</div>

<transition leave-active-class="transition ease-in duration-100" leave-from-class="opacity-100" leave-to-class="opacity-0">
<ListboxOptions

class="absolute left-0 z-10 mt-2 w-72 origin-top-right divide-y divide-gray-200 overflow-hidden rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
<ListboxOption
:disabled="!option.active"
as="template" v-for="option in publishingOptions" :key="option.title" :value="option" v-slot="{ active, selected }">
<li :class="[active ? 'bg-indigo-600 text-white' : 'text-gray-900', 'cursor-default select-none p-4 text-sm']">
<div class="flex flex-col">
<div class="flex justify-between">
<p :class="selected ? 'font-semibold' : 'font-normal'">{{ option.title }}
<span class="text-gray-600" v-if="!option.active">
(coming soon..)</span>
</p>
<span v-if="selected" :class="active ? 'text-white' : 'text-indigo-600'">
<CheckIcon class="h-5 w-5" aria-hidden="true" />
</span>
</div>
<p :class="[active ? 'text-indigo-200' : 'text-gray-500', 'mt-2']">{{ option.description }}</p>
</div>
</li>
</ListboxOption>
</ListboxOptions>
</transition>
</div>
</Listbox>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Listbox, ListboxButton, ListboxLabel, ListboxOption, ListboxOptions } from '@headlessui/vue'
import { CheckIcon, ChevronDownIcon } from '@heroicons/vue/20/solid'
const emit = defineEmits(['embeddingTypeChosen'])
const publishingOptions = [
{ active: true, key: "mock", title: 'Mock LLM', description: 'This will mock all the LLM features great for local development', current: true },
{ active: true, key: "openai", title: 'OpenAi', description: 'This will work with the OpenAi Api', current: false },
{ active: false, key: "mock", title: 'OpenAi Azure', description: 'This will work with the Azure OpenAi Api', current: false },
{ active: false, key: "ollama", title: 'Ollama', description: 'This will work with the Ollam API', current: false },
{ active: false, key: "mock", title: 'Gemini', description: 'This will work with the Gemini Api', current: false },
{ active: true, key: "mock", title: 'Claude using Vonage', description: 'This will work with the Claude Api', current: false },
]
const selected = ref(publishingOptions[0])
watch(selected, (value) => {
console.log("emit " , value)
emit('embeddingTypeChosen', value.key)
})
</script>
Loading

0 comments on commit a8ff8c2

Please sign in to comment.