Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Append uploaded images to message history #3105

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class BufferMemory extends FlowiseMemory implements MemoryMethods {
}

if (returnBaseMessages) {
return mapChatMessageToBaseMessage(chatMessage)
return await mapChatMessageToBaseMessage(chatMessage)
}

let returnIMessages: IMessage[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ CREATE TABLE IF NOT EXISTS ${this.tableName} (
}

if (returnBaseMessages) {
return mapChatMessageToBaseMessage(chatMessage)
return await mapChatMessageToBaseMessage(chatMessage)
}

let returnIMessages: IMessage[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods {
}

if (returnBaseMessages) {
return mapChatMessageToBaseMessage(chatMessage)
return await mapChatMessageToBaseMessage(chatMessage)
}

let returnIMessages: IMessage[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class BufferWindowMemoryExtended extends FlowiseWindowMemory implements MemoryMe
}

if (returnBaseMessages) {
return mapChatMessageToBaseMessage(chatMessage)
return await mapChatMessageToBaseMessage(chatMessage)
}

let returnIMessages: IMessage[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ConversationSummaryBufferMemoryExtended extends FlowiseSummaryBufferMemory
chatMessage.unshift(...prependMessages)
}

let baseMessages = mapChatMessageToBaseMessage(chatMessage)
let baseMessages = await mapChatMessageToBaseMessage(chatMessage)

// Prune baseMessages if it exceeds max token limit
if (this.movingSummaryBuffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ConversationSummaryMemoryExtended extends FlowiseSummaryMemory implements
chatMessage.unshift(...prependMessages)
}

const baseMessages = mapChatMessageToBaseMessage(chatMessage)
const baseMessages = await mapChatMessageToBaseMessage(chatMessage)

// Get summary
if (this.llm && typeof this.llm !== 'string') {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/nodes/memory/DynamoDb/DynamoDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods {
.filter((x): x is StoredMessage => x.type !== undefined && x.data.content !== undefined)
const baseMessages = messages.map(mapStoredMessageToChatMessage)
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods {
const messages = document?.messages || []
const baseMessages = messages.map(mapStoredMessageToChatMessage)
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods {
const orderedMessages = rawStoredMessages.reverse().map((message) => JSON.parse(message))
const baseMessages = orderedMessages.map(mapStoredMessageToChatMessage)
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class BufferMemoryExtended extends FlowiseMemory implements MemoryMethods {
const previousMessages = orderedMessages.filter((x): x is StoredMessage => x.type !== undefined && x.data.content !== undefined)
const baseMessages = previousMessages.map(mapStoredMessageToChatMessage)
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/components/nodes/memory/ZepMemory/ZepMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ZepMemoryExtended extends ZepMemory implements MemoryMethods {
const memoryVariables = await this.loadMemoryVariables({}, id)
const baseMessages = memoryVariables[this.memoryKey]
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ZepMemoryExtended extends ZepMemory implements MemoryMethods {
const memoryVariables = await this.loadMemoryVariables({}, id)
const baseMessages = memoryVariables[this.memoryKey]
if (prependMessages?.length) {
baseMessages.unshift(...mapChatMessageToBaseMessage(prependMessages))
baseMessages.unshift(...(await mapChatMessageToBaseMessage(prependMessages)))
}
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
}
Expand Down
51 changes: 48 additions & 3 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import * as path from 'path'
import { JSDOM } from 'jsdom'
import { z } from 'zod'
import { DataSource } from 'typeorm'
import { ICommonObject, IDatabaseEntity, IMessage, INodeData, IVariable } from './Interface'
import { ICommonObject, IDatabaseEntity, IMessage, INodeData, IVariable, MessageContentImageUrl } from './Interface'
import { AES, enc } from 'crypto-js'
import { AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
import { getFileFromStorage } from './storageUtils'

export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
Expand Down Expand Up @@ -601,14 +602,58 @@ export const getUserHome = (): string => {
* @param {IChatMessage[]} chatmessages
* @returns {BaseMessage[]}
*/
export const mapChatMessageToBaseMessage = (chatmessages: any[] = []): BaseMessage[] => {
export const mapChatMessageToBaseMessage = async (chatmessages: any[] = []): Promise<BaseMessage[]> => {
const chatHistory = []

for (const message of chatmessages) {
if (message.role === 'apiMessage' || message.type === 'apiMessage') {
chatHistory.push(new AIMessage(message.content || ''))
} else if (message.role === 'userMessage' || message.role === 'userMessage') {
chatHistory.push(new HumanMessage(message.content || ''))
// check for image uploads
if (message.fileUploads) {
// example: [{"type":"stored-file","name":"0_DiXc4ZklSTo3M8J4.jpg","mime":"image/jpeg"}]
try {
const uploads = JSON.parse(message.fileUploads)
const imageContents: MessageContentImageUrl[] = []
for (const upload of uploads) {
if (upload.type === 'stored-file') {
const fileData = await getFileFromStorage(upload.name, message.chatflowid, message.chatId)
// as the image is stored in the server, read the file and convert it to base64
const bf = 'data:' + upload.mime + ';base64,' + fileData.toString('base64')

imageContents.push({
type: 'image_url',
image_url: {
url: bf
}
})
} else if (upload.type === 'url') {
imageContents.push({
type: 'image_url',
image_url: {
url: upload.data
}
})
}
}
chatHistory.push(
new HumanMessage({
content: [
{
type: 'text',
text: message.content
},
...imageContents
]
})
)
} catch (e) {
// failed to parse fileUploads, continue with text only
chatHistory.push(new HumanMessage(message.content || ''))
}
} else {
chatHistory.push(new HumanMessage(message.content || ''))
}
}
}
return chatHistory
Expand Down
Loading