From 66abf5cd738834a28413e27cb0c2ccf5ef108d20 Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Sat, 27 Apr 2024 13:13:39 -0400 Subject: [PATCH] adding chat references --- README.md | 8 +++++ .../WebSearch/Drivers/BaseSearchClient.php | 1 - app/Models/Chat.php | 5 +++ app/Models/ChatDocumentReference.php | 29 ++++++++++++++++ config/llmdriver.php | 1 + .../ChatDocumentReferenceFactory.php | 29 ++++++++++++++++ ..._create_chat_document_references_table.php | 34 +++++++++++++++++++ .../Models/ChatDocumentReferenceTest.php | 23 +++++++++++++ 8 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 app/Models/ChatDocumentReference.php create mode 100644 database/factories/ChatDocumentReferenceFactory.php create mode 100644 database/migrations/2024_04_27_170436_create_chat_document_references_table.php create mode 100644 tests/Feature/Models/ChatDocumentReferenceTest.php diff --git a/README.md b/README.md index 20612821..26466757 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,14 @@ memory_limit=-1 post_max_size = 100M ``` +## Which Model???? + +Keep in mind the size of the model is not the only thing that matters. If you read over [https://ollama.com/library](https://ollama.com/library) +you will see some are better at chat, some at large context, some at code etc. LaraLamma can use multiple models for different jobs. + +Looking in the `config/llmdriver.php` you can see the options in there. + + ## Road Map (still in motion) diff --git a/app/Domains/Sources/WebSearch/Drivers/BaseSearchClient.php b/app/Domains/Sources/WebSearch/Drivers/BaseSearchClient.php index a3e9b555..08b4b9c3 100644 --- a/app/Domains/Sources/WebSearch/Drivers/BaseSearchClient.php +++ b/app/Domains/Sources/WebSearch/Drivers/BaseSearchClient.php @@ -3,7 +3,6 @@ namespace App\Domains\Sources\WebSearch\Drivers; use App\Domains\Sources\WebSearch\Response\SearchResponseDto; -use PhpOffice\PhpSpreadsheet\Calculation\TextData\Search; abstract class BaseSearchClient { diff --git a/app/Models/Chat.php b/app/Models/Chat.php index d39ace87..103e8be1 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -183,4 +183,9 @@ public function user(): BelongsTo { return $this->belongsTo(User::class); } + + public function chat_document_references(): HasMany + { + return $this->hasMany(ChatDocumentReference::class); + } } diff --git a/app/Models/ChatDocumentReference.php b/app/Models/ChatDocumentReference.php new file mode 100644 index 00000000..397d50ea --- /dev/null +++ b/app/Models/ChatDocumentReference.php @@ -0,0 +1,29 @@ +belongsTo(Chat::class); + } + + public function document(): BelongsTo + { + return $this->belongsTo(Document::class); + } + + public function document_chunk(): BelongsTo + { + return $this->belongsTo(DocumentChunk::class); + } +} diff --git a/config/llmdriver.php b/config/llmdriver.php index 10aa6c64..a2b17990 100644 --- a/config/llmdriver.php +++ b/config/llmdriver.php @@ -49,6 +49,7 @@ //@see https://github.com/ollama/ollama/blob/main/docs/openai.md 'completion_model' => env('OLLAMA_COMPLETION_MODEL', 'llama3'), 'embedding_model' => env('OLLAMA_EMBEDDING_MODEL', 'mxbai-embed-large'), + 'chat_output_model' => env('OLLAMA_COMPLETION_MODEL', 'llama3'), //this is good to use other systems for better repsonses to people in chat ], ], ], diff --git a/database/factories/ChatDocumentReferenceFactory.php b/database/factories/ChatDocumentReferenceFactory.php new file mode 100644 index 00000000..3afb5b4a --- /dev/null +++ b/database/factories/ChatDocumentReferenceFactory.php @@ -0,0 +1,29 @@ + + */ +class ChatDocumentReferenceFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'chat_id' => Chat::factory(), + 'document_id' => Document::factory(), + 'document_chunk_id' => DocumentChunk::factory(), + 'reference' => fake()->text(), + ]; + } +} diff --git a/database/migrations/2024_04_27_170436_create_chat_document_references_table.php b/database/migrations/2024_04_27_170436_create_chat_document_references_table.php new file mode 100644 index 00000000..430e3e72 --- /dev/null +++ b/database/migrations/2024_04_27_170436_create_chat_document_references_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignIdFor(Chat::class); + $table->foreignIdFor(Document::class); + $table->foreignIdFor(DocumentChunk::class); + $table->string('reference'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('chat_document_references'); + } +}; diff --git a/tests/Feature/Models/ChatDocumentReferenceTest.php b/tests/Feature/Models/ChatDocumentReferenceTest.php new file mode 100644 index 00000000..9adfc6ce --- /dev/null +++ b/tests/Feature/Models/ChatDocumentReferenceTest.php @@ -0,0 +1,23 @@ +create(); + + $this->assertIsString($model->reference); + $this->assertNotNull($model->chat->id); + $this->assertNotNull($model->document->id); + $this->assertNotNull($model->document_chunk->id); + $this->assertNotNull($model->chat->chat_document_references()->first()->id); + } +}