Skip to content

Commit

Permalink
embedding added
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 30, 2024
1 parent e7dab91 commit 6c9a762
Show file tree
Hide file tree
Showing 7 changed files with 4,127 additions and 6 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function update(Collection $collection)
return to_route('collections.show', $collection);
}


public function show(Collection $collection)
{
$chatResource = $collection->chats()->where('user_id', auth()->user()->id)
Expand Down
18 changes: 18 additions & 0 deletions app/LlmDriver/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\LlmDriver\Requests\MessageInDto;
use App\LlmDriver\Responses\CompletionResponse;
use App\LlmDriver\Responses\EmbeddingsResponseDto;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
Expand All @@ -12,6 +13,23 @@ class OllamaClient extends BaseClient
{
protected string $driver = 'ollama';

public function embedData(string $prompt): EmbeddingsResponseDto
{
Log::info('LlmDriver::Ollama::completion');

$response = $this->getClient()->post('/embeddings', [
'model' => $this->getConfig('ollama')['models']['embedding_model'],
'prompt' => $prompt,
]);

$results = $response->json();

return EmbeddingsResponseDto::from([
'embedding' => data_get($results, 'embedding'),
'token_count' => 1000,
]);
}

/**
* @param MessageInDto[] $messages
*
Expand Down
1 change: 1 addition & 0 deletions config/llmdriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'models' => [
//@see https://github.com/ollama/ollama/blob/main/docs/openai.md
'completion_model' => env('CLAUDE_COMPLETION_MODEL', 'llama2'),
'embedding_model' => env('CLAUDE_EMBEDDING_MODEL', 'llama2'),
],
],
],
Expand Down
1 change: 0 additions & 1 deletion resources/js/Pages/Collection/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const closeEditCollectionSlideOut = () => {
showEditCollection.value = false;
};
onMounted(() => {
Echo.private(`collection.${props.collection.data.id}`)
.listen('.status', (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function test_update(): void
'description' => 'Test Description',
])->assertStatus(302);
$this->assertDatabaseCount('collections', 1);

$this->assertEquals(DriversEnum::Claude, $collection->refresh()->embedding_driver);

}
Expand Down
10 changes: 7 additions & 3 deletions tests/Feature/OllamaClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Feature;

use App\LlmDriver\ClaudeClient;
use App\LlmDriver\OllamaClient;
use App\LlmDriver\Requests\MessageInDto;
use App\LlmDriver\Responses\CompletionResponse;
Expand All @@ -17,9 +16,14 @@ class OllamaClientTest extends TestCase
*/
public function test_embeddings(): void
{
$this->markTestSkipped('@TODO: Requires another server');

$client = new ClaudeClient();
$client = new OllamaClient();

$data = get_fixture('ollama_embedings.json');

Http::fake([
'127.0.0.1:11434/*' => Http::response($data, 200),
]);

$results = $client->embedData('test');

Expand Down
Loading

0 comments on commit 6c9a762

Please sign in to comment.