From edd800f36ec455ecc912e0408c862223a198fc10 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 20 Sep 2023 15:25:44 -0300 Subject: [PATCH] fix(kb.gbapp): #361 GPT functions in Word. --- packages/basic.gblib/services/GBVMService.ts | 4 +-- packages/gpt.gblib/services/ChatServices.ts | 36 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 6fdfc49f..2a85e19b 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -198,7 +198,7 @@ export class GBVMService extends GBService { } } while (include); - let { code, map: jsonMap, metadata } = await this.convert(mainName, basicCode); + let { code, map, metadata } = await this.convert(mainName, basicCode); // Generates function JSON metadata to be used later. @@ -207,7 +207,7 @@ export class GBVMService extends GBService { const mapFile = `${filename}.map`; - Fs.writeFileSync(mapFile, JSON.stringify(jsonMap)); + Fs.writeFileSync(mapFile, JSON.stringify(map)); // Run JS into the GB context. diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index 31d0b7b7..92825670 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -36,6 +36,9 @@ import { GBMinInstance } from 'botlib'; import OpenAI from "openai"; import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' import { CollectionUtil } from 'pragmatismo-io-framework'; +import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords'; +import Path from 'path'; +import * as Fs from 'fs'; export class ChatServices { @@ -52,24 +55,21 @@ export class ChatServices { throw new Error('Open AI Key not configured in .gbot.'); } let functions = []; - + + // Adds .gbdialog as functions if any to GPT Functions. + await CollectionUtil.asyncForEach(Object.values(min.scriptMap), async script => { - - functions.push({ - "name": "get_current_weather", - "description": "Get the current weather in a given location", - "parameters": { - "type": "object", - "properties": { - "location": { - "type": "string", - "description": "The city and state, e.g. San Francisco, CA", - }, - "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, - }, - "required": ["location"], - } }); - }); + const path = DialogKeywords.getGBAIPath(min.botId, "gbdialog", null); + const localFolder = Path.join('work', path, `${script}.json`); + + if (Fs.existsSync(localFolder)) { + const func = Fs.readFileSync(localFolder).toJSON(); + functions.push(func); + } + + }); + + // Calls Model. const openai = new OpenAI({ apiKey: key @@ -110,7 +110,7 @@ export class ChatServices { const chatCompletion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: text }] - + }); return chatCompletion.choices[0].message.content; }