4
4
5
5
namespace Codewithkyrian \ChromaDB \Concerns ;
6
6
7
+ use Codewithkyrian \ChromaDB \Contracts \ChromaModel ;
7
8
use Codewithkyrian \ChromaDB \Embeddings \EmbeddingFunction ;
8
9
use Codewithkyrian \ChromaDB \Facades \ChromaDB ;
9
10
use Codewithkyrian \ChromaDB \Jobs \UpdateChromaCollectionJob ;
10
11
use Codewithkyrian \ChromaDB \Resources \CollectionResource ;
12
+ use Illuminate \Database \Eloquent \Model ;
11
13
use Illuminate \Support \Facades \DB ;
12
14
use Illuminate \Database \Eloquent \Builder ;
13
15
14
- /**
15
- * @method mixed getKey()
16
- * @method string getTable()
17
- * @method mixed getAttribute(string $field)
18
- * @method array getChanges()
19
- * @method self saved(\Illuminate\Events\QueuedClosure|\Closure|string|array $callback)
20
- * @method self deleted(\Illuminate\Events\QueuedClosure|\Closure|string|array $callback)
21
- */
22
16
trait HasChromaCollection
23
17
{
24
18
private static ?CollectionResource $ chromaCollection ;
25
19
26
20
protected static function bootHasChromaCollection (): void
27
21
{
28
- $ model = new static ();
29
-
30
- static ::$ chromaCollection = ChromaDB::getOrCreateCollection (
31
- name: $ model ->collectionName (),
32
- embeddingFunction: $ model ->embeddingFunction (),
33
- );
34
-
35
22
if (config ('chromadb.sync.enabled ' )) {
36
- static ::saved (function (self $ model ) {
23
+ static ::saved (function (Model & ChromaModel $ model ) {
37
24
$ changedFields = array_keys ($ model ->getChanges ());
38
25
39
26
if (!config ('chromadb.sync.queue ' , false )) {
@@ -43,14 +30,22 @@ protected static function bootHasChromaCollection(): void
43
30
}
44
31
});
45
32
46
- static ::deleted (function (self $ model ) {
33
+ static ::deleted (function (Model & ChromaModel $ model ) {
47
34
self ::getChromaCollection ()->delete ([$ model ->getKey ()]);
48
35
});
49
36
}
50
37
}
51
38
52
39
public static function getChromaCollection (): CollectionResource
53
40
{
41
+ $ model = new static ();
42
+
43
+ static ::$ chromaCollection ??= ChromaDB::getOrCreateCollection (
44
+ name: $ model ->collectionName (),
45
+ embeddingFunction: $ model ->embeddingFunction (),
46
+ );
47
+
48
+
54
49
return static ::$ chromaCollection ;
55
50
}
56
51
@@ -83,7 +78,7 @@ public function scopeQueryChromaCollection(Builder $query, string $queryText, in
83
78
*
84
79
* @return string[]
85
80
*/
86
- protected function metadataFields (): array
81
+ public function metadataFields (): array
87
82
{
88
83
return ['id ' ];
89
84
}
@@ -93,29 +88,29 @@ protected function metadataFields(): array
93
88
*
94
89
* @return string[]
95
90
*/
96
- protected function documentFields (): array
91
+ public function documentFields (): array
97
92
{
98
93
return [];
99
94
}
100
95
101
96
/**
102
97
* The embedding function to use for the collection.
103
98
*/
104
- protected function embeddingFunction (): ?EmbeddingFunction
99
+ public function embeddingFunction (): ?EmbeddingFunction
105
100
{
106
101
return null ;
107
102
}
108
103
109
104
/**
110
105
* The collection name to use for the model.
111
106
*/
112
- protected function collectionName (): string
107
+ public function collectionName (): string
113
108
{
114
109
return $ this ->getTable ();
115
110
}
116
111
117
112
118
- private function generateMetadata (): array
113
+ public function generateMetadata (): array
119
114
{
120
115
121
116
return collect ($ this ->metadataFields ())
@@ -125,7 +120,7 @@ private function generateMetadata(): array
125
120
->toArray ();
126
121
}
127
122
128
- protected function generateChromaDocument (): string
123
+ public function generateChromaDocument (): string
129
124
{
130
125
return collect ($ this ->documentFields ())
131
126
->map (function (string $ field ) {
0 commit comments