From 393c73c2170bb4d16e64f931921a6ee513895349 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 11 Sep 2024 18:39:37 -0300 Subject: [PATCH] fix (templates): ai-search OK. --- packages/llm.gblib/services/ChatServices.ts | 4 ++-- src/util.ts | 21 +++++++++++++++---- .../talk-to-data.gbot/config.csv | 8 +++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/llm.gblib/services/ChatServices.ts b/packages/llm.gblib/services/ChatServices.ts index 033f24aa..cff26a21 100644 --- a/packages/llm.gblib/services/ChatServices.ts +++ b/packages/llm.gblib/services/ChatServices.ts @@ -414,12 +414,12 @@ export class ChatServices { sources as an array of ('file' indicating the PDF filename and 'page' indicating the page number) listing all segmented context. Example JSON format: "text": "this is the answer, anything LLM output as text answer shoud be here.", "sources": [{{"file": "filename.pdf", "page": 3}}, {{"file": "filename2.pdf", "page": 1}}], - return valid JSON with brackets. Avoid explaining the context directly + return *valid READY TO BE PARSED JSON* with brackets. Avoid explaining the context directly to the Human; instead, refer to the document source, always return more than one source document and check if the answer can be extended by using additional contexts in other files, as specified before. - Double check if the output is a valid JSON with brackets. all fields are required: text, file, page. + Double check if the output is a valid RFC 8259 JSON. all fields are required: text, file, page. `; const combineDocumentsPrompt = ChatPromptTemplate.fromMessages([ diff --git a/src/util.ts b/src/util.ts index 17c5325a..e678d32e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -120,24 +120,37 @@ export class GBUtil { } public static caseInsensitive(listOrRow) { + // If the input is not an object or array, return it as is if (!listOrRow || typeof listOrRow !== 'object') { return listOrRow; } - const lowercase = oldKey => (typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey); + + // Helper function to convert property names to lowercase + const lowercase = key => typeof key === 'string' ? key.toLowerCase() : key; + + // Create a proxy that maps property accesses to lowercase property names const createCaseInsensitiveProxy = obj => { - const propertiesMap = new Map(Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]])); + const propertiesMap = new Map( + Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]]) + ); + const caseInsensitiveGetHandler = { get: (target, property) => propertiesMap.get(lowercase(property)) }; + return new Proxy(obj, caseInsensitiveGetHandler); }; + + // Handle arrays by mapping each element to a case-insensitive proxy if (Array.isArray(listOrRow)) { - return listOrRow.map(row => createCaseInsensitiveProxy(row)); + return listOrRow.map(row => + typeof row === 'object' && row !== null ? createCaseInsensitiveProxy(row) : row + ); } else { return createCaseInsensitiveProxy(listOrRow); } } - + public static async exists(filePath: string): Promise { try { await fs.access(filePath); diff --git a/templates/talk-to-data.gbai/talk-to-data.gbot/config.csv b/templates/talk-to-data.gbai/talk-to-data.gbot/config.csv index eb1e3b88..b5f99f48 100644 --- a/templates/talk-to-data.gbai/talk-to-data.gbot/config.csv +++ b/templates/talk-to-data.gbai/talk-to-data.gbot/config.csv @@ -1,4 +1,4 @@ - name,value - Answer Mode,sql - llm File,northwind.db - llm Driver,sqlite \ No newline at end of file +name,value +Answer Mode,sql +llm File,northwind.db +llm Driver,sqlite \ No newline at end of file