1
1
import { Prisma } from '@prisma/client' ;
2
2
import { BadRequestException , Injectable } from '@nestjs/common' ;
3
- import { ConfigService } from '@nestjs/config' ;
4
3
import { IProduct , IProductScore } from './types' ;
5
- import { AppConfig , EnvObjects } from '../config/types' ;
6
4
import { PrismaService } from '../prisma/prisma.service' ;
7
5
import OpenAI from 'openai' ;
8
6
import { OpenapiService } from '../openapi/openapi.service' ;
9
7
10
- function jaccardIndex ( s1 : Set < string > , s2 : Set < string > ) {
11
- return ( s1 as any ) . intersection ( s2 ) . size / ( s1 as any ) . union ( s2 ) . size ;
12
- }
13
8
function formatVector ( vect : number [ ] ) {
14
9
return `[${ vect . map ( ( nn ) => nn . toString ( 10 ) ) . join ( ',' ) } ]` ;
15
10
}
@@ -34,8 +29,8 @@ export class ProductService {
34
29
throw new BadRequestException ( productId , 'Product not found' ) ;
35
30
}
36
31
const scores = await this . prismaService . client . $queryRaw <
37
- ( IProductScore & { tagScore : number } ) [ ]
38
- > `
32
+ ( IProductScore & { tagScore : number } ) [ ]
33
+ > `
39
34
SELECT
40
35
array_length(allTags.items, 1) AS "unionSize",
41
36
interTags.sizee AS "interSize",
74
69
WHERE
75
70
pp2."id" != pp1."id" AND pp1."id" = ${ productId } ;
76
71
` ;
77
- return scores . map (
78
- ( { score, tagScore, productId} ) => ( {
72
+ return scores
73
+ . map ( ( { score, tagScore, productId } ) => ( {
79
74
score : TAGS_WEIGH * tagScore + DESCRIPTION_WEIGH * score ,
80
75
productId,
81
- } ) ,
82
- ) . sort ( ( { score : score1 } , { score : score2 } ) => score2 - score1 ) ;
76
+ } ) )
77
+ . sort ( ( { score : score1 } , { score : score2 } ) => score2 - score1 ) ;
83
78
}
84
79
85
80
async bulkCreate ( items : IProduct [ ] ) {
86
81
const wEmbeddingsItems = await Promise . all (
87
82
items . map ( async ( item ) => {
88
83
try {
89
- const wordEmbedding = await this . openapiService . wordEmbedding ( item . description ) ;
84
+ const wordEmbedding = await this . openapiService . wordEmbedding (
85
+ item . description ,
86
+ ) ;
90
87
return {
91
88
...item ,
92
89
wordEmbedding,
@@ -113,28 +110,31 @@ WHERE
113
110
// );
114
111
// this.prismaService.client.$executeRaw`INSERT INTO items (id, name, description, tags, wordEmbedding) VALUES ${sqlValues};`;
115
112
await this . prismaService . client . $transaction (
116
- ( [ ] as Prisma . PrismaPromise < Prisma . BatchPayload | number > [ ] ) . concat ( ...wEmbeddingsItems . map (
117
- // (wEmbeddingsItem) => this.prismaService.client.$executeRaw
118
- // `INSERT INTO "Product" ("id", "name", "description", "tags", "wordEmbedding") VALUES (${wEmbeddingsItem.id},${wEmbeddingsItem.name},${wEmbeddingsItem.description},${wEmbeddingsItem.tags},${formatVector(wEmbeddingsItem.wordEmbedding)});`
119
- // ,
120
- ( wEmbeddingsItem ) => [
121
- this . prismaService . client . $executeRawUnsafe (
122
- `INSERT INTO "Product" ("id", "name", "description", "tags", "wordEmbedding") VALUES ($1,$2,$3,$4,'${ formatVector ( wEmbeddingsItem . wordEmbedding ) } '::vector);` ,
123
- ...[
124
- wEmbeddingsItem . id ,
125
- wEmbeddingsItem . name ,
126
- wEmbeddingsItem . description ,
127
- wEmbeddingsItem . tags ,
128
- ] ,
129
- ) ,
130
- // ToDo: Store cache of relations between all of them? Symmetric relation
131
- this . prismaService . client . tagOfProduct . createMany ( {
132
- data : wEmbeddingsItem . tags . map (
133
- ( tag ) => ( { tagValue : tag , productId : wEmbeddingsItem . id } ) ,
113
+ ( [ ] as Prisma . PrismaPromise < Prisma . BatchPayload | number > [ ] ) . concat (
114
+ ...wEmbeddingsItems . map (
115
+ // (wEmbeddingsItem) => this.prismaService.client.$executeRaw
116
+ // `INSERT INTO "Product" ("id", "name", "description", "tags", "wordEmbedding") VALUES (${wEmbeddingsItem.id},${wEmbeddingsItem.name},${wEmbeddingsItem.description},${wEmbeddingsItem.tags},${formatVector(wEmbeddingsItem.wordEmbedding)});`
117
+ // ,
118
+ ( wEmbeddingsItem ) => [
119
+ this . prismaService . client . $executeRawUnsafe (
120
+ `INSERT INTO "Product" ("id", "name", "description", "tags", "wordEmbedding") VALUES ($1,$2,$3,$4,'${ formatVector ( wEmbeddingsItem . wordEmbedding ) } '::vector);` ,
121
+ ...[
122
+ wEmbeddingsItem . id ,
123
+ wEmbeddingsItem . name ,
124
+ wEmbeddingsItem . description ,
125
+ wEmbeddingsItem . tags ,
126
+ ] ,
134
127
) ,
135
- } ) ,
136
- ] ,
137
- ) ) ,
128
+ // ToDo: Store cache of relations between all of them? Symmetric relation
129
+ this . prismaService . client . tagOfProduct . createMany ( {
130
+ data : wEmbeddingsItem . tags . map ( ( tag ) => ( {
131
+ tagValue : tag ,
132
+ productId : wEmbeddingsItem . id ,
133
+ } ) ) ,
134
+ } ) ,
135
+ ] ,
136
+ ) ,
137
+ ) ,
138
138
) ;
139
139
}
140
140
}
0 commit comments