({
memory,
query
}: {
memory: string;
query: string;
})
| 15 | import { dlog } from '@/dev/utils/dlog'; |
| 16 | |
| 17 | export async function retrieveMemory({ |
| 18 | memory, |
| 19 | query |
| 20 | }: { |
| 21 | memory: string; |
| 22 | query: string; |
| 23 | }) { |
| 24 | p.intro( |
| 25 | heading({ |
| 26 | text: 'RETRIEVE', |
| 27 | sub: 'Retrieve similar chunks of a memory' |
| 28 | }) |
| 29 | ); |
| 30 | |
| 31 | // Spinner to show current action. |
| 32 | const spin = p.spinner(); |
| 33 | |
| 34 | try { |
| 35 | if (!memory) { |
| 36 | p.log.error( |
| 37 | 'Memory name is required. Use --memory or -m flag to specify.' |
| 38 | ); |
| 39 | process.exit(1); |
| 40 | } |
| 41 | |
| 42 | if (!query) { |
| 43 | p.log.error( |
| 44 | 'Query is required. Use --query, or -q flag to specify.' |
| 45 | ); |
| 46 | process.exit(1); |
| 47 | } |
| 48 | |
| 49 | // 1- Check memory exists. |
| 50 | const memoryName = validateMemoryName(memory); // Throws error if invalid so no need to check success |
| 51 | await checkMemoryExists(memoryName); |
| 52 | |
| 53 | // 2- Load memory data. |
| 54 | spin.start('Processing memory data...'); |
| 55 | const memoryChunks = await getDocumentsFromMemory([memory]); |
| 56 | if (memoryChunks.length === 0) |
| 57 | return p.log.error( |
| 58 | 'Please make sure the memory has one or more documents and they are embedded.' |
| 59 | ); |
| 60 | |
| 61 | // 3- Generate embeddings of query |
| 62 | spin.message('Generating embeddings...'); |
| 63 | |
| 64 | // Read config to determine which embedding to use. |
| 65 | const config = await loadConfig(); |
| 66 | const useLocalEmbeddings = config.memory?.useLocalEmbeddings || false; |
| 67 | |
| 68 | let queryEmbedding = []; |
| 69 | if (useLocalEmbeddings) { |
| 70 | // Use local embeddings |
| 71 | dlog('Generating local embeddings'); |
| 72 | queryEmbedding = await generateLocalEmbeddings([query]); |
| 73 | } else { |
| 74 | // Use OpenAI embeddings |
no test coverage detected