Skip to content

Commit

Permalink
fix(llm.gblib): Fix in doc. publishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Nov 22, 2024
1 parent a23ba24 commit 1f74549
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 92 deletions.
4 changes: 3 additions & 1 deletion packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ export class GBDeployer implements IGBDeployer {
try {
vectorStore = await HNSWLib.load(min['vectorStorePath'], embedding);
} catch (e) {
GBLogEx.info(min, `Creating new store... ${GBUtil.toYAML(e)}`);
GBLogEx.info(min, `Creating new store...`);
vectorStore = new HNSWLib(embedding, {
space: 'cosine'
});
await vectorStore.save(min['vectorStorePath']);

}
return vectorStore;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,9 @@ export class KBService implements IGBKBService {
}

files = await walkPromise(urlJoin(localPath, 'docs'));

if (files[0]) {
GBLogEx.info(min, `Add embeddings from .gbkb: ${files.length}}...`);
await CollectionUtil.asyncForEach(files, async file => {
let content = null;
let filePath = path.join(file.root, file.name);
Expand Down
73 changes: 32 additions & 41 deletions packages/llm.gblib/services/ChatServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,19 @@ import {
import { RunnableSequence } from '@langchain/core/runnables';
import { DynamicStructuredTool } from '@langchain/core/tools';
import { convertToOpenAITool } from '@langchain/core/utils/function_calling';
import { ChatOpenAI, OpenAI } from '@langchain/openai';
import { SqlDatabaseChain } from 'langchain/chains/sql_db';
import { ChatOpenAI } from '@langchain/openai';
import { SqlDatabase } from 'langchain/sql_db';
import { DataSource } from 'typeorm';
import { GBMinInstance } from 'botlib';
import fs from 'fs/promises';
import { jsonSchemaToZod } from 'json-schema-to-zod';
import { BufferWindowMemory } from 'langchain/memory';
import path from 'path';
import { PngPageOutput, pdfToPng } from 'pdf-to-png-converter';
import { getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs';
import { CollectionUtil } from 'pragmatismo-io-framework';
import urlJoin from 'url-join';
import { GBServer } from '../../../src/app.js';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
import { GBVMService } from '../../basic.gblib/services/GBVMService.js';
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
import {
DEFAULT_SQL_DATABASE_PROMPT,
SQL_POSTGRES_PROMPT,
SQL_SQLITE_PROMPT,
SQL_MSSQL_PROMPT,
SQL_MYSQL_PROMPT
} from 'langchain/chains/sql_db';
import { GBUtil } from '../../../src/util.js';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
export interface CustomOutputParserFields { }
export type ExpectedOutput = any;

Expand Down Expand Up @@ -140,40 +125,46 @@ export class GBLLMOutputParser extends BaseLLMOutputParser<ExpectedOutput> {
res = JSON.parse(result);
} catch (e) {
GBLogEx.verbose(this.min, `LLM JSON error: ${GBUtil.toYAML(e)}.`);

return result;
}

let { sources, text } = res;

await CollectionUtil.asyncForEach(sources, async source => {
let found = false;
if (source && source.file.endsWith('.pdf')) {
const gbaiName = GBUtil.getGBAIPath(this.min.botId, 'gbkb');
const localName = path.join(process.env.PWD, 'work', gbaiName, 'docs', source.file);

if (localName) {
const pngs = await GBUtil.pdfPageAsImage(this.min, localName, source.page);

if (!isNaN(this.user.userSystemId)) {
await this.min.whatsAppDirectLine.sendFileToDevice(
this.user.userSystemId, pngs[0].url,
localName, null, undefined, true);
if (!sources) {

}
else {
text = `![alt text](${pngs[0].url})
GBLogEx.verbose(this.min, `LLM JSON output sources is NULL.`);
}
else {
await CollectionUtil.asyncForEach(sources, async source => {
let found = false;
if (source && source.file.endsWith('.pdf')) {
const gbaiName = GBUtil.getGBAIPath(this.min.botId, 'gbkb');
const localName = path.join(process.env.PWD, 'work', gbaiName, 'docs', source.file);

if (localName) {
const pngs = await GBUtil.pdfPageAsImage(this.min, localName, source.page);

if (!isNaN(this.user.userSystemId)) {
await this.min.whatsAppDirectLine.sendFileToDevice(
this.user.userSystemId, pngs[0].url,
localName, null, undefined, true);

}
else {
text = `![alt text](${pngs[0].url})
${text}`;
}
found = true;
source.file = localName;
}
found = true;
source.file = localName;
}
}

if (!found) {
GBLogEx.info(this.min, `File not found referenced in other .pdf: ${source.file}`);
}
});

if (!found) {
GBLogEx.info(this.min, `File not found referenced in other .pdf: ${source.file}`);
}
});
}
return { text, sources };
}
}
Expand Down Expand Up @@ -307,7 +298,7 @@ export class ChatServices {
}

const LLMMode = (mode ?? answerMode).toLowerCase();


let memory;
if (user && !this.memoryMap[user.userSystemId]) {
Expand Down
52 changes: 52 additions & 0 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,54 @@ export class WhatsappDirectLine extends GBService {

}

// New method to send button list
private async sendButtonList(to: string, buttons: string[]) {
const baseUrl = 'https://graph.facebook.com/v20.0';
const accessToken = this.whatsappServiceKey;
const sendMessageEndpoint = `${baseUrl}/${this.whatsappServiceNumber}/messages`;

const messageData = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
to: to,
type: 'interactive',
interactive: {
type: 'button',
body: {
text: 'Please select an option:'
},
action: {
buttons: buttons.map((button, index) => ({
type: 'reply',
reply: {
id: `button_${index + 1}`,
title: button
}
}))
}
}
};

const response = await fetch(sendMessageEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(messageData)
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(`Failed to send button list: ${JSON.stringify(errorData)}`);
}

const result = await response.json();
GBLogEx.info(this.min, 'Button list sent successfully:' + JSON.stringify(result));
return result;
}


public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) {
try {
const cmd = '/audio ';
Expand All @@ -941,6 +989,10 @@ export class WhatsappDirectLine extends GBService {
case 'meta':
if (msg['name']) {
await this.customClient.sendTemplate(to, msg['name'], 'pt_BR', msg['components']);
} else if (msg.startsWith('[[') && msg.endsWith(']]')) {
// Parse the button list
const buttons = JSON.parse(msg);
await this.sendButtonList(to, buttons);
} else {
messages = msg.match(/(.|[\r\n]){1,4096}/g);

Expand Down
Loading

0 comments on commit 1f74549

Please sign in to comment.