-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathlib.ts
213 lines (191 loc) · 6.18 KB
/
lib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Lib file for memory module
*
* All functions return {data: any|null, error: string|null}
*/
import * as p from '@clack/prompts';
import fs from 'fs';
import type { Message } from 'types/pipe';
import { fromZodError } from 'zod-validation-error';
import { defaultRagPrompt, MEMORYSETS } from './constants';
import {
cosineSimilaritySearch,
getDocumentsFromMemory,
type SimilarChunk
} from './db/lib';
// import { generateLocalEmbeddings } from './generate-local-embeddings';
import { dlog } from '@/dev/utils/dlog';
import { memoryDocSchema, memoryNameSchema } from 'types/memory';
import { loadConfig } from '../config/config-handler';
import { logger } from '../logger-utils';
import { generateLocalEmbeddings } from './generate-local-embeddings';
import { getOpenAIEmbeddings } from './generate-openai-embeddings';
import { getMessageContent } from '@/dev/utils/thread/get-message-content';
export async function checkDirectoryExists(directoryPath: string) {
try {
await fs.promises.access(directoryPath);
return {
data: true,
error: null
};
} catch (error: any) {
console.log('utils/memory/lib.ts: checkDirectoryExists: error:', error);
return {
data: null,
error: getErrorMsg(error, 'Error checking directory exists')
};
}
}
export const getErrorMsg = (error: any, defaultMsg: string) => {
const isErrorMessage = error instanceof Error;
const errorMessage = isErrorMessage
? error.message.trim() || defaultMsg
: defaultMsg;
return errorMessage;
};
/**
* Formats the given number of bytes into a human-readable string representation.
* @param bytes - The number of bytes to format.
* @returns A string representing the formatted size.
*/
export function formatDocSize(bytes: number): string {
if (bytes === 0) return '0 bytes';
if (bytes < 1024) {
return bytes.toFixed(2) + ' bytes';
} else if (bytes < 1024 * 1024) {
const fileSizeInKB = bytes / 1024;
return fileSizeInKB.toFixed(2) + ' KB';
} else {
const fileSizeInMB = bytes / (1024 * 1024);
return fileSizeInMB.toFixed(2) + ' MB';
}
}
/**
* Validates the given memory name.
*
* @param memoryName - The name of the memory to validate.
* @returns The validated memory name.
* Logs error and exits process if the memory name is invalid.
*/
export const validateMemoryName = (memoryName: string) => {
const validatedName = memoryNameSchema.safeParse(memoryName);
if (!validatedName.success) {
const err = fromZodError(validatedName.error).message;
p.cancel(`Invalid memory name: ${err}`);
process.exit(1);
}
return validatedName.data;
};
/**
* Validates the document embedding schema using the provided memory and document names.
*
* @param {Object} params - The parameters for the function.
* @param {string} params.memoryName - The name of the memory.
* @param {string} params.documentName - The name of the document.
* @returns {Object} The validated data if the input is valid.
* @throws Will terminate the process if the input is invalid.
*/
export const validateMemoryDocNames = ({
memoryName,
documentName
}: {
memoryName: string;
documentName: string;
}) => {
const validatedData = memoryDocSchema.safeParse({
memoryName,
documentName
});
if (!validatedData.success) {
const err = fromZodError(validatedData.error).message;
p.cancel(`Invalid input: ${err}`);
process.exit(1);
}
return validatedData.data;
};
export const getAugmentedContext = ({
similarChunks,
messages
}: {
similarChunks: SimilarChunk[];
messages: Message[];
}) => {
if (similarChunks.length === 0) return '';
const memoryContext = similarChunks
.map(chunk => `${chunk.text} \n\n Source: ${chunk.attributes.docName}`)
.join('\n\n');
// Extract Rag prompt from the messages.
const ragMsg = messages.find(m => m.role === 'system' && m.name === 'rag');
// If there is no rag prompt, use the default rag prompt.
const ragPrompt = ragMsg?.content || defaultRagPrompt;
const contextContent = `"""CONTEXT:\n ${memoryContext}"""`;
const augmentedContext = `"""${ragPrompt}""" \n\n ${contextContent}`;
return augmentedContext;
};
export const addContextFromMemory = async ({
messages,
memoryNames
}: {
messages: Message[];
memoryNames: string[];
}) => {
try {
// Check if there are no memory names.
const isMemoryAttached = memoryNames.length > 0;
logger('memory', isMemoryAttached, 'Memory attached');
// Check if there are no messages.
const messagesExist = messages.length > 0;
// Return the messages if there are no memory names or messages.
if (!isMemoryAttached || !messagesExist) return;
// This will be the user prompt.
const lastUserMsg = [...messages]
.reverse()
.find(m => m.role === 'user');
const userPrompt = getMessageContent(lastUserMsg!);
// If there is no user prompt, return the messages.
if (!userPrompt) return;
// 1- Generate the embeddings of the user prompt.
// Read config to determine which embedding to use.
const config = await loadConfig();
const useLocalEmbeddings = config.memory?.useLocalEmbeddings || false;
let embeddings = [];
if (useLocalEmbeddings) {
// Use local embeddings
dlog('Generating local embeddings');
embeddings = await generateLocalEmbeddings([userPrompt]);
} else {
// Use OpenAI embeddings
dlog('Generating OpenAI embeddings');
embeddings = await getOpenAIEmbeddings([userPrompt]);
}
// 2- Get all the memorysets from the db.
const memoryChunks = await getDocumentsFromMemory(memoryNames);
if (memoryChunks.length === 0) return;
// 3- Get similar chunks from the memorysets.
const similarChunks = cosineSimilaritySearch({
chunks: memoryChunks,
queryEmbedding: embeddings[0].embedding,
topK: MEMORYSETS.MAX_CHUNKS_ATTACHED_TO_LLM
});
if (similarChunks.length === 0) return;
logger('memory.similarChunks', similarChunks);
return similarChunks;
} catch (error: any) {
dlog('utils/memory/lib.ts: addContextFromMemory: error:', error);
}
};
export const validateEmbedDocInput = ({
memoryName,
documentName
}: {
memoryName: string;
documentName: string;
}) => {
const validatedName = memoryNameSchema.safeParse(memoryName);
if (!validatedName.success) {
const err = fromZodError(validatedName.error).message;
p.cancel(`Invalid memory name: ${err}`);
process.exit(1);
}
return validatedName.data;
};