@@ -27,7 +27,7 @@ export type ConversationalRetrievalQAChainInput = {
27
27
async function setupVectorstore ( selectedModel ) {
28
28
console . log ( 'Setting up vectorstore' , selectedModel ) ;
29
29
const embeddings = new HuggingFaceTransformersEmbeddings ( {
30
- modelName : 'Supabase/gte- small' ,
30
+ modelName : 'Xenova/jina-embeddings-v2- small-en ' ,
31
31
} ) ;
32
32
const voyClient = new VoyClient ( ) ;
33
33
return new VoyVectorStore ( voyClient , embeddings ) ;
@@ -49,6 +49,16 @@ export async function embedDocs(selectedModel, localFile): Promise<EmbedDocsOutp
49
49
documents . push (
50
50
new Document ( {
51
51
pageContent : pageContent . textContent ,
52
+ metadata : {
53
+ pageURL : pageContent . pageURL ,
54
+ title : pageContent . title ,
55
+ length : pageContent . length ,
56
+ excerpt : pageContent . excerpt ,
57
+ byline : pageContent . byline ,
58
+ dir : pageContent . dir ,
59
+ siteName : pageContent . siteName ,
60
+ lang : pageContent . lang ,
61
+ } ,
52
62
} ) ,
53
63
) ;
54
64
} else {
@@ -77,8 +87,6 @@ export async function* talkToDocument(selectedModel, vectorStore, input: Convers
77
87
console . log ( 'chat_history' , input . chat_history ) ;
78
88
console . log ( 'vectorStore' , vectorStore ) ;
79
89
const retriever = vectorStore . asRetriever ( ) ;
80
- const context = retriever . pipe ( formatDocumentsAsString ) ;
81
- console . log ( 'context' , context ) ;
82
90
const condenseQuestionTemplate = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
83
91
84
92
Chat History:
@@ -91,8 +99,15 @@ export async function* talkToDocument(selectedModel, vectorStore, input: Convers
91
99
Do not use any other sources of information.
92
100
Do not provide any answer that is not based on the context.
93
101
If there is no answer, type "Not sure based on the context".
102
+ Additionally you will be given metadata like
103
+ title,content,length,excerpt,byline,dir,siteName,lang
104
+ in the metadata field. Use this information to help you answer the question.
105
+
94
106
{context}
95
107
108
+ Metadata:
109
+ {metadata}
110
+
96
111
Question: {question}
97
112
Answer:
98
113
` ) ;
@@ -109,6 +124,7 @@ export async function* talkToDocument(selectedModel, vectorStore, input: Convers
109
124
{
110
125
context : retriever . pipe ( formatDocumentsAsString ) ,
111
126
question : new RunnablePassthrough ( ) ,
127
+ metadata : retriever . pipe ( documents => getMetadataString ( documents [ 0 ] . metadata ) ) ,
112
128
} ,
113
129
prompt ,
114
130
llm ,
@@ -122,6 +138,20 @@ export async function* talkToDocument(selectedModel, vectorStore, input: Convers
122
138
}
123
139
}
124
140
141
+ function getMetadataString ( metadata ) {
142
+ const result = [ ] ;
143
+
144
+ for ( const key in metadata ) {
145
+ // Check if the property is not an object and not an array
146
+ if ( Object . prototype . hasOwnProperty . call ( metadata , key ) && typeof metadata [ key ] !== 'object' ) {
147
+ result . push ( `${ key } : ${ metadata [ key ] } ` ) ;
148
+ }
149
+ }
150
+ console . log ( 'result' , result ) ;
151
+
152
+ return result . join ( ' ' ) ;
153
+ }
154
+
125
155
export const formatChatHistory = ( chatHistory : { question : string ; answer : string } [ ] ) => {
126
156
console . log ( 'chatHistory' , chatHistory ) ;
127
157
const formattedDialogueTurns = chatHistory . map (
@@ -174,7 +204,7 @@ export async function* chatWithLLM(selectedModel, input: ConversationalRetrieval
174
204
const llm = new ChatOllama ( {
175
205
baseUrl : OLLAMA_BASE_URL ,
176
206
model : selectedModel ,
177
- temperature : 0 ,
207
+ temperature : 0.3 ,
178
208
} ) ;
179
209
const chatPrompt = ChatPromptTemplate . fromMessages ( [
180
210
[
@@ -207,7 +237,6 @@ export async function* chatWithLLM(selectedModel, input: ConversationalRetrieval
207
237
} ) ;
208
238
209
239
for await ( const chunk of stream ) {
210
- console . log ( 'chunk' , chunk ) ;
211
240
yield chunk . response ;
212
241
}
213
242
}
0 commit comments