|
| 1 | +import { heading } from '@/utils/heading'; |
| 2 | +import * as p from '@clack/prompts'; |
| 3 | +import { Langbase, EmbeddingModels } from 'langbase'; |
| 4 | +import { getApiKey } from '@/utils/get-langbase-api-key'; |
| 5 | +import { memoryNameSchema } from 'types/memory'; |
| 6 | +import { fromZodError } from 'zod-validation-error'; |
| 7 | + |
| 8 | +export async function createMemory() { |
| 9 | + const apiKey = await getApiKey(); |
| 10 | + |
| 11 | + p.intro(heading({ text: 'MEMORY', sub: 'Create a new memory' })); |
| 12 | + await p.group( |
| 13 | + { |
| 14 | + name: () => |
| 15 | + p.text({ |
| 16 | + message: 'Name of the memory', |
| 17 | + placeholder: 'cli-memory', |
| 18 | + validate: value => { |
| 19 | + const validatedName = memoryNameSchema.safeParse(value); |
| 20 | + if (!validatedName.success) { |
| 21 | + const validationError = fromZodError( |
| 22 | + validatedName.error |
| 23 | + ).toString(); |
| 24 | + return validationError; |
| 25 | + } |
| 26 | + return; |
| 27 | + } |
| 28 | + }), |
| 29 | + description: () => |
| 30 | + p.text({ |
| 31 | + message: 'Description of the memory', |
| 32 | + placeholder: 'This is a CLI memory' |
| 33 | + }), |
| 34 | + model: () => |
| 35 | + p.select({ |
| 36 | + message: 'Embedding Model', |
| 37 | + options: [ |
| 38 | + { value: 'openai:text-embedding-3-large', label: 'OpenAI Text Embedding 3 Large' }, |
| 39 | + { value: 'cohere:embed-multilingual-v3.0', label: 'Cohere Embed Multilingual v3.0' }, |
| 40 | + { value: 'cohere:embed-multilingual-light-v3.0', label: 'Cohere Embed Multilingual Light v3.0' }, |
| 41 | + { value: 'google:text-embedding-004', label: 'Google Text Embedding 004' } |
| 42 | + ] |
| 43 | + }) as Promise<string>, |
| 44 | + topK: () => |
| 45 | + p.text({ |
| 46 | + message: 'Top K', |
| 47 | + placeholder: '10', |
| 48 | + validate: value => { |
| 49 | + if (Number(value) < 1 || Number(value) > 100) { |
| 50 | + return 'Top K must be between 1 and 100'; |
| 51 | + } |
| 52 | + return; |
| 53 | + } |
| 54 | + }), |
| 55 | + chunkSize: () => |
| 56 | + p.text({ |
| 57 | + message: 'Chunk size', |
| 58 | + placeholder: '10000', |
| 59 | + validate: value => { |
| 60 | + if (Number(value) < 1024 || Number(value) > 300000) { |
| 61 | + return 'Chunk size must be between 1024 and 300000'; |
| 62 | + } |
| 63 | + return; |
| 64 | + } |
| 65 | + }), |
| 66 | + chunkOverlap: () => |
| 67 | + p.text({ |
| 68 | + message: 'Chunk overlap', |
| 69 | + placeholder: '1000', |
| 70 | + validate: value => { |
| 71 | + if (Number(value) < 100 || Number(value) > 1000) { |
| 72 | + return 'Chunk overlap must be between 100 and 1000'; |
| 73 | + } |
| 74 | + return; |
| 75 | + } |
| 76 | + }), |
| 77 | + |
| 78 | + }, |
| 79 | + { |
| 80 | + onCancel: () => { |
| 81 | + p.cancel('Operation cancelled.'); |
| 82 | + process.exit(0); |
| 83 | + } |
| 84 | + } |
| 85 | + ).then(async ({ name, description, model, topK, chunkSize, chunkOverlap }) => { |
| 86 | + const langbase = new Langbase({ |
| 87 | + apiKey: apiKey |
| 88 | + }); |
| 89 | + |
| 90 | + const spinner = p.spinner(); |
| 91 | + spinner.start('Memory is being created...'); |
| 92 | + |
| 93 | + const memory = await langbase.memories.create({ |
| 94 | + name, |
| 95 | + description, |
| 96 | + top_k: Number(topK), |
| 97 | + chunk_size: Number(chunkSize), |
| 98 | + chunk_overlap: Number(chunkOverlap), |
| 99 | + embedding_model: model as EmbeddingModels, |
| 100 | + }); |
| 101 | + |
| 102 | + spinner.stop('Memory created successfully!'); |
| 103 | + p.outro(heading({ text: 'MEMORY', sub: `${memory.name} created successfully` })); |
| 104 | + }); |
| 105 | +} |
0 commit comments