1
+ import { DataType } from '@zilliz/milvus2-sdk-node/dist/milvus' ;
2
+ import type { ChatCompletionRequestMessage } from 'openai' ;
3
+
1
4
import OpenAIClient from './clients/OpenAIClient' ;
2
- import ChromaClient from './clients/ChromaClient ' ;
5
+ import MilvusClient from './clients/MilvusClient ' ;
3
6
4
- import type { ChatCompletionRequestMessage } from 'openai' ;
5
7
6
8
const RELATIVE_TEXT_COUNT = 3 ;
7
9
@@ -13,30 +15,15 @@ const PRE_PROMPT = `You are a helpful AI assistant. Use the following pieces of
13
15
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
14
16
Try to keep your answers helpful, short and to the point using markdown formatting.` ;
15
17
16
- interface QueryResponse {
17
- ids : string [ ] [ ] ;
18
- documents : string [ ] [ ] ;
19
- metadatas : {
20
- userID : string ;
21
- context : string [ ] ;
22
- } [ ] [ ] ;
23
- }
24
-
25
- interface GetQueryResponse {
26
- ids : string [ ] ;
27
- documents : string [ ] ;
28
- metadatas : {
29
- userID : string ;
30
- context : string [ ] ;
31
- } [ ] ;
32
- }
33
-
34
18
const getChatResponse = async (
35
19
messages : ChatCompletionRequestMessage [ ] ,
36
20
question : string ,
37
- user : string
21
+ page : string
38
22
) : Promise < string > => {
39
- const blockCollection = await ChromaClient . getCollection ( 'blocks' ) ;
23
+ // ~ Load the block collection
24
+ await MilvusClient . loadCollection ( {
25
+ collection_name : 'blocks' ,
26
+ } ) ;
40
27
41
28
// ~ Create an embedding for the latest message
42
29
const embeddings = await OpenAIClient . createEmbedding ( {
@@ -45,45 +32,54 @@ const getChatResponse = async (
45
32
} ) ;
46
33
47
34
try {
48
- // ~ Query the database for the most similar message
49
- const similarMessages = await blockCollection . query (
50
- embeddings . data . data [ 0 ] . embedding ,
51
- RELATIVE_TEXT_COUNT ,
52
- {
53
- userID : user ,
35
+ const similarMessages = await MilvusClient . search ( {
36
+ collection_name : 'blocks' ,
37
+ limit : RELATIVE_TEXT_COUNT ,
38
+ vector_type : DataType . FloatVector ,
39
+ params : {
40
+ anns_field : 'block_id' ,
41
+ topk : `${ RELATIVE_TEXT_COUNT } ` ,
42
+ metric_type : "L2" ,
43
+ params : JSON . stringify ( { nprobe : 10 } ) ,
54
44
} ,
55
- ) as QueryResponse ;
45
+ vector : embeddings . data . data [ 0 ] . embedding ,
46
+ expr : `page_id == ${ page } ` ,
47
+ output_fields : [ 'content' , 'context' ]
48
+ } )
56
49
57
50
// ~ If there are no similar messages, return a default message
58
- if ( ! similarMessages . documents ) {
51
+ if ( ! similarMessages . results ) {
59
52
return 'I don\'t know what to say.' ;
60
53
}
61
54
55
+ console . log ( JSON . stringify (
56
+ similarMessages
57
+ ) )
58
+
62
59
// ~ Get the context messages
63
60
const contextIDs = Array . from (
64
61
new Set (
65
- similarMessages . metadatas
62
+ similarMessages . results
66
63
. flat ( )
67
- . flatMap ( ( metadata ) => metadata . context )
64
+ . flatMap ( ( metadata ) => JSON . parse ( metadata . context ) as string [ ] )
68
65
)
69
- )
66
+ ) ;
70
67
71
- const contextMessages = await blockCollection . get (
72
- contextIDs ,
73
- {
74
- userID : user ,
75
- }
76
- ) as GetQueryResponse ;
68
+ const contextMessages = await MilvusClient . query ( {
69
+ collection_name : 'blocks' ,
70
+ expr : `block_id in [${ contextIDs . join ( ', ' ) } ]` ,
71
+ output_fields : [ 'content' , 'block_id' ] ,
72
+ } )
77
73
78
74
const contextMessagesMap : Record < string , string > = { } ;
79
75
80
- contextMessages . documents . forEach ( ( document , index ) => {
81
- contextMessagesMap [ contextMessages . ids [ index ] ] = document ;
76
+ contextMessages . data . forEach ( ( result ) => {
77
+ contextMessagesMap [ result . block_id ] = result . content ;
82
78
} ) ;
83
79
84
- const context = similarMessages . metadatas
85
- . flat ( 2 )
86
- . flatMap ( ( metadata ) => metadata . context )
80
+ const context = similarMessages . results
81
+ . map ( ( result ) => result . context )
82
+ . flat ( )
87
83
. map ( ( id ) => contextMessagesMap [ id ] )
88
84
. filter ( ( message ) => message )
89
85
. map ( ( message ) => message . trim ( ) )
0 commit comments